Nawinkumar15 commited on
Commit
dbca85f
Β·
verified Β·
1 Parent(s): 4da2efa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -45
app.py CHANGED
@@ -1,73 +1,62 @@
1
  import gradio as gr
2
  from ultralytics import YOLO
3
- import cv2
4
  import tempfile
5
- import numpy as np
6
 
7
- # Load YOLOv8 model once
8
- model = YOLO("best.pt")
 
9
 
10
- # πŸ“Έ Image prediction
11
  def predict_image(image):
12
- results = model.predict(image, imgsz=320, conf=0.4, verbose=False)
13
  return results[0].plot()
14
 
15
- # πŸŽ₯ Video file prediction
16
  def predict_video(video_path):
 
 
 
17
  cap = cv2.VideoCapture(video_path)
18
- fps = cap.get(cv2.CAP_PROP_FPS)
19
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) // 2)
20
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) // 2)
21
 
 
22
  temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
23
- out = cv2.VideoWriter(temp_output.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
24
-
25
- while True:
26
- ret, frame = cap.read()
27
- if not ret:
28
- break
29
- frame = cv2.resize(frame, (width, height))
30
- results = model.predict(frame, imgsz=320, conf=0.4, verbose=False)
31
- annotated = results[0].plot()
 
 
 
32
  out.write(annotated)
33
 
34
  cap.release()
35
  out.release()
36
  return temp_output.name
37
 
38
- # πŸ”΄ Live webcam stream detection
39
- def live_feed():
40
- cap = cv2.VideoCapture(0)
41
- while True:
42
- ret, frame = cap.read()
43
- if not ret:
44
- break
45
- frame = cv2.resize(frame, (640, 480))
46
- results = model.predict(frame, imgsz=320, conf=0.4, verbose=False)
47
- annotated = results[0].plot()
48
- yield annotated
49
- cap.release()
50
-
51
- # πŸš€ Gradio App
52
  with gr.Blocks() as demo:
53
- gr.Markdown("# πŸ” YOLOv8 Object Detection")
54
- gr.Markdown("Upload an image or video, or use your webcam for real-time detection.")
55
 
56
- with gr.Tab("πŸ–ΌοΈ Image Detection"):
57
  img_input = gr.Image(type="pil")
58
- img_output = gr.Image()
59
- img_btn = gr.Button("Detect Objects")
60
  img_btn.click(predict_image, inputs=img_input, outputs=img_output)
61
 
62
- with gr.Tab("🎞️ Video Detection"):
63
  vid_input = gr.Video()
64
  vid_output = gr.Video()
65
- vid_btn = gr.Button("Detect in Video")
66
  vid_btn.click(predict_video, inputs=vid_input, outputs=vid_output)
67
 
68
- with gr.Tab("πŸ“· Live Webcam"):
69
- gr.Markdown("Note: Webcam streaming may not work on Hugging Face Spaces.")
70
- live_img = gr.Image(streaming=True)
71
- live_img.stream(live_feed)
72
-
73
  demo.launch()
 
1
  import gradio as gr
2
  from ultralytics import YOLO
 
3
  import tempfile
4
+ import cv2
5
 
6
+ # βœ… Load YOLOv8 model once on GPU (if available)
7
+ model = YOLO("best.pt")
8
+ model.to("cuda" if model.device.type == "cpu" else model.device)
9
 
10
+ # βœ… Inference on image
11
  def predict_image(image):
12
+ results = model.predict(image, imgsz=640, conf=0.5, verbose=False)
13
  return results[0].plot()
14
 
15
+ # βœ… Optimized video detection
16
  def predict_video(video_path):
17
+ import os
18
+
19
+ # Open input video
20
  cap = cv2.VideoCapture(video_path)
21
+ fps = cap.get(cv2.CAP_PROP_FPS) or 25
22
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5)
23
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)
24
 
25
+ # Create output video file
26
  temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
27
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
28
+ out = cv2.VideoWriter(temp_output.name, fourcc, fps, (width, height))
29
+
30
+ # Efficient streaming-based inference
31
+ for result in model.track(source=video_path, stream=True, imgsz=480, conf=0.5, verbose=False):
32
+ if result.orig_img is None:
33
+ continue
34
+ # Resize original image
35
+ frame = cv2.resize(result.orig_img, (width, height))
36
+ # Draw annotations
37
+ annotated = result.plot()
38
+ # Write to output video
39
  out.write(annotated)
40
 
41
  cap.release()
42
  out.release()
43
  return temp_output.name
44
 
45
+ # βœ… Gradio UI
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  with gr.Blocks() as demo:
47
+ gr.Markdown("# πŸš€ YOLOv8 Fast Detection\nOptimized for Speed on Images & Videos")
 
48
 
49
+ with gr.Tab("Image"):
50
  img_input = gr.Image(type="pil")
51
+ img_output = gr.Image(label="Detected")
52
+ img_btn = gr.Button("Run Detection")
53
  img_btn.click(predict_image, inputs=img_input, outputs=img_output)
54
 
55
+ with gr.Tab("Video"):
56
  vid_input = gr.Video()
57
  vid_output = gr.Video()
58
+ vid_btn = gr.Button("Run Detection on Video")
59
  vid_btn.click(predict_video, inputs=vid_input, outputs=vid_output)
60
 
61
+ # βœ… Launch Gradio
 
 
 
 
62
  demo.launch()