kebincontreras commited on
Commit
2c61a10
·
verified ·
1 Parent(s): 3897883

Delete detection.py

Browse files
Files changed (1) hide show
  1. detection.py +0 -157
detection.py DELETED
@@ -1,157 +0,0 @@
1
-
2
- from ultralytics import YOLOv10
3
- import os
4
- import torch
5
-
6
- def yolov10_inference(image, model_id, image_size, conf_threshold):
7
- model = YOLOv10.from_pretrained(f'jameslahm/{model_id}')
8
- results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
9
- detections = []
10
- if results and len(results) > 0:
11
- for result in results:
12
- for box in result.boxes:
13
- detections.append({
14
- "coords": box.xyxy.cpu().numpy(),
15
- "class": result.names[int(box.cls.cpu())],
16
- "conf": box.conf.cpu().numpy()
17
- #"bbox": box.xyxy.cpu().numpy().tolist()
18
- })
19
- return results[0].plot() if results and len(results) > 0 else image, detections
20
-
21
- def calculate_iou(boxA, boxB):
22
- xA = max(boxA[0], boxB[0])
23
- yA = max(boxA[1], boxB[1])
24
- xB = min(boxA[2], boxB[2])
25
- yB = min(boxA[3], boxB[3])
26
- interArea = max(0, xB - xA) * max(0, yB - yA)
27
- boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
28
- boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])
29
- iou = interArea / float(boxAArea + boxBArea - interArea)
30
- return iou
31
-
32
-
33
-
34
- def calculate_detection_metrics(detections_true, detections_pred):
35
- true_positives = 0
36
- pred_positives = len(detections_pred)
37
- real_positives = len(detections_true)
38
- ious = []
39
- for pred in detections_pred:
40
- for real in detections_true:
41
- if pred['class'] == real['class']:
42
- iou = calculate_iou(pred['coords'].flatten(), real['coords'].flatten())
43
- if iou >= 0.5:
44
- true_positives += 1
45
- ious.append(iou)
46
- break
47
- precision = true_positives / pred_positives if pred_positives > 0 else 0
48
- recall = true_positives / real_positives if real_positives > 0 else 0
49
- f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
50
- average_iou = sum(ious) / len(ious) if ious else 0
51
- return {"Precision": precision, "Recall": recall, "F1-Score": f1_score, "IOU": average_iou}
52
-
53
-
54
-
55
-
56
- def read_kitti_annotations(file_path):
57
- ground_truths = []
58
- with open(file_path, 'r') as file:
59
- for line in file:
60
- parts = line.strip().split()
61
- if parts[0] != 'DontCare': # Ignorar 'DontCare'
62
- class_label = parts[0].lower() # Clase en minúscula
63
- bbox = [float(parts[4]), float(parts[5]), float(parts[6]), float(parts[7])]
64
- ground_truths.append({'class': class_label, 'bbox': bbox})
65
- return ground_truths
66
-
67
-
68
- def save_detections(detections, output_dir, filename='detections.txt'):
69
- if not os.path.exists(output_dir):
70
- os.makedirs(output_dir)
71
- with open(os.path.join(output_dir, filename), 'w') as file:
72
- for detection in detections:
73
- class_label = detection['class']
74
- bbox = ','.join(map(str, detection['bbox']))
75
- file.write(f"{class_label},[{bbox}]\n")
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
- def yolov10_inference_1(image, model_id, image_size, conf_threshold):
92
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
93
- model = YOLOv10.from_pretrained(f'jameslahm/{model_id}').to(device)
94
- results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
95
- detections = []
96
- if results and len(results) > 0:
97
- for result in results:
98
- for box in result.boxes:
99
- detections.append({
100
- #"coords": box.xyxy.cpu().numpy(),
101
- "class": result.names[int(box.cls.cpu())],
102
- #"conf": box.conf.cpu().numpy()
103
-
104
- "bbox": box.xyxy.cpu().numpy().tolist()
105
- })
106
- return results[0].plot() if results and len(results) > 0 else image, detections
107
-
108
-
109
- def calculate_iou_1(boxA, boxB):
110
- # Asegúrate de que boxA y boxB son listas planas de flotantes
111
- # Esto es necesario porque la función max() y min() requieren comparar elementos directamente
112
- boxA = [float(num) for sublist in boxA for num in sublist] if isinstance(boxA[0], list) else [float(num) for num in boxA]
113
- boxB = [float(num) for sublist in boxB for num in sublist] if isinstance(boxB[0], list) else [float(num) for num in boxB]
114
-
115
- # Determina las coordenadas de la intersección
116
- xA = max(boxA[0], boxB[0])
117
- yA = max(boxA[1], boxB[1])
118
- xB = min(boxA[2], boxB[2])
119
- yB = min(boxA[3], boxB[3])
120
-
121
- # Calcula el área de la intersección
122
- interArea = max(0, xB - xA) * max(0, yB - yA)
123
-
124
- # Calcula el área de cada cuadro delimitador y la unión
125
- boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
126
- boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])
127
- unionArea = boxAArea + boxBArea - interArea
128
-
129
- # Calcula el IoU
130
- iou = interArea / float(unionArea)
131
- return iou
132
-
133
-
134
- def calculate_detection_metrics_1(detections_true, detections_pred):
135
- true_positives = 0
136
- pred_positives = len(detections_pred)
137
- real_positives = len(detections_true)
138
- ious = []
139
- for pred in detections_pred:
140
- pred_bbox = pred['bbox']
141
- pred_class = pred['class']
142
- for real in detections_true:
143
- real_bbox = real['bbox']
144
- real_class = real['class']
145
- if pred_class == real_class:
146
- iou = calculate_iou_1(pred_bbox, real_bbox)
147
- if iou >= 0.5:
148
- true_positives += 1
149
- ious.append(iou)
150
- break
151
- precision = true_positives / pred_positives if pred_positives > 0 else 0
152
- recall = true_positives / real_positives if real_positives > 0 else 0
153
- f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
154
- average_iou = sum(ious) / len(ious) if ious else 0
155
- return {"Precision": precision, "Recall": recall, "F1-Score": f1_score, "IOU": average_iou}
156
-
157
-