Ask Your Question

Revision history [back]

Detecting Certain Classes with OpenCV DNN

Hello all, I was recently working on an object detection project in which I had to detect and localize certain objects in a frame. However, I was only able to use the OpenCV example on the Tensorflow Object Detection github in order to draw yellow boxes around each identified object. However, I want to print the name of the object that I have detected. I wanted to know how to do this. Also, I have only written code to detect and localize the image in one frame, how would I do so in order to make it process a video stream? Sorry for asking so many questions but, my last question is that, all my code is written in Python, how would I write all of the similar code in C++?

As aforementioned: Code to Detect: https://pastebin.com/LsYHZ89W PB file, Pbtxt, model config: https://drive.google.com/drive/folders/19g7xnC9ekjUHeFNtgcvSn2J1ioWmrb_L?usp=sharing

Detecting Certain Classes with OpenCV DNN

Hello all, I was recently working on an object detection project in which I had to detect and localize certain objects in a frame. However, I was only able to use the OpenCV example on the Tensorflow Object Detection github in order to draw yellow boxes around each identified object. However, I want to print the name of the object that I have detected. I wanted to know how to do this. Also, I have only written code to detect and localize the image in one frame, how would I do so in order to make it process a video stream? Sorry for asking so many questions but, my last question is that, all my code is written in Python, how would I write all of the similar code in C++?

As aforementioned: Code to Detect: https://pastebin.com/LsYHZ89W aforementioned:

import cv2 as cv
from collections import defaultdict
cvNet = cv.dnn.readNetFromTensorflow('/Users/User/Desktop/Robotics/Skystone-Vision/Final/image_tensor/PB_file/model.pb', '/Users/User/Desktop/Robotics/Skystone-Vision/Final/image_tensor/Pbtxt/model.pbtxt')

img = cv.imread('/Users/User/Desktop/TestStuff/work.jpeg')
rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()
numOfRect = 0
objects_dict = {}
for detection in cvOut[0,0,:,:]:
    score = float(detection[2])
    objects = detection
    if score > 0.75:
        left = detection[3] * cols
        top = detection[4] * rows
        right = detection[5] * cols
        bottom = detection[6] * rows
        objects_dict[score]=[left,top,right,bottom]
        numOfRect += 1

        print(score)
#print(objects_dict)
l=list(objects_dict.keys())
l.sort(reverse=True)
print(l)
for i in l[:6] :
    print(i,objects_dict[i])
    cv.rectangle(img, (int(objects_dict[i][0]), int(objects_dict[i][1])), (int(objects_dict[i][2]), int(objects_dict[i][3])), (23, 230, 210), thickness=5)

cv.imshow('img', img)
print(numOfRect)
print(objects)
cv.waitKey()

PB file, Pbtxt, model config: https://drive.google.com/drive/folders/19g7xnC9ekjUHeFNtgcvSn2J1ioWmrb_L?usp=sharing