Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,8 +19,12 @@ if best_path is None:
|
|
| 19 |
print("Using pre-trained YOLOv5 weights (yolov5s.pt) instead.")
|
| 20 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Load pre-trained weights
|
| 21 |
else:
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Step 3: Define weapon classes to detect
|
| 26 |
weapon_classes = ['bomb', 'gun', 'pistol', 'Automatic', 'Rifle', 'Bazooka',
|
|
@@ -28,7 +32,10 @@ weapon_classes = ['bomb', 'gun', 'pistol', 'Automatic', 'Rifle', 'Bazooka',
|
|
| 28 |
'Sniper', 'Sword'] # Adjust based on your dataset
|
| 29 |
|
| 30 |
def detect_weapons(image):
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Print available model class names to check for class mismatches
|
| 34 |
model_classes = results.names # This should give the list of class labels used by the model
|
|
|
|
| 19 |
print("Using pre-trained YOLOv5 weights (yolov5s.pt) instead.")
|
| 20 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Load pre-trained weights
|
| 21 |
else:
|
| 22 |
+
try:
|
| 23 |
+
print(f"Model weights found at: {best_path}")
|
| 24 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path=best_path)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error loading custom model: {e}")
|
| 27 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Fallback to pre-trained model
|
| 28 |
|
| 29 |
# Step 3: Define weapon classes to detect
|
| 30 |
weapon_classes = ['bomb', 'gun', 'pistol', 'Automatic', 'Rifle', 'Bazooka',
|
|
|
|
| 32 |
'Sniper', 'Sword'] # Adjust based on your dataset
|
| 33 |
|
| 34 |
def detect_weapons(image):
|
| 35 |
+
try:
|
| 36 |
+
results = model(image)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error during detection: {e}", None
|
| 39 |
|
| 40 |
# Print available model class names to check for class mismatches
|
| 41 |
model_classes = results.names # This should give the list of class labels used by the model
|