Aumkeshchy2003 commited on
Commit
eaa57e7
·
verified ·
1 Parent(s): 8378a4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -25
app.py CHANGED
@@ -2,40 +2,67 @@ import torch
2
  import numpy as np
3
  import gradio as gr
4
  from PIL import Image
 
5
 
6
  # Device configuration
7
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8
 
9
- # Load YOLOv5s model (smallest and fastest variant)
10
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
11
 
12
- # Enable half-precision for CUDA devices
 
13
  if device.type == 'cuda':
14
- model.half()
15
 
16
- def detect_objects(image):
17
- # Convert numpy array to PIL Image
18
- image_pil = Image.fromarray(image)
 
19
 
20
- # Perform inference without gradient calculation
21
- with torch.no_grad():
22
- results = model(image_pil)
 
 
 
 
 
 
 
 
23
 
24
- # Render detections using optimized YOLOv5 method
25
- rendered_images = results.render()
26
-
27
- # Return the first rendered image as numpy array
28
- return np.array(rendered_images[0]) if rendered_images else image
29
 
30
- # Gradio interface
31
- iface = gr.Interface(
32
- fn=detect_objects,
33
- inputs=gr.Image(type="numpy", label="Upload Image"),
34
- outputs=gr.Image(type="numpy", label="Detected Objects"),
35
- title="High-Speed Object Detection with YOLOv5s",
36
- description="Optimized for speed using YOLOv5s and GPU acceleration.",
37
- allow_flagging="never",
38
- examples=["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
39
- )
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- iface.launch()
 
 
 
 
 
 
 
2
  import numpy as np
3
  import gradio as gr
4
  from PIL import Image
5
+ import time
6
 
7
  # Device configuration
8
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
 
10
+ # Load optimized YOLOv5s model
11
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
12
 
13
+ # Performance optimizations
14
+ model.conf = 0.5 # Confidence threshold (adjust for speed/accuracy balance)
15
  if device.type == 'cuda':
16
+ model.half() # FP16 precision
17
 
18
+ def process_frame(image):
19
+ """Process single frame with error handling"""
20
+ if image is None:
21
+ return None
22
 
23
+ try:
24
+ # Convert numpy array to PIL Image
25
+ image_pil = Image.fromarray(image)
26
+
27
+ # Perform inference
28
+ with torch.no_grad():
29
+ results = model(image_pil)
30
+
31
+ # Render results
32
+ rendered_images = results.render()
33
+ return np.array(rendered_images[0]) if rendered_images else image
34
 
35
+ except Exception as e:
36
+ print(f"Processing error: {e}")
37
+ return image
 
 
38
 
39
+ with gr.Blocks(title="Real-Time Object Detection") as app:
40
+ gr.Markdown("# 🚀 Real-Time Object Detection with Dual Input")
41
+ gr.Markdown("Supports live webcam streaming and image uploads")
42
+
43
+ with gr.Tabs():
44
+ with gr.TabItem("📷 Live Camera"):
45
+ with gr.Row():
46
+ webcam_input = gr.Webcam(label="Live Feed", streaming=True)
47
+ live_output = gr.Image(label="Processed Feed", streaming=True)
48
+ webcam_input.change(process_frame, webcam_input, live_output)
49
+
50
+ with gr.TabItem("🖼️ Image Upload"):
51
+ with gr.Row():
52
+ upload_input = gr.Image(type="numpy", label="Upload Image")
53
+ upload_output = gr.Image(label="Detection Result")
54
+ upload_input.change(process_frame, upload_input, upload_output)
55
+
56
+ gr.Markdown("### ⚙️ Performance Settings")
57
+ with gr.Accordion("Advanced Settings", open=False):
58
+ gr.Slider(minimum=0.1, maximum=0.9, value=0.5,
59
+ label="Confidence Threshold", interactive=True)
60
+ gr.Checkbox(label="Enable FP16 Acceleration", value=True)
61
 
62
+ # Configure queue and launch
63
+ app.queue(concurrency_count=4, max_size=20).launch(
64
+ server_name="0.0.0.0",
65
+ server_port=7860,
66
+ share=False,
67
+ enable_queue=True
68
+ )