Spaces:
Sleeping
Sleeping
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import gradio as grimport cv2from ultralytics import YOLO
# Load your custom trained modelmodel = YOLO("best.pt")
def detect(frame): # If the frame is empty (can happen during stream initialization), return None if frame is None: return None, 0
results = model(frame, conf=0.4) pothole_count = 0
for r in results: if r.boxes is not None: pothole_count += len(r.boxes)
for box in r.boxes: # Get coordinates and draw bounding box x1, y1, x2, y2 = map(int, box.xyxy[0]) cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# You can keep the text on the video if you want, or remove this part cv2.putText(frame, f"Potholes: {pothole_count}", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
# RETURN BOTH: The modified image, and the total count return frame, pothole_count
# Use Gradio Blocks for a cleaner, modern interfacewith gr.Blocks() as demo: gr.Markdown("# Live Pothole Detection") gr.Markdown("Using YOLOv8 to detect potholes in real-time.") with gr.Row(): with gr.Column(): # Input: Modern Gradio 4.x syntax for webcam streaming input_video = gr.Image(sources=["webcam"], streaming=True, label="Webcam Feed") with gr.Column(): # Outputs: The processed video and the live counter output_video = gr.Image(label="Processed Feed") live_count = gr.Number(label="Live Pothole Count", value=0, interactive=False)
# The .stream() method runs the function continuously as new frames come in input_video.stream( fn=detect, inputs=[input_video], outputs=[output_video, live_count] )
if __name__ == "__main__": demo.launch()app.pyAdded+56