Face Detection

FREEMIUM
Verified
Por inferdo | Atualizado vor 17 Tagen | Visual Recognition
Popularidade

9.6 / 10

Latência

2,017ms

Nível de serviço

100%

Health Check

N/A

Voltar para todas as discussões

Set Bounding box coordinates Python

Rapid account: Thedevelopermoses
thedevelopermoses
vor 10 Monaten

#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)

Junte-se à discussão - adicione o comentário abaixo:

Efetue login / inscreva-se para postar novos comentários