【问题标题】:How to count objects using tensorflow model?如何使用张量流模型计算对象?
【发布时间】:2021-06-14 19:28:41
【问题描述】:

我想根据检测分数计算检测到的对象。我正在使用 EdjeElectronics 的对象检测代码。这是我一直在使用的代码:

# 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')

这是检测线:

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

并使用它来可视化它:

# 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)

我的问题是,我找到了一个视频,该视频告诉我如何根据分数(信心值)进行计数。但我不太清楚。

视频中的那个人使用这个循环来计算检测到的物体:

# 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

然后用这个在屏幕上打印出来:

# 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)

到目前为止,我无法更新 current_count 值,我猜循环中有问题。所以,我无法计算检测到的对象,因为它总是显示0 值。请帮帮我

【问题讨论】:

  • 也许您的结果都没有超过阈值?你把分数打印出来了吗?
  • 当它检测到物体时,检测到的物体显示出 90% 的置信度。要么我在循环中使用了错误的变量,要么我不知道......
  • 我认为分数是二维的,你看过形状吗? len(detection_scores) 可能会返回 1
  • 我会尝试...抱歉我是新手
  • TypeError: len 对于符号张量没有很好的定义。 (detection_scores:0) 请致电x.shape 而不是len(x) 获取形状信息。它给了我这样的错误

标签: tensorflow opencv machine-learning computer-vision object-detection


【解决方案1】:

试试这个

for box,score,cls in zip(boxes[0],scores[0],classes[0]):
    if score >= 0.6 and score <= 1.0:
        count += 1
       

【讨论】:

  • omg,它成功了......非常感谢......但是,你能解释一下这行的作用吗:for box,score,cls in zip(boxes[0],scores[0 ],类[0]) ?再次,非常感谢你
  • boxes 基本上是为您提供对象边界框坐标值的边界框。 classes 是表示类的整数。可以查看对应的类here。最后scores 表示模型对对象的预测分数。 [0] 仅用于索引每个值,box,score,cls 用于一个对象。你可以试试打印出来看看
  • 好吧...我试过了,它奏效了... zip 呢?那是什么 ?对于每个值,这个循环是否从 0 循环到 1? (框,分数,类)
  • Zip 允许您同时遍历多个列表。在这里,您正在循环三个列表、框、类和分数。恐怕我没听懂你的第二个问题?每次循环,我们得到一个对象的box坐标、class和detection score,分别用box、cls、score表示
  • 好吧好吧……我明白了……谢谢你的解释……
猜你喜欢
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
相关资源
最近更新 更多