Last active
July 24, 2018 18:24
-
-
Save minakhan01/cb92298f5d7459a5309b9c97fb047a47 to your computer and use it in GitHub Desktop.
main python in AIY-projects-python/src/examples/vision/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright 2017 Google Inc. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# MINA's COMMENT START | |
# cp to folder: AIY-projects-python/src/examples/vision/ | |
# based on this link: https://github.com/minakhan01/VisionKitTry2/blob/master/pi/AIY-projects-python/src/examples/vision/object_detection.py | |
# MINA's COMMENT END | |
"""Object detection library demo. | |
- Takes an input image and tries to detect person, dog, or cat. | |
- Draws bounding boxes around detected objects. | |
- Saves an image with bounding boxes around detected objects. | |
""" | |
import argparse | |
import io | |
import sys | |
from PIL import Image | |
from PIL import ImageDraw | |
from aiy.vision.inference import ImageInference | |
from aiy.vision.models import coco_object_detection | |
def _crop_center(image): | |
width, height = image.size | |
size = min(width, height) | |
x, y = (width - size) / 2, (height - size) / 2 | |
return image.crop((x, y, x + size, y + size)), (x, y) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--input', '-i', dest='input', required=True) | |
parser.add_argument('--output', '-o', dest='output') | |
args = parser.parse_args() | |
with ImageInference(coco_object_detection.model()) as inference: | |
image = Image.open( | |
io.BytesIO(sys.stdin.buffer.read()) | |
if args.input == '-' else args.input) | |
image_center, offset = _crop_center(image) | |
draw = ImageDraw.Draw(image) | |
result = inference.run(image_center) | |
for i, obj in enumerate(coco_object_detection.get_objects(result, 0.3, offset)): | |
print('Object #%d: %s' % (i, str(obj))) | |
x, y, width, height = obj.bounding_box | |
draw.rectangle((x, y, x + width, y + height), outline='red') | |
if args.output: | |
image.save(args.output) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
changes: update all object_detection to coco_object_detection
coco_object_detection mode gist: coco_object_detection: https://gist.github.com/minakhan01/a1c5707df8b5d0c964885a1929360e49