问题描述
如何提取由 Tensorflow 对象检测模型生成的对象、对象类别、图像中检测到的对象 id 的输出分数?
How can I extract the output scores for objects , object class ,object id detected in images , generated by the Tensorflow Model for Object Detection ?
我想将所有这些详细信息存储到单独的变量中,以便以后可以将它们存储在数据库中.
I want to store all these details into individual variables so that later they can be stored in a database .
使用与此链接中相同的代码https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
Using the same code as found in this linkhttps://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
请帮我解决这个问题.
我试过了
print(str(output_dict['detection_classes'][0]), ":", str(output_dict['detection_scores'][0]))
print(str(output_dict['detection_classes'][0] ) , ":" , str(output_dict['detection_scores'][0]))
这有效并给出具有最高概率的类的对象 id 和分数.但我也想提取类名以及图像中存在的所有对象的分数、ID 和名称
This works and gives the object id and score for the class with the highest probability . But I want to extract the class name too and also the scores , Ids and names for all objects present in the image
输出示例:图片中有两只狗.当我打印出结果时,我得到了概率最高的对象的 id 和分数 [在这种情况下为 94%] 我也想打印对象名称以及图像中所有其他对象的类似详细信息
推荐答案
您可能需要一些有关 tensorflow 对象检测的知识背景,这里的简短快速解决方案可能正是您所期望的方式:
You may need some knowledge background about tensorflow object detection, short and quick solution here might be the way you expected :
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)
image_np = load_image_into_numpy_array(image)
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
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=8)
objects = []
threshold = 0.5 # in order to get higher percentages you need to lower this number; usually at 0.01 you get 100% predicted objects
for index, value in enumerate(classes[0]):
object_dict = {}
if scores[0, index] > threshold:
object_dict[(category_index.get(value)).get('name').encode('utf8')] = \
scores[0, index]
objects.append(object_dict)
print (objects)
print(len(np.where(scores[0] > threshold)[0])/num_detections[0])
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
希望这有帮助.
这篇关于使用 TensorFlow 对象检测输出分数、类别和 ID 提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!