2 Replies - 381 Views - Last Post: 29 October 2020 - 03:15 AM Rate Topic: -----

#1 wantsToCode   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 25-February 19

For-loop question (numpy array)

Posted 28 October 2020 - 05:42 AM

I have a problem that might be a simple one, but there's a part in my code I find difficult to understand.

for subdir, dirs, files in os.walk(directory):
    for file in files:
        filepath = subdir + os.sep + file
        image = cv2.imread(filepath)
        (h, w) = image.shape[:2]
        blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)

        # pass the blob through the neural network
        net.setInput(blob)
        detections = net.forward()

        # loop over the detections
        for i in np.arange(0, detections.shape[2]):
            # extract the confidence (i.e., the probability) associated with the prediction
            confidence = detections[0, 0, i, 2]

            if confidence > 0.2:
                idx = int(detections[0, 0, i, 1])
                box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                (startX, startY, endX, endY) = box.astype('int')

                label = '{}: {:.2f}%'.format(CLASSES[idx], confidence * 100)
                print(label)
                label2 = "{}".format(CLASSES[idx])
                cv2.rectangle(image, (startX, startY), (endX, endY), COLORS[idx], 2)
                y = startY - 15 if startY - 15 > 15 else startY + 15
                cv2.putText(image, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
                if label2 == 'person':
                    cv2.imwrite('output/persons/pic' + str(number) + '.jpg', image)
                    number += 1 



Now the script behaves in unwanted way. If the picture has for example five persons, it saves the same pic five times. If I'm right it's this part that is causing the problem because it loops all the detections from the picture:

        # loop over the detections
        for i in np.arange(0, detections.shape[2]):
            # extract the confidence (i.e., the probability) associated with the prediction
            confidence = detections[0, 0, i, 2]


I want to save the picture if there is a person/persons in it and only once. I don't know how to modify the script because that i from for i in np.arange(0, detections.shape[2]): is used for example in the confidence and idx values also. So just moving stuff out of that loop doesn't work. Any suggestions and help is warmly welcome.

Is This A Good Question/Topic? 0
  • +

Replies To: For-loop question (numpy array)

#2 Ornstein   User is offline

  • D.I.C Regular

Reputation: 120
  • View blog
  • Posts: 277
  • Joined: 13-May 15

Re: For-loop question (numpy array)

Posted 29 October 2020 - 02:14 AM

If I'm understanding you right, you just want to use the loop & conditional to set some (e.g. boolean) value if there's a viable detection - and then you can just check that value after the loop and save the image if it's true?
Was This Post Helpful? 1
  • +
  • -

#3 wantsToCode   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 25-February 19

Re: For-loop question (numpy array)

Posted 29 October 2020 - 03:15 AM

View PostOrnstein, on 29 October 2020 - 02:14 AM, said:

If I'm understanding you right, you just want to use the loop & conditional to set some (e.g. boolean) value if there's a viable detection - and then you can just check that value after the loop and save the image if it's true?


I was thinking that too, but the problem is that I don't understand how to do it. for i in np.arange(0, detections.shape[2]): loops over the detections, so "i" is the detected person in image. But if i save the image inside that loop, it saves from an image with three persons three different images where the first image has one box, second two and the third has all the three boxes, one for each person detected. But I only want to save one image with all the detections, so basically the last one from those three images.
if (len(detections) > 0): actually gives me what I want because it tells me if there is detections in a pic or not. But I cant save the image outside that loop because I define the label inside it and can't use it from outside of it like this:


if label == 'person':
    cv2.imwrite('output/pic' + str(number) + '.jpg', frame)
    number += 1



So, I need the label to get only the right images, but I only want one image with all the detections from it.

This post has been edited by wantsToCode: 29 October 2020 - 03:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1