我正在执行https://github.com/tensorflow/tensorflow这个检测图像中对象的例子。
我希望得到检测到的对象的数量,这是代码,它为我提供了在图像中绘制的检测到的对象。但是我无法计算检测到的物体。
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=1)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
这是提供实际物体检测的代码块,如下图所示:
如何获取对象数?
答案 0 :(得分:3)
解决它只是打印boxes.shape的长度
print(len(boxes.shape))
答案 1 :(得分:3)
您可以使用 TensorFlow Object Counting API ,它是在TensorFlow之上构建的开源框架,可以轻松开发对象计数系统以计数任何对象!
有关详细信息,请参见TensorFlow Object Counting API,如果发现有用的话,请给星号that回购,以表示对开源社区的支持!
答案 2 :(得分:1)
重要的是要注意盒子的数量总是100个。
如果你查看实际绘制框的代码,即vis_util.visualize_boxes_and_labels_on_image_array
函数,你会发现他们正在定义一个阈值 - min_score_thresh=.5
- 到将绘制的框限制为只有那些分数> 1的检测结果。 0.5。您可以将此视为仅精确检测概率> 50%的绘图框。您可以向上或向下调整此阈值以增加绘制的框数。但是,如果将它降低得太低,则会出现很多不准确的框。
答案 3 :(得分:1)
您应该检查分数并将对象计为手动。 代码在这里:
#code to test image start
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
#code to test image finish
#add this part to count objects
final_score = np.squeeze(scores)
count = 0
for i in range(100):
if scores is None or final_score[i] > 0.5:
count = count + 1
#count is the number of objects detected
答案 4 :(得分:1)
添加此部分以计数对象
final_score = np.squeeze(scores)
count = 0
for i in range(100):
if scores is None or final_score[i] > 0.5:
count = count + 1
count是检测到的对象数
此部分将打印计数,但将以连续方式打印,可以像最终计数=某个值那样仅打印一次,而不必重复打印