Spaces:
Sleeping
Sleeping
Download app.py from romiioooz/object_detection: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/romiioooz/object_detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/romiioooz/object_detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/romiioooz/object_detection/resolve/main/app.py
1.46 kB
| import gradio as gr | |
| from ultralytics import YOLO | |
| import tempfile | |
| import cv2 | |
| import os | |
| # ๐ง ุชุญู ูู ุงูู ูุฏูู | |
| model = YOLO("best.pt") | |
| # ๐งฉ ุฏุงูุฉ ู ุนุงูุฌุฉ ุงูููุฏูู | |
| def detect_video(video_path): | |
| try: | |
| # ๐ ููุชุญ ุงูููุฏูู | |
| cap = cv2.VideoCapture(video_path) | |
| fps = cap.get(cv2.CAP_PROP_FPS) | |
| width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
| height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
| # ๐ ูุฌูุฒ ุงูููุฏูู ุงููุงุชุฌ | |
| temp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name | |
| out = cv2.VideoWriter(temp_out, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height)) | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| # ๐ ูุนู ู detection ุนูู ุงููุฑูู | |
| results = model(frame, verbose=False) | |
| annotated_frame = results[0].plot() | |
| out.write(annotated_frame) | |
| cap.release() | |
| out.release() | |
| return temp_out | |
| except Exception as e: | |
| return f"Error: {e}" | |
| # ๐จ ูุงุฌูุฉ Gradio | |
| demo = gr.Interface( | |
| fn=detect_video, | |
| inputs=gr.Video(label="๐ฅ Upload a video"), | |
| outputs=gr.Video(label="๐ฆ Detected video"), | |
| title="YOLO Object Detection Demo ๐", | |
| description="Upload a video and see YOLO detect objects frame by frame!" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |