Detecting Certain Classes with OpenCV DNN

asked 2019-12-23 02:50:54 -0600

aditya_mangalampalli gravatar image

updated 2019-12-26 09:07:59 -0600

supra56 gravatar image

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:

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/folder...

edit retag flag offensive close merge delete

Comments

what kind of model do you try to use ? does it have the required object classes ? do you need to retrain it on your own ?

please put at least your code here, not on some pastebin, where it will expire (also, pastebin.com is not reachable from cn or tr)

berak gravatar imageberak ( 2019-12-23 02:56:34 -0600 )edit

@berak My apologies, I did not know that pastebin was not accessible. I will assume that the PB fille, the PBtxt file, and the model config are accessible. The code that I am using is however rather long and it seems to be exceeding the character limit. If the Google Drive files are accessible the code should be accessible too. The name of the file is DetectRightObj.py. The link for the code: along with the rest of the files. Here

aditya_mangalampalli gravatar imageaditya_mangalampalli ( 2019-12-23 11:44:06 -0600 )edit

@berak The forum seems to be adding the text that I have typed along with the code for a total characted count. The code itself is rather close to 40 lines.

aditya_mangalampalli gravatar imageaditya_mangalampalli ( 2019-12-23 12:23:25 -0600 )edit

However, I want to print the name of the object that I have detected.

you need a list of classnames, or at least the name of the dataset it was trained on (and take it from there)

berak gravatar imageberak ( 2019-12-26 10:00:51 -0600 )edit