Aumkeshchy2003 commited on
Commit
dc80d48
·
verified ·
1 Parent(s): 234adab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
app.py CHANGED
@@ -1,51 +1,45 @@
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 ultralytics.yolo.utils.ops import non_max_suppression
8
- from ultralytics.yolo.engine.model import Model
9
-
10
 
11
  # Load YOLOv5 model
12
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
13
- model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
 
14
  model.eval()
15
 
16
  def preprocess_image(image):
17
  image = Image.fromarray(image)
18
- image_tensor = F.to_tensor(image).unsqueeze(0).to(device)
19
- return image_tensor
20
 
21
- def draw_boxes(image, outputs, threshold=0.3):
22
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
23
- h, w, _ = image.shape
24
 
25
- for box in outputs:
26
- 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()
27
- if score > threshold:
28
- x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
29
- cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
30
- text = f"{model.names[label]}: {score:.2f}"
31
- cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
32
 
33
- return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
 
 
34
 
35
- def detect_objects(image):
36
- image_tensor = preprocess_image(image)
37
- outputs = model(image_tensor)
38
- outputs = non_max_suppression(outputs)[0]
39
- result_image = draw_boxes(image, outputs.cpu().numpy())
40
- return result_image
41
 
 
42
  iface = gr.Interface(
43
  fn=detect_objects,
44
  inputs=gr.Image(type="numpy"),
45
  outputs=gr.Image(type="numpy"),
46
- title="YOLOv5 Object Detection",
47
- description="Upload an image to detect objects using the YOLOv5 model."
48
  )
49
 
50
- if __name__ == "__main__":
51
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
1
+ from ultralytics import YOLO # Use Ultralytics' YOLO module
2
  import torch
3
  import cv2
4
  import numpy as np
5
+ import gradio as gr
6
  from PIL import Image
 
 
 
 
7
 
8
  # Load YOLOv5 model
9
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
+ model = YOLO("yolov5s.pt") # Load pre-trained YOLOv5s model
11
+ model.to(device)
12
  model.eval()
13
 
14
  def preprocess_image(image):
15
  image = Image.fromarray(image)
16
+ image = image.convert("RGB")
17
+ return image
18
 
19
+ def detect_objects(image):
20
+ image = preprocess_image(image)
21
+ results = model.predict(image) # Run YOLOv5 inference
22
 
23
+ # Convert results to bounding box format
24
+ detections = []
25
+ for result in results:
26
+ for box in result.boxes.xyxy:
27
+ x1, y1, x2, y2 = map(int, box[:4])
28
+ detections.append([x1, y1, x2, y2])
 
29
 
30
+ # Draw bounding boxes
31
+ image = np.array(image)
32
+ for x1, y1, x2, y2 in detections:
33
+ cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
34
 
35
+ return image
 
 
 
 
 
36
 
37
+ # Gradio UI
38
  iface = gr.Interface(
39
  fn=detect_objects,
40
  inputs=gr.Image(type="numpy"),
41
  outputs=gr.Image(type="numpy"),
42
+ live=True,
 
43
  )
44
 
45
+ iface.launch()