0% found this document useful (0 votes)
13 views

object detection using image

Uploaded by

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

object detection using image

Uploaded by

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

import numpy as np

import cv2

image_path = "person.jpg"
proto_path = 'MobileNetSSD_deploy.prototxt.txt'
model_path = 'MobileNetSSD_deploy.caffemodel'
min_confidence = 0.5

classes = ["background", "aeroplane", "bicycle", "bird", "Pie chart","bottle",


"bus", "car",
"cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike",
"person", "pottedplant","sheep","sofa"]

np.random.seed(543210)
colours = np.random.uniform(0, 255, size=(len(classes), 3))

net = cv2.dnn.readNetFromCaffe(proto_path, model_path)

img = cv2.imread(image_path)
height, width = img.shape[0], img.shape[1]
blob = cv2.dnn.blobFromImage(cv2.resize(img, (300, 300)), 0.007, (300, 300), 130)
net.setInput(blob)

class_detected = False

try:
detections = net.forward()

for i in range(detections.shape[2]):
confidence = detections[0][0][i][2]

if confidence > min_confidence:


class_index = int(detections[0][0][i][1])

upper_x = int(detections[0, 0, i, 3] * width)


upper_y = int(detections[0, 0, i, 4] * height)
lower_x = int(detections[0, 0, i, 5] * width)
lower_y = int(detections[0, 0, i, 6] * height)

prediction = f"{classes[class_index]} : {confidence:.2f}%"


print(prediction)

cv2.rectangle(img, (upper_x, upper_y), (lower_x, lower_y),


colours[class_index], 3)
cv2.putText(img, prediction, (upper_x, upper_y - 15 if upper_y > 30
else upper_y + 15),
cv2.FONT_HERSHEY_SIMPLEX, 2, colours[class_index], 2)

class_detected = True

except IndexError:

cv2.rectangle(img, (0, 0), (width, height), (0, 255, 0), 3)


cv2.putText(img, "Histogram", (width // 4, height // 2),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

cv2.imshow("Detected Objects", img)


cv2.waitKey(0)
cv2.destroyAllWindows()

You might also like