Aumkeshchy2003 commited on
Commit
46e3370
·
verified ·
1 Parent(s): 7894411

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
app.py CHANGED
@@ -1,38 +1,45 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from fastapi.responses import JSONResponse
3
  import torch
 
 
4
  from PIL import Image
5
  from torchvision.transforms import functional as F
6
- from yolov5.models.yolo import Model
7
  from yolov5.utils.general import non_max_suppression
8
 
9
- app = FastAPI()
10
-
11
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
13
  model.eval()
14
 
15
  def preprocess_image(image):
16
- image_tensor = F.to_tensor(image)
17
- return image_tensor.unsqueeze(0).to(device)
 
18
 
19
- def draw_boxes(outputs, threshold=0.3):
20
- boxes = []
 
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:
24
- boxes.append({
25
- "label": model.names[label],
26
- "score": score,
27
- "box": [x1, y1, x2, y2]
28
- })
29
- return boxes
30
 
31
- @app.post("/predict/")
32
- async def predict(file: UploadFile = File(...)):
33
- image = Image.open(file.file)
34
  image_tensor = preprocess_image(image)
35
  outputs = model(image_tensor)
36
  outputs = non_max_suppression(outputs)[0]
37
- boxes = draw_boxes(outputs)
38
- return JSONResponse(content={"boxes": boxes})
 
 
 
 
 
 
 
 
 
 
 
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")
 
 
10
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
11
  model.eval()
12
 
13
  def preprocess_image(image):
14
+ image = image.convert("RGB")
15
+ image_tensor = F.to_tensor(image).unsqueeze(0).to(device)
16
+ return image_tensor
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:
24
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
25
+ cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
26
+ text = f"{model.names[label]}: {score:.2f}"
27
+ cv2.putText(image, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
28
+ return Image.fromarray(image)
 
29
 
30
+ def detect_objects(image):
 
 
31
  image_tensor = preprocess_image(image)
32
  outputs = model(image_tensor)
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()