Michtiii commited on
Commit
f767e50
Β·
verified Β·
1 Parent(s): 7fa286a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -7,12 +7,11 @@ import tempfile
7
  import time
8
  import os
9
 
10
- # Fix Ultralytics config (HF Spaces fix)
11
  os.environ["YOLO_CONFIG_DIR"] = "/tmp/Ultralytics"
12
 
13
- # πŸ”₯ Use your trained model for real pothole detection
14
- # model = YOLO("best.pt")
15
- model = YOLO("yolov8n.pt") # fallback demo
16
 
17
  detections_log = []
18
 
@@ -34,9 +33,12 @@ def process_video(video, conf_threshold):
34
  if not cap.isOpened():
35
  return None, None, "Cannot open video", pd.DataFrame()
36
 
 
37
  fps = cap.get(cv2.CAP_PROP_FPS)
38
  if fps <= 0 or fps > 60:
39
- fps = 25
 
 
40
 
41
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) or 640
42
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) or 480
@@ -44,7 +46,7 @@ def process_video(video, conf_threshold):
44
  output_path = os.path.join(tempfile.gettempdir(), f"out_{int(time.time())}.mp4")
45
 
46
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
47
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
48
 
49
  if not out.isOpened():
50
  return None, None, "VideoWriter failed", pd.DataFrame()
@@ -52,27 +54,39 @@ def process_video(video, conf_threshold):
52
  pothole_count = 0
53
  frame_count = 0
54
 
 
 
 
 
55
  while cap.isOpened():
56
  ret, frame = cap.read()
57
  if not ret:
58
  break
59
 
 
 
 
 
 
60
  frame = cv2.resize(frame, (width, height))
 
 
 
 
61
  results = model(frame)[0]
62
 
63
- # πŸ”΄ Detection loop
64
  for box in results.boxes:
65
  conf = float(box.conf[0])
66
 
67
- if conf < conf_threshold:
 
68
  continue
69
 
70
  x1, y1, x2, y2 = map(int, box.xyxy[0])
71
 
72
- # πŸ”΄ RED BOUNDING BOX
73
  cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 4)
74
 
75
- # Label text
76
  label = f"Pothole {conf:.2f}"
77
 
78
  # Label background
@@ -118,19 +132,18 @@ def process_video(video, conf_threshold):
118
  # ---------------- UI ---------------- #
119
  with gr.Blocks() as demo:
120
 
121
- gr.Markdown("# 🚧 Smart Pothole Detection (YOLO)")
122
 
123
  with gr.Row():
124
 
125
- # LEFT PANEL (SMALL)
126
  with gr.Column(scale=1):
127
  video_input = gr.Video(label="Upload Video")
128
- conf = gr.Slider(0, 1, value=0.3, step=0.05, label="Confidence")
129
  btn = gr.Button("Run Detection")
130
 
131
- # RIGHT PANEL (BIG)
132
  with gr.Column(scale=3):
133
-
134
  with gr.Row():
135
  before = gr.Video(label="Original")
136
  after = gr.Video(label="Detected")
@@ -145,6 +158,6 @@ with gr.Blocks() as demo:
145
  )
146
 
147
 
148
- # HF Spaces compatible launch
149
  if __name__ == "__main__":
150
  demo.launch()
 
7
  import time
8
  import os
9
 
10
+ # Fix Ultralytics path (HF Spaces)
11
  os.environ["YOLO_CONFIG_DIR"] = "/tmp/Ultralytics"
12
 
13
+ # πŸ”₯ Replace with best.pt for real pothole detection
14
+ model = YOLO("yolov8n.pt")
 
15
 
16
  detections_log = []
17
 
 
33
  if not cap.isOpened():
34
  return None, None, "Cannot open video", pd.DataFrame()
35
 
36
+ # πŸŽ₯ FIX FPS (avoid fast video)
37
  fps = cap.get(cv2.CAP_PROP_FPS)
38
  if fps <= 0 or fps > 60:
39
+ fps = 20
40
+
41
+ output_fps = max(10, fps * 0.6)
42
 
43
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) or 640
44
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) or 480
 
46
  output_path = os.path.join(tempfile.gettempdir(), f"out_{int(time.time())}.mp4")
47
 
48
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
49
+ out = cv2.VideoWriter(output_path, fourcc, output_fps, (width, height))
50
 
51
  if not out.isOpened():
52
  return None, None, "VideoWriter failed", pd.DataFrame()
 
54
  pothole_count = 0
55
  frame_count = 0
56
 
57
+ # ⚑ Frame skipping for smoother output
58
+ frame_skip = 2
59
+ frame_id = 0
60
+
61
  while cap.isOpened():
62
  ret, frame = cap.read()
63
  if not ret:
64
  break
65
 
66
+ frame_id += 1
67
+
68
+ if frame_id % frame_skip != 0:
69
+ continue
70
+
71
  frame = cv2.resize(frame, (width, height))
72
+
73
+ # πŸ”§ Smooth frame
74
+ frame = cv2.GaussianBlur(frame, (3, 3), 0)
75
+
76
  results = model(frame)[0]
77
 
 
78
  for box in results.boxes:
79
  conf = float(box.conf[0])
80
 
81
+ # 🎯 Better filtering
82
+ if conf < max(conf_threshold, 0.4):
83
  continue
84
 
85
  x1, y1, x2, y2 = map(int, box.xyxy[0])
86
 
87
+ # πŸ”΄ RED BOX
88
  cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 4)
89
 
 
90
  label = f"Pothole {conf:.2f}"
91
 
92
  # Label background
 
132
  # ---------------- UI ---------------- #
133
  with gr.Blocks() as demo:
134
 
135
+ gr.Markdown("# 🚧 Smart Pothole Detection (Improved)")
136
 
137
  with gr.Row():
138
 
139
+ # LEFT PANEL
140
  with gr.Column(scale=1):
141
  video_input = gr.Video(label="Upload Video")
142
+ conf = gr.Slider(0, 1, value=0.4, step=0.05, label="Confidence")
143
  btn = gr.Button("Run Detection")
144
 
145
+ # RIGHT PANEL (BIG VIDEOS)
146
  with gr.Column(scale=3):
 
147
  with gr.Row():
148
  before = gr.Video(label="Original")
149
  after = gr.Video(label="Detected")
 
158
  )
159
 
160
 
161
+ # HF Spaces launch
162
  if __name__ == "__main__":
163
  demo.launch()