Face Detection on Desktop
Prerequisites
redis server is running
face detection service is running
Code (in separate terminals):
redis-server conf/redis/redis.conf
run-face-detection
Walkthrough
Import necessary libraries
import queue
import cv2
from sic_framework.core import utils_cv2
from sic_framework.core.message_python2 import (
BoundingBoxesMessage,
CompressedImageMessage,
)
from sic_framework.devices.common_desktop.desktop_camera import DesktopCameraConf
from sic_framework.devices.desktop import Desktop
from sic_framework.services.face_detection.face_detection import FaceDetection
Create buffers for image/face objects, define callback functions
Whenever a new image or face detection is received, on_image or on_faces will be called to place the data object into its respective buffer.
imgs_buffer = queue.Queue(maxsize=1)
faces_buffer = queue.Queue(maxsize=1)
def on_image(image_message: CompressedImageMessage):
imgs_buffer.put(image_message.image)
def on_faces(message: BoundingBoxesMessage):
faces_buffer.put(message.bboxes)
Set up the camera, register callback functions
# Create camera configuration using fx and fy to resize the image along x- and y-axis, and possibly flip image
conf = DesktopCameraConf(fx=1.0, fy=1.0, flip=1)
# Connect to the services
desktop = Desktop(camera_conf=conf)
face_rec = FaceDetection()
# Feed the camera images into the face recognition component
face_rec.connect(desktop.camera)
# Send the outputs back to this program
desktop.camera.register_callback(on_image)
face_rec.register_callback(on_faces)
Display face detection
while True:
img = imgs_buffer.get()
faces = faces_buffer.get()
for face in faces:
utils_cv2.draw_bbox_on_image(face, img)
cv2.imshow("", img)
cv2.waitKey(1)
The full code can be found here
, multiple selections available,