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 mediapipe.tasks import python
from mediapipe.tasks.python 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
) -> np.ndarray:
"""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 + bbox.width, bbox.origin_y + bbox.height
cv2.rectangle(image, start_point, end_point, TEXT_COLOR, 3)
# Draw label and score
category = detection.categories[0]
category_name = category.category_name
probability = round(category.score, 2)
result_text = category_name + ' (' + str(probability) + ')'
text_location = (MARGIN + bbox.origin_x,
MARGIN + ROW_SIZE + bbox.origin_y)
cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,
FONT_SIZE, TEXT_COLOR, FONT_THICKNESS)
return image
IMAGE_FILE = r'C:\Users\Administrator\Documents\obj1.jpeg'
img = cv2.imread(IMAGE_FILE)
# STEP 2: Create an ObjectDetector object.
base_options =
python.BaseOptions(model_asset_path=r'C:\Users\Administrator\Downloads\
efficientdet_lite0.tflite')
options = vision.ObjectDetectorOptions(base_options=base_options,
score_threshold=0.3)
detector = vision.ObjectDetector.create_from_options(options)
# STEP 3: Load the input image.
image = mp.Image.create_from_file(IMAGE_FILE)
# STEP 4: Detect objects in the input image.
detection_result = detector.detect(image)
# STEP 5: Process the detection result. In this case, visualize it.
image_copy = np.copy(image.numpy_view())
annotated_image = visualize(image_copy, detection_result)
rgb_annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
cv2.imshow('image',rgb_annotated_image)
OUTPUT:
RESULT:
Thus the program to implement Object Detection and Recognition is executed
successfully and output is verified.