Update app.py
Browse files
app.py
CHANGED
@@ -16,10 +16,20 @@ def predict(image):
|
|
16 |
results_all = model_all(img_array)
|
17 |
results_np = model_np(img_array)
|
18 |
|
|
|
19 |
# Plot results from the first model (all detections)
|
|
|
20 |
for r in results_all:
|
21 |
im_array = r.plot()
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Initialize counters and confidence lists
|
25 |
class_counts_all = {i: 0 for i in range(len(model_all.names))}
|
@@ -39,17 +49,16 @@ def predict(image):
|
|
39 |
np_count += 1
|
40 |
np_confidences.append(float(box.conf[0]))
|
41 |
|
42 |
-
#
|
43 |
output = "Detection Results:\n"
|
44 |
-
output += "Model 1 (Helmet, License Plate, Motorcyclist):\n"
|
45 |
for i in range(len(model_all.names)):
|
46 |
count = class_counts_all[i]
|
47 |
avg_conf = np.mean(class_confidences_all[i]) if class_confidences_all[i] else 0
|
48 |
output += f"{model_all.names[i]}: {count} detections (Avg. Confidence: {avg_conf:.2f})\n"
|
49 |
|
50 |
-
|
51 |
avg_np_conf = np.mean(np_confidences) if np_confidences else 0
|
52 |
-
output += f"Number Plates: {np_count} detections (Avg. Confidence: {avg_np_conf:.2f})\n"
|
53 |
|
54 |
return im, output
|
55 |
|
@@ -66,4 +75,4 @@ iface = gr.Interface(
|
|
66 |
)
|
67 |
|
68 |
# Launch the interface
|
69 |
-
iface.launch(share='True')
|
|
|
16 |
results_all = model_all(img_array)
|
17 |
results_np = model_np(img_array)
|
18 |
|
19 |
+
# Merge detections into a single image
|
20 |
# Plot results from the first model (all detections)
|
21 |
+
combined_image = None
|
22 |
for r in results_all:
|
23 |
im_array = r.plot()
|
24 |
+
combined_image = im_array
|
25 |
+
|
26 |
+
# Plot results from the second model (number plate detection) on the same image
|
27 |
+
for r in results_np:
|
28 |
+
np_array = r.plot()
|
29 |
+
if combined_image is not None:
|
30 |
+
combined_image = np.maximum(combined_image, np_array) # Combine both images (max pixel values)
|
31 |
+
|
32 |
+
im = Image.fromarray(combined_image[..., ::-1]) # Convert back to RGB PIL image
|
33 |
|
34 |
# Initialize counters and confidence lists
|
35 |
class_counts_all = {i: 0 for i in range(len(model_all.names))}
|
|
|
49 |
np_count += 1
|
50 |
np_confidences.append(float(box.conf[0]))
|
51 |
|
52 |
+
# Combine the counts from both models into one output string
|
53 |
output = "Detection Results:\n"
|
|
|
54 |
for i in range(len(model_all.names)):
|
55 |
count = class_counts_all[i]
|
56 |
avg_conf = np.mean(class_confidences_all[i]) if class_confidences_all[i] else 0
|
57 |
output += f"{model_all.names[i]}: {count} detections (Avg. Confidence: {avg_conf:.2f})\n"
|
58 |
|
59 |
+
# Add number plate detection results from the second model
|
60 |
avg_np_conf = np.mean(np_confidences) if np_confidences else 0
|
61 |
+
output += f"Number Plates : {np_count} detections (Avg. Confidence: {avg_np_conf:.2f})\n"
|
62 |
|
63 |
return im, output
|
64 |
|
|
|
75 |
)
|
76 |
|
77 |
# Launch the interface
|
78 |
+
iface.launch(share='True')
|