Python API for the MediaPipe Object Detection Solution

Task 1 (GSoC 2021)

Aniket Ray's photo
Aniket Ray
·Aug 19, 2021·

2 min read

This is an extension to Mediapipe Object Detection Solution

Configuration Options

- max_object_detection
Maximum number of objects to detect. Default to 3.
- min_detection_confidence
Minimum confidence value ([0.0, 1.0]) from the object detection model for the detection to be considered successful. Default to 0.6
- min_suppression_threshold
Minimum suppression value ([0.0, 1.0]) from the object detection model for the detection to be considered successful. Default to 0.4

Python API

Please first follow general instructions to install the MediaPipe Python package, then learn more with the usage example below.

Supported configuration options:

max_object_detection
min_detection_confidence
min_suppression_threshold

max_object_detection=5 min_detection_confidence=0.6.gif

Usage

import cv2
import mediapipe as mp

mp_object_detection = mp.solutions.object_detection
mp_drawing = mp.solutions.drawing_utils

# For static images:
IMAGE_FILES = []
with mp_object_detection.ObjectDetection(
    min_detection_confidence=0.5) as object_detection:
  for idx, file in enumerate(IMAGE_FILES):
    image = cv2.imread(file)
    # Convert the BGR image to RGB and process it with MediaPipe Object Detection.
    results = object_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

    # Draw the object detection annotations on the image
    # for each detected object.
    if not results.detections:
      continue
    annotated_image = image.copy()
    for detection in results.detections:
      mp_drawing.draw_detection(annotated_image, detection)
    cv2.imwrite('/tmp/annotated_image' + str(idx) + '.png', annotated_image)

# For webcam input:
cap = cv2.VideoCapture(0)
with mp_object_detection.ObjectDetection(
    max_object_detection=1,
    min_detection_confidence=0.6,
    min_suppression_threshold=0.4) as object_detection:
  while cap.isOpened():
    success, image = cap.read()
    if not success:
      print("Ignoring empty camera frame.")
      # If loading a video, use 'break' instead of 'continue'.
      continue

    # Flip the image horizontally for a later selfie-view display, and convert
    # the BGR image to RGB.
    image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
    # To improve performance, optionally mark the image as not writeable to
    # pass by reference.
    image.flags.writeable = False
    results = object_detection.process(image)

    # Draw the object detection annotations on the image
    # for each detected object.
    image.flags.writeable = True
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    if results.detections:
      for detection in results.detections:
        mp_drawing.draw_detection(image, detection)
    cv2.imshow('MediaPipe Object Detection', image)
    if cv2.waitKey(5) & 0xFF == 27:
      break
cap.release()


GitHub

 
Share this