0% found this document useful (0 votes)
43 views4 pages

Object Detection with OpenCV

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Object Detection with OpenCV

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXP:4 OBJECT DETECTION AND RECOGNITION

AIM : To develop a program to implement Object Detection and Recognition

ALGORITHM:

Step 1: Import Necessary Libraries


● Import the required libraries for image processing, object detection,
and visualization.

Step 2: Define Constants and Functions


● Define any constants needed for visualization (e.g., margin, font size)
and functions to perform tasks such as visualizing detection results.

Step 3: Load Image and Model


● Load the input image using OpenCV.
● Create an ObjectDetector object using MediaPipe.
● Specify options for the ObjectDetector, such as the model path and
score threshold.

Step 4: Detect Objects in the Image


● Convert the loaded image to a format compatible with MediaPipe.
● Use the ObjectDetector to detect objects in the image.
● Retrieve the detection results.

Step 5: Visualize Detection Results


● Create a copy of the input image for visualization purposes.
● Iterate over the detection results.
● Draw bounding boxes around detected objects on the image.
● Display labels and scores for each detected object.
● Convert the annotated image from BGR to RGB format (if necessary).
● Display the annotated image using a suitable method (e.g., OpenCV's
imshow).
PROGRAM :
import cv2
import numpy as np
import mediapipe as mp
from [Link] import python
from [Link] import vision

MARGIN = 10 # pixels
ROW_SIZE = 10 # pixels
FONT_SIZE = 1
FONT_THICKNESS = 1
TEXT_COLOR = (255, 0, 0) # red

def visualize(
image,
detection_result
) -> [Link]:
"""Draws bounding boxes on the input image and return it.
Args:
image: The input RGB image.
detection_result: The list of all "Detection" entities to be visualize.
Returns:
Image with bounding boxes.
"""
for detection in detection_result.detections:
# Draw bounding_box
bbox = detection.bounding_box
start_point = bbox.origin_x, bbox.origin_y
end_point = bbox.origin_x + [Link], bbox.origin_y + [Link]
[Link](image, start_point, end_point, TEXT_COLOR, 3)

# Draw label and score


category = [Link][0]
category_name = category.category_name
probability = round([Link], 2)
result_text = category_name + ' (' + str(probability) + ')'
text_location = (MARGIN + bbox.origin_x,
MARGIN + ROW_SIZE + bbox.origin_y)
[Link](image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,
FONT_SIZE, TEXT_COLOR, FONT_THICKNESS)

return image

IMAGE_FILE = r'C:\Users\Administrator\Documents\[Link]'

img = [Link](IMAGE_FILE)
# STEP 2: Create an ObjectDetector object.
base_options =
[Link](model_asset_path=r'C:\Users\Administrator\Downloads\
efficientdet_lite0.tflite')
options = [Link](base_options=base_options,
score_threshold=0.3)
detector = [Link].create_from_options(options)

# STEP 3: Load the input image.


image = [Link].create_from_file(IMAGE_FILE)

# STEP 4: Detect objects in the input image.


detection_result = [Link](image)

# STEP 5: Process the detection result. In this case, visualize it.


image_copy = [Link](image.numpy_view())
annotated_image = visualize(image_copy, detection_result)
rgb_annotated_image = [Link](annotated_image, cv2.COLOR_BGR2RGB)
[Link]('image',rgb_annotated_image)
OUTPUT:

RESULT:
Thus the program to implement Object Detection and Recognition is executed
successfully and output is verified.

You might also like