This repository was archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 696
/
Copy pathface_detection_camera.py
executable file
·78 lines (63 loc) · 2.79 KB
/
face_detection_camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/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
from picamera import PiCamera
from aiy.vision.inference import CameraInference
from aiy.vision.models import face_detection
from aiy.vision.annotator import Annotator
def avg_joy_score(faces):
if faces:
return sum(face.joy_score for face in faces) / len(faces)
return 0.0
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):
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()