Aumkeshchy2003 commited on
Commit
936b8a6
·
verified ·
1 Parent(s): b21ddec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -1,9 +1,16 @@
1
- import gradio as gr
2
  import torch
 
3
  import cv2
4
  import numpy as np
5
  from PIL import Image
6
  from torchvision.transforms import functional as F
 
 
 
 
 
 
7
  from yolov5.utils.general import non_max_suppression
8
 
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -17,7 +24,6 @@ def preprocess_image(image):
17
 
18
  def draw_boxes(image, outputs, threshold=0.3):
19
  image = np.array(image)
20
- h, w, _ = image.shape
21
  for box in outputs:
22
  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()
23
  if score > threshold:
@@ -33,13 +39,8 @@ def detect_objects(image):
33
  outputs = non_max_suppression(outputs)[0]
34
  return draw_boxes(image, outputs)
35
 
36
- iface = gr.Interface(
37
- fn=detect_objects,
38
- inputs=gr.Image(type="pil"),
39
- outputs=gr.Image(type="pil"),
40
- title="YOLO Object Detector",
41
- description="Upload an image to detect objects using YOLOv5."
42
- )
43
 
44
  if __name__ == "__main__":
45
  iface.launch()
 
1
+ import os
2
  import torch
3
+ import gradio as gr
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
7
  from torchvision.transforms import functional as F
8
+
9
+ # Clone yolov5 if not present
10
+ if not os.path.exists("yolov5"):
11
+ os.system("git clone https://github.com/ultralytics/yolov5.git")
12
+ os.system("pip install -r yolov5/requirements.txt")
13
+
14
  from yolov5.utils.general import non_max_suppression
15
 
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
24
 
25
  def draw_boxes(image, outputs, threshold=0.3):
26
  image = np.array(image)
 
27
  for box in outputs:
28
  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()
29
  if score > threshold:
 
39
  outputs = non_max_suppression(outputs)[0]
40
  return draw_boxes(image, outputs)
41
 
42
+ iface = gr.Interface(fn=detect_objects, inputs=gr.Image(type="pil"), outputs=gr.Image(type="pil"),
43
+ title="YOLO Object Detector", description="Upload an image to detect objects using YOLOv5.")
 
 
 
 
 
44
 
45
  if __name__ == "__main__":
46
  iface.launch()