lundi 23 mars 2020

Count elements in ndarray in combination with loop and if clause

I'm building a face detection script for images in Python using opencv based on the following blog article: https://www.pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/

The script works well and now I'd like to add a face counter to obtain the number of face detections per image. I've tried it with a simple count and with numpy.count_nonzero, but I'm lacking some prerequisite skills to get the code right.

Here's my code (trying to spare you the irrelevant parts):

...

ap.add_argument("-c", "--confidence", type=float, default=0.5,
    help="minimum probability to filter weak detections")
args = vars(ap.parse_args())

(this is only to explain args["confidence"] down below)

...

net.setInput(blob)
detections = net.forward()
count = 0

(input image is loaded and converted to a blob which is passed through the network to obtain the face detections (type = ndarray))

for i in range(0, detections.shape[2]):
    confidence = detections[0, 0, i, 2]

    if confidence > args["confidence"]:
        count += 1

print("[INFO]",count, "faces detected")

As an alternative I tried box_counter = np.count_nonzero(box) in the if-clause, but that didn't work either.

Work highly appreciate any suggestions for improvement!

Thanks

Aucun commentaire:

Enregistrer un commentaire