Skip to content

Instantly share code, notes, and snippets.

@gelicia
Created October 27, 2018 17:15
Show Gist options
  • Save gelicia/0f479829a19612dc584c2920a78a85ac to your computer and use it in GitHub Desktop.
Save gelicia/0f479829a19612dc584c2920a78a85ac to your computer and use it in GitHub Desktop.
#!/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.
"""Camera inference face detection demo code.
Runs continuous face detection on the VisionBonnet and prints the number of
detected faces.
Example
face_detection_camera.py --num_frames 10
"""
import argparse
import io
import sys
from PIL import Image
from PIL import ImageDraw
from aiy.vision.inference import CameraInference
from aiy.vision.models import object_detection
from aiy.vision.annotator import Annotator
from picamera import PiCamera
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 read_stdin():
return io.BytesIO(sys.stdin.buffer.read())
def main():
"""Face detection camera inference example."""
parser = argparse.ArgumentParser()
parser.add_argument('--num_frames', '-n', type=int, dest='num_frames', default=None,
help='Sets the number of frames to run for, otherwise runs forever.')
args = parser.parse_args()
# Forced sensor mode, 1640x1232, full FoV. See:
# https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
# This is the resolution inference run on.
with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera:
camera.start_preview()
# Annotator renders in software so use a smaller size and scale results
# for increased performace.
annotator = Annotator(camera, dimensions=(320, 240))
scale_x = 320 / 1640
scale_y = 240 / 1232
# Incoming boxes are of the form (x, y, width, height). Scale and
# transform to the form (x1, y1, x2, y2).
def transform(bounding_box):
x, y, width, height = bounding_box
return (scale_x * x, scale_y * y, scale_x * (x + width),
scale_y * (y + height))
with CameraInference(face_detection.model()) as inference:
for result in inference.run(args.num_frames):
objs = object_detection.get_objects(result, 0.3);
annotator.clear()
for obj in objs:
annotator.bounding_box(transform(obj.bounding_box), fill=0)
annotator.update()
# faces = face_detection.get_faces(result)
# annotator.clear()
# for face in faces:
# annotator.bounding_box(transform(face.bounding_box), fill=0)
# annotator.update()
# print('#%05d (%5.2f fps): num_faces=%d, avg_joy_score=%.2f' %
# (inference.count, inference.rate, len(faces), avg_joy_score(faces)))
camera.stop_preview()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment