Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
|
|
1 |
import os
|
2 |
import torch
|
3 |
-
import cv2
|
4 |
-
import numpy as np
|
5 |
import gradio as gr
|
6 |
from PIL import Image
|
7 |
|
@@ -20,39 +19,14 @@ else:
|
|
20 |
print(f"Model weights found at: {best_path}")
|
21 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path=best_path)
|
22 |
|
23 |
-
# Define weapon-related classes
|
24 |
-
WEAPON_CLASSES = ["pistol", "rifle", "knife", "bomb", "gun", "weapon"]
|
25 |
-
|
26 |
# Detection function
|
27 |
def detect_weapons(image):
|
28 |
results = model(image)
|
29 |
-
detected_classes = results.pandas().xyxy[0]['name']
|
30 |
-
|
31 |
-
threat_message = "Threat detected: Be careful" if weapons else "No threat detected"
|
32 |
return threat_message, Image.fromarray(results.render()[0])
|
33 |
|
34 |
-
#
|
35 |
-
def detect_from_camera():
|
36 |
-
cap = cv2.VideoCapture(0) # Open webcam (use 0 for default camera)
|
37 |
-
while cap.isOpened():
|
38 |
-
ret, frame = cap.read()
|
39 |
-
if not ret:
|
40 |
-
break
|
41 |
-
# Convert frame (OpenCV format) to PIL image
|
42 |
-
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
43 |
-
results = model(image)
|
44 |
-
detected_classes = results.pandas().xyxy[0]['name']
|
45 |
-
weapons = [cls for cls in detected_classes if cls in WEAPON_CLASSES]
|
46 |
-
annotated_frame = np.array(results.render()[0])
|
47 |
-
|
48 |
-
# Display live feed with annotations
|
49 |
-
cv2.imshow("Weapon Detection", cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR))
|
50 |
-
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit
|
51 |
-
break
|
52 |
-
cap.release()
|
53 |
-
cv2.destroyAllWindows()
|
54 |
-
|
55 |
-
# Gradio interface for image uploads
|
56 |
iface = gr.Interface(
|
57 |
fn=detect_weapons,
|
58 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
@@ -61,21 +35,7 @@ iface = gr.Interface(
|
|
61 |
gr.Image(label="Detected Image"),
|
62 |
],
|
63 |
title="Weapon Detection AI",
|
64 |
-
description="Upload an image to detect weapons like bombs, guns, and pistols.
|
65 |
)
|
66 |
|
67 |
-
|
68 |
-
def main():
|
69 |
-
print("Select mode:")
|
70 |
-
print("1. Image Upload (Gradio Interface)")
|
71 |
-
print("2. Live Camera Detection")
|
72 |
-
choice = input("Enter your choice (1 or 2): ").strip()
|
73 |
-
if choice == "1":
|
74 |
-
iface.launch()
|
75 |
-
elif choice == "2":
|
76 |
-
detect_from_camera()
|
77 |
-
else:
|
78 |
-
print("Invalid choice. Please restart and select 1 or 2.")
|
79 |
-
|
80 |
-
if __name__ == "__main__":
|
81 |
-
main()
|
|
|
1 |
+
|
2 |
import os
|
3 |
import torch
|
|
|
|
|
4 |
import gradio as gr
|
5 |
from PIL import Image
|
6 |
|
|
|
19 |
print(f"Model weights found at: {best_path}")
|
20 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path=best_path)
|
21 |
|
|
|
|
|
|
|
22 |
# Detection function
|
23 |
def detect_weapons(image):
|
24 |
results = model(image)
|
25 |
+
detected_classes = results.pandas().xyxy[0]['name'].unique()
|
26 |
+
threat_message = "Threat detected: Be careful" if len(detected_classes) > 0 else "No threat detected"
|
|
|
27 |
return threat_message, Image.fromarray(results.render()[0])
|
28 |
|
29 |
+
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
iface = gr.Interface(
|
31 |
fn=detect_weapons,
|
32 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
|
|
35 |
gr.Image(label="Detected Image"),
|
36 |
],
|
37 |
title="Weapon Detection AI",
|
38 |
+
description="Upload an image to detect weapons like bombs, guns, and pistols."
|
39 |
)
|
40 |
|
41 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|