Spaces:
Configuration error
Configuration error
import pandas as pd | |
from fuzzywuzzy import process | |
import json | |
######################## | |
#this is the first part of the process | |
# the function gonna take result data : and return json data | |
def create_annotations_from_results(results): | |
# Initialize dictionaries to store encountered object names and their counts | |
encountered_names = {} | |
encountered_ids = set() | |
# Initialize a list to store the annotations | |
annotations = [] | |
# Iterate over the predictions and create annotations | |
for result in results: | |
for box in result.boxes: | |
# Extracting bounding box coordinates and rounding them | |
cords = [round(coord) for coord in box.xyxy[0].tolist()] | |
# Extracting class ID and confidence score | |
name_object = result.names[box.cls[0].item()] | |
conf = round(box.conf[0].item(), 2) | |
# Extracting tracked ID | |
id_track = int(box.id) | |
# Skip if ID is encountered | |
if id_track in encountered_ids: | |
continue | |
else: | |
encountered_ids.add(id_track) | |
# Update encountered names and counts | |
if name_object in encountered_names: | |
count = encountered_names[name_object] + 1 | |
encountered_names[name_object] = count | |
name_object_with_count = f"{name_object}_{count}" | |
else: | |
count = 1 | |
encountered_names[name_object] = 1 | |
name_object_with_count = name_object | |
# Calculate bounding box center coordinates | |
center_x = (cords[0] + cords[2]) / 2 | |
center_y = (cords[1] + cords[3]) / 2 | |
# Determine location relative to the image center | |
image_center_x = result.orig_shape[1] / 2 | |
image_center_y = result.orig_shape[0] / 2 | |
location_x = "left" if center_x < image_center_x else "right" | |
location_y = "above" if center_y < image_center_y else "below" | |
# Calculate dimensions of the bounding box | |
width = abs(cords[2] - cords[0]) | |
height = abs(cords[3] - cords[1]) | |
# Create annotation dictionary | |
annotation = { | |
"id_tracked": id_track, | |
"object_type_name": name_object_with_count, | |
"coordinates": cords, | |
"probability": conf, | |
"count": count, | |
"location": (location_x, location_y), | |
"dimension": (width, height) | |
} | |
# Append annotation to the list | |
annotations.append(annotation) | |
# Print annotation if needed for debugging | |
print(annotation) | |
return annotations | |
######################## | |