Newer
Older
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 15 17:15:51 2024
@author: Proprietaire
"""
import os
import numpy as np
from PIL import Image
from skimage import io, draw, color
from skimage.feature import hog
from skimage.color import rgb2hsv, rgb2gray
from skimage.segmentation import felzenszwalb
from skimage.measure import regionprops
from sklearn import svm
from sklearn.metrics import accuracy_score
from skimage.transform import pyramid_gaussian
from sklearn.cluster import DBSCAN
AVERAGE_SIZE_IMAGE = (127, 145)
# Utilitaire pour générer une bbox vide pour les images sans label
def generate_empty_bbox(image_width, image_height):
x_min = np.random.randint(0, image_width - AVERAGE_SIZE_IMAGE[0])
y_min = np.random.randint(0, image_height - AVERAGE_SIZE_IMAGE[1])
x_max = x_min + AVERAGE_SIZE_IMAGE[0]
y_max = y_min + AVERAGE_SIZE_IMAGE[1]
return (x_min, y_min, x_max, y_max)
# Chargement des données
def load_data(image_dir, label_dir):
datas = {}
for image_file in os.listdir(image_dir):
image_path = os.path.join(image_dir, image_file)
name = image_file.split('.')[0]
label_path = os.path.join(label_dir, f"{name}.csv")
image = np.array(Image.open(image_path))
with open(label_path, 'r') as file:
rows = file.readlines()
label_data = {}
if rows == ['\n']:
xmin, ymin, xmax, ymax = generate_empty_bbox(image.shape[1], image.shape[0])
cropped_image = np.array(Image.fromarray(image[ymin:ymax, xmin:xmax]).resize(AVERAGE_SIZE_IMAGE))
label_data[0] = {
"name": "empty",
"coord": (xmin, ymin, xmax, ymax),
"img": cropped_image
}
else:
for i, row in enumerate(rows):
row = row.strip().split(",")
xmin, ymin, xmax, ymax = map(int, row[0:4])
class_name = row[4]
cropped_image = np.array(Image.fromarray(image[ymin:ymax, xmin:xmax]).resize(AVERAGE_SIZE_IMAGE))
label_data[i] = {
"name": class_name,
"coord": (xmin, ymin, xmax, ymax),
"img": cropped_image
}
datas[name] = {
"img": image,
"labels": label_data,
}
return datas
# Dict pour convertir les noms de classe en entiers
name_to_int = {
"danger": 0,
"interdiction": 1,
"obligation": 2,
"stop": 3,
"ceder": 4,
"frouge": 5,
"forange": 6,
"fvert": 7,
"ff": 8,
"empty": 9
}
######################################################################################
######################################################################################
# Création des caractéristiques HOG pour la détection
def extract_hog_features_stop(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["stop"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
#elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
else:
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
def extract_hog_features_danger(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["danger"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
def extract_hog_features_interdiction(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["interdiction"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
def extract_hog_features_obligation(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["obligation"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
def extract_hog_features_ceder(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["ceder"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
"""
def extract_hog_features_frouge(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["frouge"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
def extract_hog_features_forange(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["forange"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
def extract_hog_features_fvert(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["fvert"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
"""
def extract_hog_features_feu(datas):
X = []
Y = []
for name, data in datas.items():
for row in data["labels"].values():
img_gray = rgb2gray(row["img"])
hog_features = np.array(hog(img_gray, pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten()
class_label = name_to_int[row["name"]]
if class_label == name_to_int["fvert"] or class_label == name_to_int["forange"] or class_label == name_to_int["frouge"]:
X.append(hog_features)
Y.append(1) # Positive sample (stop sign)
elif class_label != name_to_int["empty"]: # Exclude 'empty' class from negatives
X.append(hog_features)
Y.append(0) # Negative sample (not stop sign)
return np.array(X), np.array(Y)
######################################################################################
######################################################################################
# Entraînement du modèle SVM avec les caractéristiques HOG
datas_train = load_data("dataset-main-train/train/images", "dataset-main-train/train/labels")
datas_val = load_data("dataset-main-val/val/images", "dataset-main-val/val/labels")
# Dossier contenant les images d'évaluation
test_image_folder = 'dataset-main-val/val/images'
output_folder_stop = 'result_detection_v1_stop'
output_folder_danger = 'result_detection_v1_danger'
output_folder_interdiction = 'result_detection_v1_interdiction'
output_folder_obligation = 'result_detection_v1_obligation'
output_folder_ceder = 'result_detection_v1_ceder'
output_folder_feu = 'result_detection_v1_feu'
X_train_stop, Y_train_stop = extract_hog_features_stop(datas_train)
X_val_stop, Y_val_stop = extract_hog_features_stop(datas_val)
X_train_danger, Y_train_danger = extract_hog_features_danger(datas_train)
X_val_danger, Y_val_danger = extract_hog_features_danger(datas_val)
X_train_interdiction, Y_train_interdiction = extract_hog_features_interdiction(datas_train)
X_val_interdiction, Y_val_interdiction = extract_hog_features_interdiction(datas_val)
X_train_obligation, Y_train_obligation = extract_hog_features_obligation(datas_train)
X_val_obligation, Y_val_obligation = extract_hog_features_obligation(datas_val)
X_train_ceder, Y_train_ceder = extract_hog_features_ceder(datas_train)
X_val_ceder, Y_val_ceder = extract_hog_features_ceder(datas_val)
"""
X_train_frouge, Y_train_frouge = extract_hog_features_frouge(datas_train)
X_val_frouge, Y_val_frouge = extract_hog_features_frouge(datas_val)
X_train_forange, Y_train_forange = extract_hog_features_forange(datas_train)
X_val_forange, Y_val_forange = extract_hog_features_forange(datas_val)
X_train_fvert, Y_train_fvert = extract_hog_features_fvert(datas_train)
X_val_fvert, Y_val_fvert = extract_hog_features_fvert(datas_val)
"""
X_train_feu, Y_train_feu = extract_hog_features_feu(datas_train)
X_val_feu, Y_val_feu = extract_hog_features_feu(datas_val)
######################################################################################
######################################################################################
# Créer et entraîner le classifieur SVM pour les panneaux stop
clf_stop = svm.SVC(kernel='poly')
clf_stop.fit(X_train_stop, Y_train_stop)
# Prédiction sur le jeu de validation
y_pred = clf_stop.predict(X_val_stop)
print(f"Taux d'erreur SVM pour panneaux stop: {np.mean(y_pred != Y_val_stop)}")
######################################################################################
# Panneaux danger
clf_danger = svm.SVC(kernel='poly')
clf_danger.fit(X_train_danger, Y_train_danger)
y_pred = clf_danger.predict(X_val_danger)
print(f"Taux d'erreur SVM pour panneaux danger: {np.mean(y_pred != Y_val_danger)}")
#####################################################################################
# Panneaux interdiction
clf_interdiction = svm.SVC(kernel='poly')
clf_interdiction.fit(X_train_interdiction, Y_train_interdiction)
y_pred = clf_interdiction.predict(X_val_interdiction)
print(f"Taux d'erreur SVM pour panneaux interdiction: {np.mean(y_pred != Y_val_interdiction)}")
#####################################################################################
# Panneaux obligation
clf_obligation = svm.SVC(kernel='poly')
clf_obligation.fit(X_train_obligation, Y_train_obligation)
y_pred = clf_obligation.predict(X_val_obligation)
print(f"Taux d'erreur SVM pour panneaux obligation: {np.mean(y_pred != Y_val_obligation)}")
#####################################################################################
# Panneaux ceder
clf_ceder = svm.SVC(kernel='poly')
clf_ceder.fit(X_train_ceder, Y_train_ceder)
y_pred = clf_ceder.predict(X_val_ceder)
print(f"Taux d'erreur SVM pour panneaux ceder: {np.mean(y_pred != Y_val_ceder)}")
"""
#####################################################################################
# Panneaux frouge
clf_frouge = svm.SVC(kernel='poly')
clf_frouge.fit(X_train_frouge, Y_train_frouge)
y_pred = clf_frouge.predict(X_val_frouge)
print(f"Taux d'erreur SVM pour panneaux frouge: {np.mean(y_pred != Y_val_frouge)}")
#####################################################################################
# Panneaux forange
clf_forange = svm.SVC(kernel='poly')
clf_forange.fit(X_train_forange, Y_train_forange)
y_pred = clf_forange.predict(X_val_forange)
print(f"Taux d'erreur SVM pour panneaux forange: {np.mean(y_pred != Y_val_forange)}")
#####################################################################################
# Panneaux fvert
clf_fvert = svm.SVC(kernel='poly')
clf_fvert.fit(X_train_fvert, Y_train_fvert)
y_pred = clf_fvert.predict(X_val_fvert)
print(f"Taux d'erreur SVM pour panneaux fvert: {np.mean(y_pred != Y_val_fvert)}")
"""
####################################################################################
# Feu
clf_feu = svm.SVC(kernel='poly')
clf_feu.fit(X_train_feu, Y_train_feu)
####################################################################################
####################################################################################
# Fonction pour faire glisser une fenêtre sur l'image
def sliding_window(image, step_size, window_size):
for y in range(0, image.shape[0] - window_size[1], step_size):
for x in range(0, image.shape[1] - window_size[0], step_size):
yield (x, y, image[y:y + window_size[1], x:x + window_size[0]])
# Fonction pour la suppression des doublons (NMS)
def non_max_suppression(boxes, overlap_thresh=0.3):
if len(boxes) == 0:
return []
boxes = np.array(boxes)
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []
x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
overlap = (w * h) / area[idxs[:last]]
idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlap_thresh)[0])))
return boxes[pick].astype("int")
# Préparation du dossier de sortie
if not os.path.exists(output_folder_stop):
os.makedirs(output_folder_stop)
if not os.path.exists(output_folder_danger):
os.makedirs(output_folder_danger)
if not os.path.exists(output_folder_interdiction):
os.makedirs(output_folder_interdiction)
if not os.path.exists(output_folder_obligation):
os.makedirs(output_folder_obligation)
if not os.path.exists(output_folder_ceder):
os.makedirs(output_folder_ceder)
if not os.path.exists(output_folder_feu):
os.makedirs(output_folder_feu)
######################################################################################
######################################################################################
#Fonction prediction?
def prediction(hog_features):
if clf_stop.predict(hog_features)==1:
return "stop"
if clf_danger.predict(hog_features)==1:
return "danger"
if clf_interdiction.predict(hog_features)==1:
return "interdiction"
if clf_obligation.predict(hog_features)==1:
return "obligation"
######################################################################################
######################################################################################
######################################################################################
#Detection et classification
window_sizes = [(64, 64), (128, 128), (256, 256),(512,512)] # Différentes tailles de fenêtres
step_size = 32
stop=0
danger=0
interdiction=0
obligation=0
ceder=0
feu=0
for filename in os.listdir(test_image_folder):
test_image_path = os.path.join(test_image_folder, filename)
test_image = io.imread(test_image_path)
detections = []
for window_size in window_sizes:
for (x, y, window) in sliding_window(test_image, step_size=step_size, window_size=window_size):
if window.shape[0] != window_size[1] or window.shape[1] != window_size[0]:
continue
# Extraire les caractéristiques HOG de la fenêtre
window_resized = np.array(Image.fromarray(window).resize(AVERAGE_SIZE_IMAGE))
hog_features = np.array(hog(rgb2gray(window_resized), pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten().reshape(1, -1)
#pred = prediction(hog_features)
pred=clf_stop.predict(hog_features)
if pred==1:
detections.append((x, y, x + window_size[0], y + window_size[1]))
"""
if pred == "stop":
detections.append((x, y, x + window_size[0], y + window_size[1]))
stop=stop+1
elif pred == "danger":
detections.append((x, y, x + window_size[0], y + window_size[1]))
danger=danger+1
elif pred == "interdiction":
detections.append((x, y, x + window_size[0], y + window_size[1]))
interdiction=interdiction+1
elif pred == "obligation":
detections.append((x, y, x + window_size[0], y + window_size[1]))
obligation=obligation+1
"""
# Suppression des doublons (NMS)
nms_boxes = non_max_suppression(detections, overlap_thresh=0.3)
for (x1, y1, x2, y2) in nms_boxes:
rr, cc = draw.rectangle_perimeter(start=(y1, x1), extent=(y2 - y1, x2 - x1), shape=test_image.shape)
test_image[rr, cc] = [255, 0, 0]
stop=stop+1
output_path = os.path.join(output_folder_stop, filename)
io.imsave(output_path, test_image)
print(f"Processed and saved: {filename}")
for filename in os.listdir(test_image_folder):
test_image_path = os.path.join(test_image_folder, filename)
test_image = io.imread(test_image_path)
detections = []
for window_size in window_sizes:
for (x, y, window) in sliding_window(test_image, step_size=step_size, window_size=window_size):
if window.shape[0] != window_size[1] or window.shape[1] != window_size[0]:
continue
# Extraire les caractéristiques HOG de la fenêtre
window_resized = np.array(Image.fromarray(window).resize(AVERAGE_SIZE_IMAGE))
hog_features = np.array(hog(rgb2gray(window_resized), pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten().reshape(1, -1)
#pred = prediction(hog_features)
pred=clf_danger.predict(hog_features)
if pred==1:
detections.append((x, y, x + window_size[0], y + window_size[1]))
"""
if pred == "stop":
detections.append((x, y, x + window_size[0], y + window_size[1]))
stop=stop+1
elif pred == "danger":
detections.append((x, y, x + window_size[0], y + window_size[1]))
danger=danger+1
elif pred == "interdiction":
detections.append((x, y, x + window_size[0], y + window_size[1]))
interdiction=interdiction+1
elif pred == "obligation":
detections.append((x, y, x + window_size[0], y + window_size[1]))
obligation=obligation+1
"""
# Suppression des doublons (NMS)
nms_boxes = non_max_suppression(detections, overlap_thresh=0.3)
for (x1, y1, x2, y2) in nms_boxes:
rr, cc = draw.rectangle_perimeter(start=(y1, x1), extent=(y2 - y1, x2 - x1), shape=test_image.shape)
test_image[rr, cc] = [255, 0, 0]
danger=danger+1
output_path = os.path.join(output_folder_danger, filename)
io.imsave(output_path, test_image)
print(f"Processed and saved: {filename}")
for filename in os.listdir(test_image_folder):
test_image_path = os.path.join(test_image_folder, filename)
test_image = io.imread(test_image_path)
detections = []
for window_size in window_sizes:
for (x, y, window) in sliding_window(test_image, step_size=step_size, window_size=window_size):
if window.shape[0] != window_size[1] or window.shape[1] != window_size[0]:
continue
# Extraire les caractéristiques HOG de la fenêtre
window_resized = np.array(Image.fromarray(window).resize(AVERAGE_SIZE_IMAGE))
hog_features = np.array(hog(rgb2gray(window_resized), pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten().reshape(1, -1)
#pred = prediction(hog_features)
pred=clf_interdiction.predict(hog_features)
if pred==1:
detections.append((x, y, x + window_size[0], y + window_size[1]))
"""
if pred == "stop":
detections.append((x, y, x + window_size[0], y + window_size[1]))
stop=stop+1
elif pred == "danger":
detections.append((x, y, x + window_size[0], y + window_size[1]))
danger=danger+1
elif pred == "interdiction":
detections.append((x, y, x + window_size[0], y + window_size[1]))
interdiction=interdiction+1
elif pred == "obligation":
detections.append((x, y, x + window_size[0], y + window_size[1]))
obligation=obligation+1
"""
# Suppression des doublons (NMS)
nms_boxes = non_max_suppression(detections, overlap_thresh=0.3)
for (x1, y1, x2, y2) in nms_boxes:
rr, cc = draw.rectangle_perimeter(start=(y1, x1), extent=(y2 - y1, x2 - x1), shape=test_image.shape)
test_image[rr, cc] = [255, 0, 0]
interdiction=interdiction+1
output_path = os.path.join(output_folder_interdiction, filename)
io.imsave(output_path, test_image)
print(f"Processed and saved: {filename}")
for filename in os.listdir(test_image_folder):
test_image_path = os.path.join(test_image_folder, filename)
test_image = io.imread(test_image_path)
detections = []
for window_size in window_sizes:
for (x, y, window) in sliding_window(test_image, step_size=step_size, window_size=window_size):
if window.shape[0] != window_size[1] or window.shape[1] != window_size[0]:
continue
# Extraire les caractéristiques HOG de la fenêtre
window_resized = np.array(Image.fromarray(window).resize(AVERAGE_SIZE_IMAGE))
hog_features = np.array(hog(rgb2gray(window_resized), pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten().reshape(1, -1)
#pred = prediction(hog_features)
pred=clf_obligation.predict(hog_features)
if pred==1:
detections.append((x, y, x + window_size[0], y + window_size[1]))
"""
if pred == "stop":
detections.append((x, y, x + window_size[0], y + window_size[1]))
stop=stop+1
elif pred == "danger":
detections.append((x, y, x + window_size[0], y + window_size[1]))
danger=danger+1
elif pred == "interdiction":
detections.append((x, y, x + window_size[0], y + window_size[1]))
interdiction=interdiction+1
elif pred == "obligation":
detections.append((x, y, x + window_size[0], y + window_size[1]))
obligation=obligation+1
"""
# Suppression des doublons (NMS)
nms_boxes = non_max_suppression(detections, overlap_thresh=0.3)
for (x1, y1, x2, y2) in nms_boxes:
rr, cc = draw.rectangle_perimeter(start=(y1, x1), extent=(y2 - y1, x2 - x1), shape=test_image.shape)
test_image[rr, cc] = [255, 0, 0]
obligation=obligation+1
output_path = os.path.join(output_folder_obligation, filename)
io.imsave(output_path, test_image)
print(f"Processed and saved: {filename}")
for filename in os.listdir(test_image_folder):
test_image_path = os.path.join(test_image_folder, filename)
test_image = io.imread(test_image_path)
detections = []
for window_size in window_sizes:
for (x, y, window) in sliding_window(test_image, step_size=step_size, window_size=window_size):
if window.shape[0] != window_size[1] or window.shape[1] != window_size[0]:
continue
# Extraire les caractéristiques HOG de la fenêtre
window_resized = np.array(Image.fromarray(window).resize(AVERAGE_SIZE_IMAGE))
hog_features = np.array(hog(rgb2gray(window_resized), pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten().reshape(1, -1)
#pred = prediction(hog_features)
pred=clf_ceder.predict(hog_features)
if pred==1:
detections.append((x, y, x + window_size[0], y + window_size[1]))
"""
if pred == "stop":
detections.append((x, y, x + window_size[0], y + window_size[1]))
stop=stop+1
elif pred == "danger":
detections.append((x, y, x + window_size[0], y + window_size[1]))
danger=danger+1
elif pred == "interdiction":
detections.append((x, y, x + window_size[0], y + window_size[1]))
interdiction=interdiction+1
elif pred == "obligation":
detections.append((x, y, x + window_size[0], y + window_size[1]))
obligation=obligation+1
"""
# Suppression des doublons (NMS)
nms_boxes = non_max_suppression(detections, overlap_thresh=0.3)
for (x1, y1, x2, y2) in nms_boxes:
rr, cc = draw.rectangle_perimeter(start=(y1, x1), extent=(y2 - y1, x2 - x1), shape=test_image.shape)
test_image[rr, cc] = [255, 0, 0]
ceder=ceder+1
output_path = os.path.join(output_folder_ceder, filename)
io.imsave(output_path, test_image)
print(f"Processed and saved: {filename}")
for filename in os.listdir(test_image_folder):
test_image_path = os.path.join(test_image_folder, filename)
test_image = io.imread(test_image_path)
detections = []
for window_size in window_sizes:
for (x, y, window) in sliding_window(test_image, step_size=step_size, window_size=window_size):
if window.shape[0] != window_size[1] or window.shape[1] != window_size[0]:
continue
# Extraire les caractéristiques HOG de la fenêtre
window_resized = np.array(Image.fromarray(window).resize(AVERAGE_SIZE_IMAGE))
hog_features = np.array(hog(rgb2gray(window_resized), pixels_per_cell=(16, 16), cells_per_block=(2, 2), block_norm='L2-Hys')).flatten().reshape(1, -1)
#pred = prediction(hog_features)
pred=clf_feu.predict(hog_features)
if pred==1:
detections.append((x, y, x + window_size[0], y + window_size[1]))
"""
if pred == "stop":
detections.append((x, y, x + window_size[0], y + window_size[1]))
stop=stop+1
elif pred == "danger":
detections.append((x, y, x + window_size[0], y + window_size[1]))
danger=danger+1
elif pred == "interdiction":
detections.append((x, y, x + window_size[0], y + window_size[1]))
interdiction=interdiction+1
elif pred == "obligation":
detections.append((x, y, x + window_size[0], y + window_size[1]))
obligation=obligation+1
"""
# Suppression des doublons (NMS)
nms_boxes = non_max_suppression(detections, overlap_thresh=0.3)
for (x1, y1, x2, y2) in nms_boxes:
rr, cc = draw.rectangle_perimeter(start=(y1, x1), extent=(y2 - y1, x2 - x1), shape=test_image.shape)
test_image[rr, cc] = [255, 0, 0]
feu=feu+1
output_path = os.path.join(output_folder_feu, filename)
io.imsave(output_path, test_image)
print(f"Processed and saved: {filename}")
print("Panneaux stop détectés :", stop)
print("Panneaux danger détectés :", danger)
print("Panneaux interdiction détectés :", interdiction)
print("Panneaux obligation détectés :", obligation)
print("Panneaux ceder détectés :", ceder)
print("Panneaux feu détectés :", feu)