通过通过TensorFlow对象检测API2推断获得的边界框循环循环
我已经将TensorFlow对象检测API2用于模型,现在我想循环遍历单个图像中的多个边界框,并为每个边界框创建一个单独的图像。 我使用过的代码,我能够获得一个边界框的图像,但无法循环浏览,我认为我误解了检测文件的内容。
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS)
image_np = np.array(Image.open(test_image_path))
print('Running inference for {}... '.format(test_image_path), end='')
input_tensor = tf.convert_to_tensor(image_np)
input_tensor = input_tensor[tf.newaxis, ...]
#input_tensor = input_tensor[:, :, :, :3]
#input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes'],
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=500,
min_score_thresh=.2,
agnostic_mode=False)
plt.figure()
plt.imshow(image_np_with_detections)
print('Done')
plt.show()
def crop_objects(image, image_np_with_detections, detections):
global ymin, ymax, xmin, xmax
width, height = image.size
#Coordinates of detected objects
ymin = int(detections['detection_boxes'][0][0]*height)
xmin = int(detections['detection_boxes'][0][1]*width)
ymax = int(detections['detection_boxes'][0][2]*height)
xmax = int(detections['detection_boxes'][0][3]*width)
crop_img = image_np_with_detections[ymin:ymax, xmin:xmax]
if detections['detection_scores'][0] < 0.5:
crop_img.fill(0)
#Save cropped object into image
cv2.imwrite('D:\\pcb_project\\test_images' + '.png', crop_img)
return ymin, ymax, xmin, xmax
I have used the tensorflow object detection api2 for a model , now I would want to loop through multiple bounding boxes in a single image and create a separate image for each bounding box.
The code I have used, with which I am able to get a image of one bounding box but unable to loop through, I think I have misunderstood the contents of the detections file.
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS)
image_np = np.array(Image.open(test_image_path))
print('Running inference for {}... '.format(test_image_path), end='')
input_tensor = tf.convert_to_tensor(image_np)
input_tensor = input_tensor[tf.newaxis, ...]
#input_tensor = input_tensor[:, :, :, :3]
#input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes'],
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=500,
min_score_thresh=.2,
agnostic_mode=False)
plt.figure()
plt.imshow(image_np_with_detections)
print('Done')
plt.show()
def crop_objects(image, image_np_with_detections, detections):
global ymin, ymax, xmin, xmax
width, height = image.size
#Coordinates of detected objects
ymin = int(detections['detection_boxes'][0][0]*height)
xmin = int(detections['detection_boxes'][0][1]*width)
ymax = int(detections['detection_boxes'][0][2]*height)
xmax = int(detections['detection_boxes'][0][3]*width)
crop_img = image_np_with_detections[ymin:ymax, xmin:xmax]
if detections['detection_scores'][0] < 0.5:
crop_img.fill(0)
#Save cropped object into image
cv2.imwrite('D:\\pcb_project\\test_images' + '.png', crop_img)
return ymin, ymax, xmin, xmax
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是查看了检测文件。从那里很简单。对于任何想要它的人,这对我有用。
Just looked into the detections file. From there it was simple. For anyone else who wants it, this is working for me.
该功能对我不起作用。但是,该代码正在完成这项工作:
The function doesn't work for me. This code is doing the job though:
如果检测['dentection_scores'] [i]&lt,为什么比
的代码少于
的代码; distion_threshold:
?如果我正确,那应该是更大的,即Why is it less than in the code of
if detections['detection_scores'][i] < detection_threshold:
? If I am correct it should be greater-equals to, i.e.