Aumkeshchy2003 commited on
Commit
caaaba0
·
verified ·
1 Parent(s): 8d4b32f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -21,33 +21,47 @@ model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(devi
21
  model.eval()
22
 
23
  def preprocess_image(image):
24
- image_tensor = F.to_tensor(image)
25
- return image_tensor.unsqueeze(0).to(device)
 
 
 
 
26
 
27
  def draw_boxes(image, outputs, threshold=0.3):
28
- image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
29
- h, w, _ = image.shape
 
30
 
31
- for box in outputs:
32
- score, label, x1, y1, x2, y2 = box[4].item(), int(box[5].item()), box[0].item(), box[1].item(), box[2].item(), box[3].item()
33
- if score > threshold:
34
- x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
35
- cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
36
- text = f"{model.names[label]:s}: {score:.2f}"
37
- cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
38
 
39
- return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
 
 
40
 
41
  def detect_objects(image):
42
  image_tensor = preprocess_image(image)
43
- outputs = model(image_tensor)
44
- outputs = non_max_suppression(outputs)[0]
45
- result_image = draw_boxes(image, outputs)
46
- return result_image
 
 
 
 
 
 
47
 
48
  iface = gr.Interface(
49
  fn=detect_objects,
50
- inputs=gr.Image(type="pil"),
51
  outputs=gr.Image(type="pil"),
52
  live=True
53
  )
 
21
  model.eval()
22
 
23
  def preprocess_image(image):
24
+ try:
25
+ image_tensor = F.to_tensor(image)
26
+ return image_tensor.unsqueeze(0).to(device)
27
+ except Exception as e:
28
+ print(f"Error in preprocessing image: {e}")
29
+ return None
30
 
31
  def draw_boxes(image, outputs, threshold=0.3):
32
+ try:
33
+ image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
34
+ h, w, _ = image.shape
35
 
36
+ for box in outputs:
37
+ score, label, x1, y1, x2, y2 = box[4].item(), int(box[5].item()), box[0].item(), box[1].item(), box[2].item(), box[3].item()
38
+ if score > threshold:
39
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
40
+ cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
41
+ text = f"{model.names[label]:s}: {score:.2f}"
42
+ cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
43
 
44
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
45
+ except Exception as e:
46
+ print(f"Error in drawing boxes: {e}")
47
+ return image
48
 
49
  def detect_objects(image):
50
  image_tensor = preprocess_image(image)
51
+ if image_tensor is None:
52
+ return image
53
+ try:
54
+ outputs = model(image_tensor)
55
+ outputs = non_max_suppression(outputs)[0]
56
+ result_image = draw_boxes(image, outputs)
57
+ return result_image
58
+ except Exception as e:
59
+ print(f"Error in detecting objects: {e}")
60
+ return image
61
 
62
  iface = gr.Interface(
63
  fn=detect_objects,
64
+ inputs=gr.Video(source="webcam", type="pil"),
65
  outputs=gr.Image(type="pil"),
66
  live=True
67
  )