Aumkeshchy2003 commited on
Commit
b86c5b1
·
verified ·
1 Parent(s): a32f6c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -24
app.py CHANGED
@@ -2,12 +2,11 @@ import torch
2
  import numpy as np
3
  import gradio as gr
4
  from PIL import Image
5
- import cv2
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
  # Set model confidence threshold
@@ -16,7 +15,7 @@ if device.type == 'cuda':
16
  model.half()
17
 
18
  def process_frame(image):
19
- """Processes a frame from the webcam and applies YOLOv5 object detection."""
20
  if image is None:
21
  return None
22
 
@@ -32,35 +31,17 @@ def process_frame(image):
32
  print(f"Error processing frame: {e}")
33
  return image
34
 
35
- def process_uploaded_image(image):
36
- """Processes an uploaded image and applies YOLOv5 object detection."""
37
- if image is None:
38
- return None
39
-
40
- try:
41
- image_pil = Image.fromarray(image)
42
- with torch.no_grad():
43
- results = model(image_pil)
44
-
45
- rendered_images = results.render()
46
- return np.array(rendered_images[0]) if rendered_images else image
47
-
48
- except Exception as e:
49
- print(f"Error processing image: {e}")
50
- return image
51
-
52
  # Create Gradio UI
53
  with gr.Blocks(title="Real-Time Object Detection") as app:
54
- gr.Markdown("# Real-Time Object Detection with Dual Input")
55
 
56
  with gr.Tabs():
57
  # 📷 Live Webcam Tab
58
  with gr.TabItem("📷 Live Camera"):
59
  with gr.Row():
60
- webcam_input = gr.Image(source="webcam", label="Live Feed")
61
  live_output = gr.Image(label="Processed Feed")
62
 
63
- # Process frame when new frame is available
64
  webcam_input.change(process_frame, inputs=webcam_input, outputs=live_output)
65
 
66
  # 🖼️ Image Upload Tab (With Submit Button)
@@ -70,6 +51,6 @@ with gr.Blocks(title="Real-Time Object Detection") as app:
70
  submit_button = gr.Button("Submit")
71
  upload_output = gr.Image(label="Detection Result")
72
 
73
- submit_button.click(process_uploaded_image, inputs=upload_input, outputs=upload_output)
74
 
75
  app.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
 
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 YOLOv5 model
10
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
11
 
12
  # Set model confidence threshold
 
15
  model.half()
16
 
17
  def process_frame(image):
18
+ """Process a video frame or image and apply YOLOv5 object detection."""
19
  if image is None:
20
  return None
21
 
 
31
  print(f"Error processing frame: {e}")
32
  return image
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # Create Gradio UI
35
  with gr.Blocks(title="Real-Time Object Detection") as app:
36
+ gr.Markdown("# Real-Time Object Detection")
37
 
38
  with gr.Tabs():
39
  # 📷 Live Webcam Tab
40
  with gr.TabItem("📷 Live Camera"):
41
  with gr.Row():
42
+ webcam_input = gr.Video(label="Live Feed")
43
  live_output = gr.Image(label="Processed Feed")
44
 
 
45
  webcam_input.change(process_frame, inputs=webcam_input, outputs=live_output)
46
 
47
  # 🖼️ Image Upload Tab (With Submit Button)
 
51
  submit_button = gr.Button("Submit")
52
  upload_output = gr.Image(label="Detection Result")
53
 
54
+ submit_button.click(process_frame, inputs=upload_input, outputs=upload_output)
55
 
56
  app.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)