Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- utils/inference_router.py +35 -0
- utils/video_utils.py +20 -0
utils/inference_router.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import concurrent.futures
|
| 2 |
+
from models import (
|
| 3 |
+
bridge_detector, encroachment_detector, structure_progress_detector,
|
| 4 |
+
pothole_detector, crack_detector, unauthorized_median_detector,
|
| 5 |
+
crash_barrier_detector, highway_exit_detector, drain_detector,
|
| 6 |
+
tree_height_detector, latitude_detector, vari_detector
|
| 7 |
+
)
|
| 8 |
+
from utils.video_utils import extract_frames
|
| 9 |
+
|
| 10 |
+
model_map = {
|
| 11 |
+
"Bridges": bridge_detector.run,
|
| 12 |
+
"Encroachment": encroachment_detector.run,
|
| 13 |
+
"Structure Progress": structure_progress_detector.run,
|
| 14 |
+
"Potholes": pothole_detector.run,
|
| 15 |
+
"Crack": crack_detector.run,
|
| 16 |
+
"Unauthorized Median Opening": unauthorized_median_detector.run,
|
| 17 |
+
"Crash Barriers": crash_barrier_detector.run,
|
| 18 |
+
"Entry/Exit of Highway": highway_exit_detector.run,
|
| 19 |
+
"Drain": drain_detector.run,
|
| 20 |
+
"Tree Height": tree_height_detector.run,
|
| 21 |
+
"Latitude": latitude_detector.run,
|
| 22 |
+
"VARI": vari_detector.run
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def process_video(video_file, selected_params):
|
| 26 |
+
frames = extract_frames(video_file)
|
| 27 |
+
results = {}
|
| 28 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 29 |
+
future_to_param = {
|
| 30 |
+
executor.submit(model_map[param], frames): param for param in selected_params
|
| 31 |
+
}
|
| 32 |
+
for future in concurrent.futures.as_completed(future_to_param):
|
| 33 |
+
param = future_to_param[future]
|
| 34 |
+
results[param] = future.result()
|
| 35 |
+
return [img for imgs in results.values() for img in imgs]
|
utils/video_utils.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
def extract_frames(video_file, interval=30):
|
| 5 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 6 |
+
tmp.write(video_file.read())
|
| 7 |
+
tmp.close()
|
| 8 |
+
|
| 9 |
+
cap = cv2.VideoCapture(tmp.name)
|
| 10 |
+
frames = []
|
| 11 |
+
i = 0
|
| 12 |
+
while cap.isOpened():
|
| 13 |
+
ret, frame = cap.read()
|
| 14 |
+
if not ret:
|
| 15 |
+
break
|
| 16 |
+
if i % interval == 0:
|
| 17 |
+
frames.append(frame)
|
| 18 |
+
i += 1
|
| 19 |
+
cap.release()
|
| 20 |
+
return frames
|