0

i want to count detected objects based on detection scores. I am using EdjeElectronics's Object detection code. And here's the code i've been using:

# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Output tensors are the detection boxes, scores, and classes
# Each box represents a part of the image where a particular object was detected
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represents level of confidence for each of the objects.
# The score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

This is the detection lines:

(boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: frame_expanded})

And visualizing it using this:

# Draw the results of the detection (aka 'visulaize the results')
    vis_util.visualize_boxes_and_labels_on_image_array(
        frame,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.60)

My problem is, i found a video and that video tells me how to count based on scores (value of confidence). But i can't quite figure it out.

The guy in the video using this loop to count the detected objects:

# Loop over all detections and draw detection box if confidence is above minimum threshold
    for i in range (len(scores)):
        if ((scores[i] > 0.6).all() and (scores[i] <= 1.0).all()):

            current_count+=1

And print it on screen using this:

# Draw framerate, current count, and total count in corner of frame
    cv2.putText (frame,'Detections In Frame: ' + str(current_count),(30,75),cv2.FONT_HERSHEY_SIMPLEX,1,(98,189,184),2,cv2.LINE_AA)

So far, i can't update the current_count value, i'm guessing there's something wrong in the loop. So, i can't count the detected objects because it always showed 0 value. Please help me

7
  • Maybe none of your results are above the threshold? Have you printed the scores? Commented Mar 17, 2021 at 12:54
  • When it detect objects, the detected objects show 90% confidence. Either i'm using wrong variable at loop or i don't know... Commented Mar 17, 2021 at 13:01
  • I think the scores come in 2D, have you looked at the shape? len(detection_scores) might return 1 Commented Mar 17, 2021 at 13:45
  • i'll try... sorry i'm new on this Commented Mar 17, 2021 at 14:17
  • TypeError: len is not well defined for symbolic Tensors. (detection_scores:0) Please call x.shape rather than len(x) for shape information. it give me error like this Commented Mar 18, 2021 at 4:23

1 Answer 1

2

Try this

for box,score,cls in zip(boxes[0],scores[0],classes[0]):
    if score >= 0.6 and score <= 1.0:
        count += 1
       
Sign up to request clarification or add additional context in comments.

5 Comments

omg, it worked... thank you so much.... but, can you explain what this line do : for box,score,cls in zip(boxes[0],scores[0],classes[0]) ? again, thank you so much
boxes basically are the bounding boxes that give you the value of the bounding box coordinates for the object. classes are integers that represents the class. You can check the corresponding class here. And finally scores represents the prediction scores by the model for the objects. The [0] is just used for indexing Each value, box,score,cls is for one object. You can try printing them out and having a look
okay... i tried and it worked... what about zip ? what is that ? and is this loop is looping from 0 to 1 for every value ? (boxes, scores, classes)
Zip allows you to loop through multiple lists at the same time. Here you are looping through three lists, boxes, classes and scores. I am afraid I didn’t understand your second question? Each time we loop through, we get the box coordinates, class and detection score for one object that is represented by box, cls,score respectively
okay okay... i understand... thank you for your explanation...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.