使用Tensorflow对象检测API提取边界框内的图像



下面是我试图更改的Tensorflow Object Detection API示例中的一个片段。

我试图提取包围在边界框中的图像,但无法使用Tensorflow Detection API进行提取。请提出建议。


while True:
frame = cv2.imread(cap)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
expanded_frame = np.expand_dims(frame, axis=0)
(boxes, scores, classes, num_c) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: expanded_frame})
# Visualization of the detection
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=1,
min_score_thresh=0.40)
cv2.imshow('Detection', frame)
filename = r'C:UsersuserDesktopNew folderFace DetectedDetected.png'
cv2.imwrite(filename,frame)  
fps.update()

if cv2.waitKey(1) == ord('q'):
fps.stop()
break
print("Fps: {:.2f}".format(fps.fps()))
fps.update()
cap.stop()
cv2.destroyAllWindows()

您可以使用来确定盒子的尺寸

true_boxes = boxes[0][scores[0] > min_score_thresh]
for i in range(true_boxes.shape[0]):
ymin = true_boxes[i,0]*height
xmin = true_boxes[i,1]*width
ymax = true_boxes[i,2]*height
xmax = true_boxes[i,3]*width
print ("Top left")
print (xmin,ymin,)
print ("Bottom right")
print (xmax,ymax)

并且可以使用裁剪图像

cropped_img = image.crop((x_min, y_min, x_max, y_max))

有关更多详细信息,请参阅此链接。非常感谢。