yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
RPi AI Camera📖
01.Preparation before use
  • Preparation before use
02.Environment construction
  • Environment construction
03.rpicam-apps examples
  • rpicam-apps examples
04.PiCamera2 Examples
  • PiCamera2 Examples
05.Mediapipe Course
  • 1. Face effects
  • 2. Face Detection
  • 3. 3D Object Recognition
  • 4. Brush
  • 5. Finger control
  • 6. Gesture Recognition
06.Advanced AI Course
  • 1. Human Pose Estimation
  • 2. Object Detection
  • 3. yolov5
  • 4. QR code
  • 5. Face Detection
  • 6. Color recognition
Download
  • System_File
  • Code
  • Software Tools
Download ZIP

Welcome to RPi AI Camera repository

5. Face Detection

5. Face detection

Note: In this case, the AI camera does not have computing power bonus and is called as a normal camera!

File path: /home/pi/AI_Camera/face_detaction-CSI.ipynb

The test code is as follows:

import enum
import cv2
def bgr8_to_jpeg(value, quality=75):
    return bytes(cv2.imencode('.jpg', value)[1])
xxxxxxxxxx
import cv2
import ipywidgets.widgets as widgets
import threading
import time
import sys
import libcamera
from picamera2 import Picamera2
xxxxxxxxxx
import inspect
import ctypes
def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    tid = ctypes.c_long(tid)
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
def stop_thread(thread):
    _async_raise(thread.ident, SystemExit)
xxxxxxxxxx
# body_haar = cv2.CascadeClassifier("haarcascade_upperbody.xml")
face_haar = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_haar = cv2.CascadeClassifier("haarcascade_eye_tree_eyeglasses.xml")
def Camera_display():
    while 1:
        frame = picam2.capture_array()
        gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_haar.detectMultiScale(gray_img, 1.1, 3)
        for face_x,face_y,face_w,face_h in faces:
            cv2.rectangle(frame, (face_x, face_y), (face_x+face_w, face_y+face_h), (0,255,0), 2)
        eyes = eye_haar.detectMultiScale(gray_img, 1.1, 3)
        for eye_x,eye_y,eye_w,eye_h in eyes:
            cv2.rectangle(frame, (eye_x,eye_y), (eye_x+eye_w, eye_y+eye_h), (255,0,0), 2)            
        image_widget.value = bgr8_to_jpeg(frame)
        time.sleep(0.010)
xxxxxxxxxx
image_widget = widgets.Image(format='jpeg', width=320, height=240)
display(image_widget)
​x
cv2.startWindowThread()
picam2 = Picamera2()
config = picam2.create_preview_configuration(main={"format": 'RGB888', "size": (640, 480)})
#config["transform"] = libcamera.Transform(hflip=0, vflip=1)
picam2.configure(config)
picam2.start()

frame = picam2.capture_array()
image_widget.value = bgr8_to_jpeg(frame)
xxxxxxxxxx
# Start the process
thread1 = threading.Thread(target=Camera_display)
thread1.daemon = True
thread1.start()
xxxxxxxxxx
# End the process
picam2.stop()
stop_thread(thread1)

After running, we can see the camera display below the display camera component and frame the face and eyes of the person. Note that the light may affect the experimental results.

image-20241216104643656

Note: For CSI cameras, if you want to rerun the program, you need to do the following:

image-20241216104718303

QR code
Color recognition

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文