# Python API for the MediaPipe Object Detection Solution

<sup> *This is an extension to [Mediapipe Object Detection](https://google.github.io/mediapipe/solutions/object_detection.html) Solution* </sup>

#### Configuration Options

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

-----
#### Python API
Please first follow general [instructions](https://google.github.io/mediapipe/getting_started/python.html) to install the MediaPipe Python package, then learn more with the usage example below.

Supported configuration options:

<sup>max_object_detection</sup><br>
<sup>min_detection_confidence</sup><br>
<sup>min_suppression_threshold</sup><br>


![max_object_detection=5 min_detection_confidence=0.6.gif](https://firebasestorage.googleapis.com/v0/b/bionry-website.appspot.com/o/max_object_detection%3D5%20min_detection_confidence%3D0.6.gif?alt=media&token=cdc6658a-bfc9-4c5a-834e-776ad55ab07f)

#### Usage

```python
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()

```
</sup>

----
#### GitHub

* [Repository File](https://github.com/aniketiq/mediapipe/blob/ObjectDetectionSolution/mediapipe/python/solutions/object_detection.py)
* [Final Commit](https://github.com/aniketiq/mediapipe/commit/16e448cea9b69b0e22de03b47abdc598dcce0817#diff-994ed2fb45ffe8bdc46e9e7550f03ab150bf71589075895fe75d942f3c73c57f)
