Python

Example source code using OpenCV and the mediapipe library to detect body parts. The source code supports identifying facial landmarks and hand positions with custom labels.

This code creates a graphical application using PyQt5 to detect hands and faces from a live video feed, powered by OpenCV and MediaPipe. The application can recognize the number of raised fingers and display the name of each finger. Additionally, it detects the face and marks the forehead position with a cross.

Main Structure of the Code

  1. Initialize Libraries and Modules:
    • Initializes MediaPipe Hands to detect hands and Face Detection to detect faces with a minimum confidence of 0.7.
    • Specifies the path to a Unicode font to display finger names in Vietnamese.
  2. Main Functions:
    • fingers_up: Identifies which fingers are raised based on the landmark coordinates of each finger.
    • draw_finger_names: Displays the name of each finger on the frame.
    • draw_forehead_cross: Marks the forehead position with a cross on the detected face.
  3. VideoApp Class:
    • Creates a graphical interface to display live video from the camera with two control buttons:
      • Start Tracking: Starts detection and tracking.
      • Stop: Stops the detection process.
    • The update_frame function processes each video frame:
      • Converts the frame for hand and face detection processing.
      • If a hand is detected, draws connections and displays the number of raised fingers along with each finger’s name.
      • If a face is detected, draws a box around the face and a cross on the forehead.
    • add_letterbox: Centers the video with black padding around it.
  4. Run the Application:
    • Initializes and runs the Qt application with a window showing the live video feed from the camera and detection features.

Usage Instructions

  1. Run the Application: Run the code, and the Hand and Face Tracking window will open.
  2. Start Tracking: Press the Start Tracking button to enable the camera and start detection.
  3. View Results:
    • The number of raised fingers and the name of each finger will be displayed on the screen if a hand is detected.
    • If a face is detected, the application will draw a box around it and mark the forehead position with a cross.
  4. Stop Tracking: Press the Stop button to stop the camera and clear the display.