google / aiyprojects-raspbian

API libraries, samples, and system images for AIY Projects (Voice Kit and Vision Kit)
https://aiyprojects.withgoogle.com/
Apache License 2.0
1.63k stars 694 forks source link

Bounding Box Size #506

Open SwannSchilling opened 6 years ago

SwannSchilling commented 6 years ago

Hello, I am using the face_detection_raspivid.py, it seems to have the least load on the pi's cpu! It all works out great, but since building a face tracking system, I realized that the len(faces) sometimes also returns negative values for the face.bounding_box! Which would be the numerical range that the face.bounding_box returns? Is there a set min/max value? How could I set the returned min/max value myself?

dmitriykovalev commented 6 years ago

Thanks for reporting this! Looks like a bug in our code. Please make sure that bounding box is inside (x=0, y=0, w=result.width, y=result.width) rectangle where result is returned from inference.run() call.

SwannSchilling commented 6 years ago

This is the code I am running!

#!/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_raspivid.py --num_frames 10
"""
import argparse

from contextlib import contextmanager
from aiy.vision.inference import CameraInference
from aiy.vision.models import face_detection

def avg_joy_score(faces):
    if faces:
        return sum(face.joy_score for face in faces) / len(faces)
    return 0.0

def raspivid_cmd(sensor_mode):
    return ('raspivid', '--mode', str(sensor_mode), '--timeout', '0', '--nopreview')

@contextmanager
def Process(cmd):
    process = subprocess.Popen(cmd)
    try:
        yield
    finally:
        process.terminate()
        process.wait()

def main():
    parser = argparse.ArgumentParser('Face detection using raspivid.')
    parser.add_argument('--num_frames', '-n', type=int, default=None,
        help='Sets the number of frames to run for, otherwise runs forever.')
    args = parser.parse_args()

    with Process(raspivid_cmd(sensor_mode=4)), \
         CameraInference(face_detection.model()) as inference:
        for result in inference.run(args.num_frames):
            faces = face_detection.get_faces(result)
            if len(faces) >= 1:
                for face in faces:
                    x = int(face.bounding_box[0])
                    y = int(face.bounding_box[1])
                    joy = int((avg_joy_score(faces))*1000)

if __name__ == '__main__':
    main()