draw detections when blobFromImages is used
Hello,
I was testing OpenCV face detection using a pre-trained model:
(h, w) = image.shape[:2]
net = cv2.dnn.readNetFromCaffe("deploy.prototxt.txt", "res10_300x300_ssd_iter_140000_fp16.caffemodel")
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), [104., 117., 123.], False, False)
net.setInput(blob)
detections = net.forward()
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.7:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
This example is working ok. But I don't now how to modify the code above in order to draw the detections if two images are used instead of only one:
blob2 = cv2.dnn.blobFromImages(images, 1.0, (300, 300), [104., 117., 123.], False, False)
net.setInput(blob2)
detections = net.forward()
How to draw the detections?
Thanks in advanced
there is something weird here.
if we use
cv2.dnn.blobFromImages([im1, im2, im3],...)
, we get e.g. a detection.shape of[1,1,27,7]
, in other words, all detections end up in the same batchnum / channel.and, although all images get resized to the same size, the number of detections per image differs, depending on the image (size ?), which makes it impossible to seperate them per image with multiple ones.
then, it also crashes with the tensorflow uint8 model:
ok; yes, it was the problem, I didn't know how to separate the detections per image. Thanks @berak.
wait, we still have to solve it ;(
imho, there's something wrong with the detection_out layer.hmm, the PriorBox layers all look like this:
(2 inputs with batchsize 3, but the output has batchsize 1 only)
i also tried the MobileNetSSD_deploy model, same problem.@berak, There is a specification of output for detection_out layers:
[batchId, classId, confidence, left, top, right, bottom]
so we need to check[0]
element to split output per samples. But, as mentioned, the problem is how to manage number of detections. For different batch size we always get1x1xNx7
where N is a number of detections (by default 200). To increase it, we can modify.prototxt
:keep_top_k: 200
. It's just a guess. Please let me check it.@dkurt, ah ! indeed the batch id is the 1st element of each row !