File size: 2,770 Bytes
f33a600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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

########################