Update services/under_construction/culvert_check.py
Browse files
services/under_construction/culvert_check.py
CHANGED
|
@@ -1,18 +1,19 @@
|
|
| 1 |
from ultralytics import YOLO
|
| 2 |
import cv2
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
MODEL_PATH = "yolov8m.pt"
|
| 6 |
model = YOLO(MODEL_PATH)
|
| 7 |
|
| 8 |
def run_culvert_check(video_file):
|
| 9 |
if video_file is None:
|
| 10 |
-
return "Please upload a video."
|
| 11 |
-
|
| 12 |
cap = cv2.VideoCapture(video_file)
|
| 13 |
results_summary = []
|
| 14 |
-
|
| 15 |
frame_idx = 0
|
|
|
|
| 16 |
while True:
|
| 17 |
success, frame = cap.read()
|
| 18 |
if not success:
|
|
@@ -24,10 +25,15 @@ def run_culvert_check(video_file):
|
|
| 24 |
results = model.predict(source=frame, save=False, conf=0.3)
|
| 25 |
if results:
|
| 26 |
for r in results:
|
| 27 |
-
if hasattr(r, 'boxes') and r.boxes is not None:
|
| 28 |
count = len(r.boxes)
|
| 29 |
results_summary.append(f"Frame {frame_idx}: {count} culvert features detected.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
frame_idx += 1
|
| 31 |
-
cap.release()
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
| 1 |
from ultralytics import YOLO
|
| 2 |
import cv2
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
MODEL_PATH = "yolov8m.pt"
|
| 6 |
model = YOLO(MODEL_PATH)
|
| 7 |
|
| 8 |
def run_culvert_check(video_file):
|
| 9 |
if video_file is None:
|
| 10 |
+
return "Please upload a video.", None
|
| 11 |
+
|
| 12 |
cap = cv2.VideoCapture(video_file)
|
| 13 |
results_summary = []
|
| 14 |
+
detected_frame = None
|
| 15 |
frame_idx = 0
|
| 16 |
+
|
| 17 |
while True:
|
| 18 |
success, frame = cap.read()
|
| 19 |
if not success:
|
|
|
|
| 25 |
results = model.predict(source=frame, save=False, conf=0.3)
|
| 26 |
if results:
|
| 27 |
for r in results:
|
| 28 |
+
if hasattr(r, 'boxes') and r.boxes is not None and len(r.boxes) > 0:
|
| 29 |
count = len(r.boxes)
|
| 30 |
results_summary.append(f"Frame {frame_idx}: {count} culvert features detected.")
|
| 31 |
+
annotated = r.plot()
|
| 32 |
+
detected_frame = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
|
| 33 |
+
cap.release()
|
| 34 |
+
return "\n".join(results_summary), detected_frame
|
| 35 |
+
|
| 36 |
frame_idx += 1
|
|
|
|
| 37 |
|
| 38 |
+
cap.release()
|
| 39 |
+
return "No culvert features found.", None
|