Aumkeshchy2003 commited on
Commit
b31672b
·
verified ·
1 Parent(s): 93fb6ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -6,7 +6,7 @@ from ultralytics import YOLO
6
  import threading
7
  import time
8
 
9
- # Load YOLOv5 model (optimized for CUDA if available)
10
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
11
  model = YOLO("yolov5s.pt").to(device)
12
 
@@ -16,42 +16,46 @@ colors = np.random.randint(0, 255, size=(num_classes, 3), dtype=np.uint8)
16
 
17
  def detect_objects(image):
18
  """Detect objects in an uploaded image with different bounding box colors."""
19
- results = model(image)
 
20
  detections = results[0].boxes.data.cpu().numpy() # Get detections
21
 
22
  for box in detections:
23
- x1, y1, x2, y2, conf, cls = map(int, box[:6])
 
24
  label = f"{model.names[cls]} {conf:.2f}"
25
- color = tuple(map(int, colors[cls])) # Assign unique color based on class
26
 
27
- cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
28
- cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
29
 
30
- return image
31
 
32
  # Real-time webcam processing
33
- cap = cv2.VideoCapture(0) # Capture from webcam
34
  frame = None
35
  lock = threading.Lock()
36
 
37
  def process_webcam():
38
  """Continuously capture and process frames from the webcam."""
39
  global frame
40
- while True:
41
  ret, img = cap.read()
42
  if not ret:
43
  continue
44
 
45
- results = model(img)
 
46
  detections = results[0].boxes.data.cpu().numpy()
47
 
48
  for box in detections:
49
- x1, y1, x2, y2, conf, cls = map(int, box[:6])
 
50
  label = f"{model.names[cls]} {conf:.2f}"
51
  color = tuple(map(int, colors[cls])) # Assign unique color
52
 
53
- cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
54
- cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
55
 
56
  with lock:
57
  frame = img
@@ -71,9 +75,12 @@ with gr.Blocks() as demo:
71
  with gr.Tabs():
72
  with gr.Tab("Real-Time Webcam"):
73
  webcam_output = gr.Image(label="Live Webcam Feed")
 
74
  def update_webcam():
75
  while True:
76
- webcam_output.update(get_webcam_frame())
 
 
77
  time.sleep(1/30) # ~30 FPS
78
 
79
  threading.Thread(target=update_webcam, daemon=True).start()
 
6
  import threading
7
  import time
8
 
9
+ # Load YOLOv5 model
10
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
11
  model = YOLO("yolov5s.pt").to(device)
12
 
 
16
 
17
  def detect_objects(image):
18
  """Detect objects in an uploaded image with different bounding box colors."""
19
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
20
+ results = model.predict(image) # Explicitly call predict
21
  detections = results[0].boxes.data.cpu().numpy() # Get detections
22
 
23
  for box in detections:
24
+ x1, y1, x2, y2, conf, cls = box[:6]
25
+ cls = int(cls) # Convert class to int
26
  label = f"{model.names[cls]} {conf:.2f}"
27
+ color = tuple(map(int, colors[cls])) # Assign unique color
28
 
29
+ cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
30
+ cv2.putText(image, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
31
 
32
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert back to RGB for Gradio
33
 
34
  # Real-time webcam processing
35
+ cap = cv2.VideoCapture(0)
36
  frame = None
37
  lock = threading.Lock()
38
 
39
  def process_webcam():
40
  """Continuously capture and process frames from the webcam."""
41
  global frame
42
+ while cap.isOpened():
43
  ret, img = cap.read()
44
  if not ret:
45
  continue
46
 
47
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
48
+ results = model.predict(img) # Explicitly call predict
49
  detections = results[0].boxes.data.cpu().numpy()
50
 
51
  for box in detections:
52
+ x1, y1, x2, y2, conf, cls = box[:6]
53
+ cls = int(cls) # Convert class to int
54
  label = f"{model.names[cls]} {conf:.2f}"
55
  color = tuple(map(int, colors[cls])) # Assign unique color
56
 
57
+ cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
58
+ cv2.putText(img, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
59
 
60
  with lock:
61
  frame = img
 
75
  with gr.Tabs():
76
  with gr.Tab("Real-Time Webcam"):
77
  webcam_output = gr.Image(label="Live Webcam Feed")
78
+
79
  def update_webcam():
80
  while True:
81
+ with lock:
82
+ img = frame if frame is not None else np.zeros((480, 640, 3), dtype=np.uint8)
83
+ webcam_output.update(img)
84
  time.sleep(1/30) # ~30 FPS
85
 
86
  threading.Thread(target=update_webcam, daemon=True).start()