1 回答

TA贡献1827条经验 获得超8个赞
我用以下功能解决了这个问题:
i 是一个用于循环的变量,基本上是当前图像的数量
def crop_objects(image, image_np, output_dict, i):
global ymin, ymax, xmin, xmax
width, height = image.size
#Coordinates of detected objects
ymin = int(output_dict['detection_boxes'][0][0]*height)
xmin = int(output_dict['detection_boxes'][0][1]*width)
ymax = int(output_dict['detection_boxes'][0][2]*height)
xmax = int(output_dict['detection_boxes'][0][3]*width)
crop_img = image_np[ymin:ymax, xmin:xmax]
# 1. Only crop objects that are detected with an accuracy above 50%,
# images
# with objects below 50% will be filled with zeros (black image)
# This is something I need in my program
# 2. Only crop the object with the highest score (Object Zero)
if output_dict['detection_scores'][0] < 0.5:
crop_img.fill(0)
#Save cropped object into image
cv2.imwrite('Images/Step_2/' + str(i) + '.png', crop_img)
return ymin, ymax, xmin, xmax
这些是它工作所必需的:
image = Image.open(image_path)
image_np = load_image_into_numpy_array(image)
def load_image_into_numpy_array(image):
#Für Bilderkennung benötigte Funktion
last_axis = -1
dim_to_repeat = 2
repeats = 3
grscale_img_3dims = np.expand_dims(image, last_axis)
training_image = np.repeat(grscale_img_3dims, repeats, dim_to_repeat).astype('uint8')
assert len(training_image.shape) == 3
assert training_image.shape[-1] == 3
return training_image
这可能比仅裁剪对象所需的代码更多。
添加回答
举报