Face Detection

免费增值
Verified
通过 inferdo | 已更新 21 days ago | Visual Recognition
人气

9.6 / 10

延迟

1,877ms

服务等级

86%

Health Check

N/A

返回所有讨论

Set Bounding box coordinates Python

Rapid account: Thedevelopermoses

#Use the below python code snippet to convert this API’S weird bounding box coordinates and use pillow to draw a rectangle on the face using the converted coordinates.

from PIL import Image, ImageDraw

def calculate_new_bounding_box(start_x, start_y, end_x, end_y):
old_width = end_x - start_x
old_height = end_y - start_y
new_left = start_x + (old_width - (old_width * 0.78)) / 2
new_top = start_y + (old_height - (old_height * 0.83)) / 2
new_right = end_x - (old_width - (old_width * 0.78)) / 2
new_bottom = end_y - (old_height - (old_height * 0.83)) / 2
return [new_left, new_top, new_right, new_bottom]

def bounding_box_coords(bounding_box_dict, image_width, image_height):
start_x = int(bounding_box_dict[‘startX’])
start_y = int(bounding_box_dict[‘startY’])
end_x = int(bounding_box_dict[‘endX’])
end_y = int(bounding_box_dict[‘endY’])
width = end_x - start_x
height = end_y - start_y
return calculate_new_bounding_box(start_x, start_y, end_x, end_y)

def draw_rectangle(image_path, output_path, bounding_box_dict):
# Open the image
image = Image.open(image_path)
image_width, image_height = image.size
bounding_box = bounding_box_coords(bounding_box_dict, image_width, image_height)
# Create a drawing object
draw = ImageDraw.Draw(image)
# Draw the rectangle outline
draw.rectangle(bounding_box, outline=“green”, width=3)
# Save the modified image
image.save(output_path)

加入讨论 - 在下面添加评论:

登录/注册以发布新的评论