Ask Your Question

Revision history [back]

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

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)
cvNet.setInput(blob)
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)

plt.figure(1)
plt.imshow(img)

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