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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -22
app.py CHANGED
@@ -1,46 +1,59 @@
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")
17
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
18
  model.eval()
19
 
 
 
20
  def preprocess_image(image):
21
- image = image.convert("RGB")
22
- image_tensor = F.to_tensor(image).unsqueeze(0).to(device)
23
- return image_tensor
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:
30
  x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
31
  cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
32
- text = f"{model.names[label]}: {score:.2f}"
33
  cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
34
- return Image.fromarray(image)
 
35
 
36
  def detect_objects(image):
37
  image_tensor = preprocess_image(image)
38
  outputs = model(image_tensor)
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()
 
1
+ !pip install -U torch torchvision cython
2
+ !pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
3
+ !pip install gradio
4
+
5
  import torch
6
+ import torchvision
7
+ from torchvision.models.detection import fasterrcnn_resnet50_fpn
 
 
8
  from torchvision.transforms import functional as F
9
+ from PIL import Image
10
+ import cv2
11
+ from google.colab.patches import cv2_imshow
12
+ import gradio as gr
13
 
14
+ !git clone https://github.com/ultralytics/yolov5
15
+ %cd yolov5
16
+ !pip install -r requirements.txt
 
17
 
18
+ import torch
19
+ from yolov5.models.yolo import Model
20
 
21
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
22
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
23
  model.eval()
24
 
25
+ from yolov5.utils.general import non_max_suppression
26
+
27
  def preprocess_image(image):
28
+ image_tensor = F.to_tensor(image)
29
+ return image_tensor.unsqueeze(0).to(device)
 
30
 
31
  def draw_boxes(image, outputs, threshold=0.3):
32
+ image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
33
+ h, w, _ = image.shape
34
+
35
  for box in outputs:
36
  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()
37
  if score > threshold:
38
  x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
39
  cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
40
+ text = f"{model.names[label]:s}: {score:.2f}"
41
  cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
42
+
43
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
44
 
45
  def detect_objects(image):
46
  image_tensor = preprocess_image(image)
47
  outputs = model(image_tensor)
48
  outputs = non_max_suppression(outputs)[0]
49
+ result_image = draw_boxes(image, outputs)
50
+ return result_image
51
 
52
+ iface = gr.Interface(
53
+ fn=detect_objects,
54
+ inputs=gr.inputs.Image(type="pil"),
55
+ outputs=gr.outputs.Image(type="pil"),
56
+ live=True
57
+ )
58
 
59
+ iface.launch()