Spaces:
Sleeping
Sleeping
nn
commited on
Upload app.py
Browse files
app.py
CHANGED
|
@@ -40,7 +40,7 @@ def read_yolo_boxes(file_path):
|
|
| 40 |
for line in f:
|
| 41 |
parts = line.strip().split()
|
| 42 |
class_id = int(parts[0])
|
| 43 |
-
if COCO_CLASSES[class_id] != 'traffic light':
|
| 44 |
class_name = COCO_CLASSES[class_id]
|
| 45 |
x, y, w, h = map(float, parts[1:5])
|
| 46 |
boxes.append((class_name, x, y, w, h))
|
|
@@ -88,18 +88,20 @@ def plot_boxes_and_segment(image, yolo_boxes, segment, img_width, img_height, th
|
|
| 88 |
labels = {'intersecting': 'Intersecting Box', 'obstructed': 'Obstructed Box', 'not touching': 'Non-interacting Box'}
|
| 89 |
|
| 90 |
for yolo_box in yolo_boxes:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
ax.legend()
|
| 99 |
ax.axis('off')
|
| 100 |
plt.tight_layout()
|
| 101 |
return fig
|
| 102 |
|
|
|
|
| 103 |
# COCO classes
|
| 104 |
COCO_CLASSES = [
|
| 105 |
'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
|
|
@@ -177,16 +179,18 @@ def detect_objects(image, rail_segment):
|
|
| 177 |
|
| 178 |
fig = plot_boxes_and_segment(image, yolo_boxes, rail_segment, img_width, img_height, threshold)
|
| 179 |
|
| 180 |
-
|
| 181 |
for class_name, x, y, w, h in yolo_boxes:
|
| 182 |
-
|
| 183 |
-
|
|
|
|
| 184 |
|
| 185 |
os.remove(temp_image_path)
|
| 186 |
os.remove(label_path)
|
| 187 |
|
| 188 |
return fig, "\n".join(results), yolo_boxes
|
| 189 |
|
|
|
|
| 190 |
def process_video(video_path, rail_segment, frame_skip=15):
|
| 191 |
cap = cv2.VideoCapture(video_path)
|
| 192 |
if not cap.isOpened():
|
|
@@ -205,7 +209,7 @@ def process_video(video_path, rail_segment, frame_skip=15):
|
|
| 205 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 206 |
threshold = 10 # Set threshold (in pixels) for obstruction detection
|
| 207 |
|
| 208 |
-
|
| 209 |
ret, frame = cap.read()
|
| 210 |
if not ret:
|
| 211 |
break
|
|
@@ -216,33 +220,30 @@ def process_video(video_path, rail_segment, frame_skip=15):
|
|
| 216 |
|
| 217 |
processed_count += 1
|
| 218 |
|
| 219 |
-
# Convert frame to PIL Image for compatibility with detect_objects
|
| 220 |
pil_frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 221 |
|
| 222 |
-
# Detect objects in the frame
|
| 223 |
_, _, yolo_boxes = detect_objects(pil_frame, rail_segment)
|
| 224 |
|
| 225 |
-
# Draw rail segment
|
| 226 |
pixel_segment = convert_segment_to_pixel(rail_segment, width, height)
|
| 227 |
pts = np.array(list(zip(pixel_segment[::2], pixel_segment[1::2])), np.int32)
|
| 228 |
pts = pts.reshape((-1, 1, 2))
|
| 229 |
cv2.polylines(frame, [pts], True, (0, 0, 255), 2)
|
| 230 |
|
| 231 |
-
# Check for obstructions and draw bounding boxes
|
| 232 |
for box in yolo_boxes:
|
| 233 |
class_name, x, y, w, h = box
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
|
|
|
| 246 |
|
| 247 |
out.write(frame)
|
| 248 |
|
|
@@ -312,11 +313,11 @@ class TwoStepDetection:
|
|
| 312 |
|
| 313 |
cap.release()
|
| 314 |
|
| 315 |
-
|
| 316 |
summary = f"{processing_message}\n\nObstruction Summary:\n"
|
| 317 |
summary += f"Total frames: {total_frames}\n"
|
| 318 |
summary += f"Frames with obstructions: {obstructed_frames}\n"
|
| 319 |
-
|
| 320 |
|
| 321 |
return None, video_output, summary
|
| 322 |
|
|
|
|
| 40 |
for line in f:
|
| 41 |
parts = line.strip().split()
|
| 42 |
class_id = int(parts[0])
|
| 43 |
+
if COCO_CLASSES[class_id] != 'traffic light':
|
| 44 |
class_name = COCO_CLASSES[class_id]
|
| 45 |
x, y, w, h = map(float, parts[1:5])
|
| 46 |
boxes.append((class_name, x, y, w, h))
|
|
|
|
| 88 |
labels = {'intersecting': 'Intersecting Box', 'obstructed': 'Obstructed Box', 'not touching': 'Non-interacting Box'}
|
| 89 |
|
| 90 |
for yolo_box in yolo_boxes:
|
| 91 |
+
class_name, x_center, y_center, width, height = yolo_box
|
| 92 |
+
if class_name != 'traffic light':
|
| 93 |
+
x1, y1, x2, y2 = yolo_to_pixel_coords(x_center, y_center, width, height, img_width, img_height)
|
| 94 |
+
relationship = box_segment_relationship(yolo_box, segment, img_width, img_height, threshold)
|
| 95 |
+
color = colors[relationship]
|
| 96 |
+
label = labels[relationship]
|
| 97 |
+
ax.add_patch(plt.Rectangle((x1, y1), x2-x1, y2-y1, fill=False, edgecolor=color, linewidth=2, label=label))
|
| 98 |
|
| 99 |
ax.legend()
|
| 100 |
ax.axis('off')
|
| 101 |
plt.tight_layout()
|
| 102 |
return fig
|
| 103 |
|
| 104 |
+
|
| 105 |
# COCO classes
|
| 106 |
COCO_CLASSES = [
|
| 107 |
'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
|
|
|
|
| 179 |
|
| 180 |
fig = plot_boxes_and_segment(image, yolo_boxes, rail_segment, img_width, img_height, threshold)
|
| 181 |
|
| 182 |
+
results = []
|
| 183 |
for class_name, x, y, w, h in yolo_boxes:
|
| 184 |
+
if class_name != 'traffic light':
|
| 185 |
+
result = box_segment_relationship((0, x, y, w, h), rail_segment, img_width, img_height, threshold)
|
| 186 |
+
results.append(f"{class_name} at ({x:.2f}, {y:.2f}) is {result} the segment.")
|
| 187 |
|
| 188 |
os.remove(temp_image_path)
|
| 189 |
os.remove(label_path)
|
| 190 |
|
| 191 |
return fig, "\n".join(results), yolo_boxes
|
| 192 |
|
| 193 |
+
|
| 194 |
def process_video(video_path, rail_segment, frame_skip=15):
|
| 195 |
cap = cv2.VideoCapture(video_path)
|
| 196 |
if not cap.isOpened():
|
|
|
|
| 209 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 210 |
threshold = 10 # Set threshold (in pixels) for obstruction detection
|
| 211 |
|
| 212 |
+
while True:
|
| 213 |
ret, frame = cap.read()
|
| 214 |
if not ret:
|
| 215 |
break
|
|
|
|
| 220 |
|
| 221 |
processed_count += 1
|
| 222 |
|
|
|
|
| 223 |
pil_frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 224 |
|
|
|
|
| 225 |
_, _, yolo_boxes = detect_objects(pil_frame, rail_segment)
|
| 226 |
|
|
|
|
| 227 |
pixel_segment = convert_segment_to_pixel(rail_segment, width, height)
|
| 228 |
pts = np.array(list(zip(pixel_segment[::2], pixel_segment[1::2])), np.int32)
|
| 229 |
pts = pts.reshape((-1, 1, 2))
|
| 230 |
cv2.polylines(frame, [pts], True, (0, 0, 255), 2)
|
| 231 |
|
|
|
|
| 232 |
for box in yolo_boxes:
|
| 233 |
class_name, x, y, w, h = box
|
| 234 |
+
if class_name != 'traffic light':
|
| 235 |
+
relationship = box_segment_relationship((0, x, y, w, h), rail_segment, width, height, threshold)
|
| 236 |
+
x1, y1, x2, y2 = yolo_to_pixel_coords(x, y, w, h, width, height)
|
| 237 |
+
|
| 238 |
+
if relationship == "intersecting":
|
| 239 |
+
color = (0, 0, 255) # Red for intersecting
|
| 240 |
+
elif relationship == "obstructed":
|
| 241 |
+
color = (0, 255, 255) # Yellow for obstructed
|
| 242 |
+
else:
|
| 243 |
+
color = (0, 255, 0) # Green for not touching
|
| 244 |
+
|
| 245 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
| 246 |
+
cv2.putText(frame, f"{class_name} ({relationship})", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
|
| 247 |
|
| 248 |
out.write(frame)
|
| 249 |
|
|
|
|
| 313 |
|
| 314 |
cap.release()
|
| 315 |
|
| 316 |
+
obstruction_percentage = (obstructed_frames / total_frames) * 100
|
| 317 |
summary = f"{processing_message}\n\nObstruction Summary:\n"
|
| 318 |
summary += f"Total frames: {total_frames}\n"
|
| 319 |
summary += f"Frames with obstructions: {obstructed_frames}\n"
|
| 320 |
+
summary += f"Percentage of frames with obstructions: {obstruction_percentage:.2f}%"
|
| 321 |
|
| 322 |
return None, video_output, summary
|
| 323 |
|