yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
IMX 415 Camera Module📖
1. Jetson Orin Nano‑NX Usage Course
  • 1. Camera Driver Course
  • 2.1 Advanced AI Vision-Traffic Detection—DeepStream environment
  • 2.2 Advanced AI Vision-Traffic Detection—Four types of detection(traffic)
  • 3. YOLO11 Object Detection
  • 4. YOLO26 Object Detection
  • 5. Mediapipe Environment
  • 6. Mediapipe Hand Detection
  • 7. MediaPipe Face Detection
  • 8. MediaPipe Face Effects
  • 9. MediaPipe 3D Object Recognition
  • 10. MediaPipe Virtual Brush
  • 11. MediaPipe Gesture Recognition
  • 12. MediaPipe Finger Control
  • 13. MediaPipe Posture Estimation
  • 14. MediaPipe Face Mask
  • 15. MediaPipe Overall Detection
  • 16. MediaPipe Green Scene Rremoval(Human)
  • 17. QR code recognition
2. Raspberry Pi 5 Usage Course
  • 1. Camera Driver Course
  • 2. Advanced AI Vision-Human Pose Estimation
  • 3. Target Detection
  • 4. QR Code
  • 5. Face Detection
  • 6. Color Recognition
  • 7. YOLO11 Object Detection
  • 8. YOLO26 Object Detection
  • 9. MediaPipe Face Effects
  • 10. MediaPipe Face Detection
  • 11. MediaPipe 3D Object Recognition
  • 12. MediaPipe Virtual Brush
  • 13. MediaPipe Finger Control
  • 14. MediaPipe Gesture Recognition
3. RDK X5 Usage Course
  • 1. Camera Driver Course
  • 2. Target Detection Algorithm-FCOS
  • 3. Target Detection Algorithm-YOLOv5s
  • 4. YOLO26 Object Detection
4. Camera Bracket Installation (Optional Video)
  • Installation Video
Download
  • Code
  • SystemFile
  • 3D_Model_File
  • Hardware_Info
Download ZIP

Welcome to IMX 415 Camera Module repository

5. Face Detection

Face Detection

Face Detection

File path: /home/pi/Yahboom_Project/1.OpenCV course/04 advanced tutorial/face_detaction.ipynb

The test code is as follows:

​x
#bgr8 to jpeg format
​
​
import enum
​
​
import cv2
​
​
 
​
​
​
def bgr8_to_jpeg(value, quality=75):
​
​
    return bytes(cv2.imencode('.jpg', value)[1])
#Camera display component
​
​
import cv2
​
​
import ipywidgets.widgets as widgets
​
​
import threading
​
​
import time
​
​
import sys
​
​
  
​
​
​
image_widget = widgets.Image(format='jpeg', width=320, height=240)
​
​
display(image_widget)
image = cv2.VideoCapture(0)
​
​
image.set(3,320)
​
​
image.set(4,240)
​
​
ret, frame = image.read()
​
​
image_widget.value = bgr8_to_jpeg(frame)
# Thread function operation library
​
​
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)
# body_haar = cv2.CascadeClassifier("haarcascade_upperbody.xml")
​
​
# face_haar = cv2.CascadeClassifier("haarcascade_profileface.xml")
​
​
face_haar = cv2.CascadeClassifier("haarcascade_fullbody.xml")
​
​
#eye_haar = cv2.CascadeClassifier("haarcascade_eye.xml")  
​
​
eye_haar = cv2.CascadeClassifier("haarcascade_eye_tree_eyeglasses.xml")  
​
​
def Camera_display():
​
​
    while 1:
​
​
        ret, frame = image.read()
​
​
        # Convert the image to a grayscale image
​
​
        gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
​
​
#         # Detect all pedestrians in the image
​
​
#      bodies = body_haar.detectMultiScale(gray_img, 1.3, 5)
​
​
#      for body_x,body_y,body_w,body_h in bodies:
​
​
#      cv2.rectangle(frame, (body_x, body_y), (body_x+body_w, body_y+body_h), (0,255,0), 2)
​
​
  
​
​
​
        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)
​
​
        
​
​
#         eyes = eye_haar.detectMultiScale(gray_img, 1.3, 5)
​
​
#         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)
#Start the thread
​
​
thread1 = threading.Thread(target=Camera_display)
​
​
thread1.setDaemon(True)
​
​
thread1.start()
#End the process, release the camera, execute when it needs to end
​
​
stop_thread(thread1)
​
​
image.release()

After running, we can see the camera display frame below the camera component, with the person's face and eyes outlined. Note that lighting may affect the experimental results.

img

For the CSI camera, code path: /home/pi/Yahboom_Project/1.OpenCV course/04 advanced tutorial/face_detection_CSI.ipynb

Note: For the CSI camera, if you want to re-run the program, you need to do the following:

img

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
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文