Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use readNet (or readFromDarknet) instead of readNetFromCaffe?

I did an object detection using opencv by loading pre-trained MobileNet SSD model. from this post. It reads a video and detects objects without any problem. But I would like to use readNet (or readFromDarknet) instead of readNetFromCaffe

net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

because I have pre-trained weights and cfg file of my own objects only in Darknet framework. Therefore I simply changed readNetFromCaffe into readNet in above post and got an error:

Traceback (most recent call last):
  File "people_counter.py", line 124, in <module>
    for i in np.arange(0, detections.shape[2]):
IndexError: tuple index out of range

Here detections is an output from

blob = cv2.dnn.blobFromImage(frame, 1.0/255.0, (416, 416), True, crop=False)
net.setInput(blob)
detections = net.forward()

Its shape is (1, 1, 100, 7) tuple (when using readNetFromCaffe).

I was kinda expecting it wouldn't work just by changing the model. Then I decided to look for an object detector code where readNet was used and I found it here. I read through the code and found the same lines as follows:

blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)
net.setInput(blob)
outs = net.forward(get_output_layers(net))

Here, the shape of outs is (1, 845, 6) list. But in order for me to be able to use it right away (here), outs should be of the same size with detections. I've come up to this part and have no clue about how I should proceed.

If something isn't clear, I just need help to use readNet (or readFromDarknet) instead of readNetFromCaffe in this post