don't know how to predict classes with RCNN ResNet50 and the dnn method

asked 2020-07-21 07:40:39 -0500

I don't know how to predict classes with the RCNN ResNet50 model. I can successfully predict the bounding boxes but I don't know how to predict the right class.

I've downloaded the pretrained model weights and config files from the OpenCV GitHub Site. Link to the object_detection_classes_coco.txt.

I would be really thankful if somebody could help me or show me in the right direction. If it is not possible that would be also help me further. Thank you very much in advance :)

This is my code:

%matplotlib inline
import cv2 as cv
import matplotlib.pyplot as plt

cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'faster_rcnn_resnet50_coco_2018_01_28.pbtxt')

# Category Names
coco_names = "object_detection_classes_coco.txt"
classes = open(coco_names).read().strip().split("\n")

# define path
img_path = 'img\elephant.jpg'

# read picture
img = cv.imread(img_path, cv.IMREAD_UNCHANGED)
img = cv.cvtColor(img,cv.COLOR_BGR2RGB) 

img = cv.resize(img, (300, 300), interpolation=cv.INTER_AREA)

rows = img.shape[0]
cols = img.shape[1]

blob = cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False)
cvOut = cvNet.forward()

# loop over the number of detected objects
for i in range(0, cvOut.shape[2]):
    # extract the class ID of the detection along with the confidence
    # (i.e., probability) associated with the prediction
    classID = int(cvOut[0, 0, i, 1])
    confidence = cvOut[0, 0, i, 2]   

print("Klasse: {} \n Wahrscheinlichkeit: {}".format(classes[classID],confidence))

for detection in cvOut[0,0,:,:]:
    score = float(detection[2])
    if score > 0.3:
        left = detection[3] * cols
        top = detection[4] * rows
        right = detection[5] * cols
        bottom = detection[6] * rows
        cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)


I've only found this tutorial that is kinda similar but I've tried it out for the faster R-CNN with ResNet50 but it didn't work.

image description

edit retag flag offensive close merge delete


btw, isn't classID == detection[1] ?`

but it didn't work.

we'll shoot you if you say that again ... if you want help, be concise

berak gravatar imageberak ( 2020-07-23 10:38:58 -0500 )edit