Spaces:
Runtime error
Runtime error
Create models/object_detection.py
Browse files- models/object_detection.py +27 -0
models/object_detection.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load pretrained YOLO model or Faster R-CNN
|
| 6 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Using small YOLOv5 for demonstration
|
| 7 |
+
|
| 8 |
+
def detect_faults(video):
|
| 9 |
+
"""
|
| 10 |
+
Detect faults like cracks, dirt, etc. in the given video using YOLOv5 model.
|
| 11 |
+
Args:
|
| 12 |
+
- video (cv2.VideoCapture): Input video or image file
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
- result (list): List of detected faults with confidence scores
|
| 16 |
+
"""
|
| 17 |
+
faults = []
|
| 18 |
+
while True:
|
| 19 |
+
ret, frame = video.read()
|
| 20 |
+
if not ret:
|
| 21 |
+
break
|
| 22 |
+
results = model(frame) # Run the model on the frame
|
| 23 |
+
result = results.pandas().xywh[0] # Get bounding box and label data
|
| 24 |
+
faults.append(result)
|
| 25 |
+
|
| 26 |
+
video.release()
|
| 27 |
+
return faults
|