opencv dnn tensorflow net.forward shape

asked 2019-08-01 01:10:10 -0500

hwan gravatar image

updated 2019-08-01 11:31:48 -0500

import cv2 as cv import numpy as np

net = cv.dnn.readNetFromTensorflow('./model/tf_model.pb', './model/model.pbtxt')

img = cv.imread('./data/frame63.jpg')

(h, w) = img.shape[:2]
#cvNet.setInput(cv.dnn.blobFromImage(img, size=(32, 32), swapRB=True, crop=False))
net.setInput (cv.dnn.blobFromImage (img, 1.0 / 127.5, (32, 32), (127.5, 127.5, 127.5), swapRB = True, crop = 
False))
detections = net.forward()
print(detections.shape)

for i in range(0, detections.shape[2]):
    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
    startX, startY, endX, endY) = box.astype("int")

    confidence = detections[0, 0, i, 2]

  # If confidence > 0.5, show box around face
    if (confidence > 0.5):
        cv.rectangle(img, (startX, startY), (endX, endY), (255, 255, 255), 2)

cv.imshow('img', img)
cv.waitKey()

Only (1,2) will be called so that the 'IndexError: tuple index out of range' error occurs. Is it a matter of my cnn neural network?

edit retag flag offensive close merge delete

Comments

please edit your post and insert your code as text

LBerger gravatar imageLBerger ( 2019-08-01 05:58:50 -0500 )edit

print(detections.shape) --- and the output is ?

berak gravatar imageberak ( 2019-08-01 14:53:42 -0500 )edit

(1, 2) will be output

hwan gravatar imagehwan ( 2019-08-02 00:45:06 -0500 )edit
1

so, you cannot index it the way you want to.

but the shape looks wrong, maybe you got the wrong output layer(s) ?

(what kind of network is it even, exactly ?)

berak gravatar imageberak ( 2019-08-02 05:44:56 -0500 )edit

I used the vgg16 model

hwan gravatar imagehwan ( 2019-08-03 07:05:47 -0500 )edit