lopesdri commited on
Commit
247cca2
·
1 Parent(s): fe86f81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -32
app.py CHANGED
@@ -1,43 +1,32 @@
1
  import torch
2
- import cv2
3
- import numpy as np
4
  import gradio as gr
 
5
  from PIL import Image
6
 
 
 
7
 
8
- model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
 
 
 
 
9
 
 
 
10
 
11
- model.conf = 0.25
12
- model.iou = 0.45
13
- model.agnostic = False
14
- model.multi_label = False
15
- model.max_det = 1000
16
 
 
17
 
18
- def detect(img):
 
 
19
 
 
20
 
21
- results = model(img, size=640)
22
-
23
- predictions = results.pred[0]
24
- boxes = predictions[:, :4] # x1, y1, x2, y2
25
- scores = predictions[:, 4]
26
- categories = predictions[:, 5]
27
- new_image = np.squeeze(results.render())
28
-
29
- return new_image
30
-
31
-
32
- examples = ['apple_img.jpg', 'michael.gif']
33
-
34
-
35
- css = ".output-image, .input-image, .image-preview {height: 400px !important}"
36
-
37
- iface = gr.Interface(fn=detect,
38
- inputs=gr.inputs.Image(type="numpy",),
39
- outputs=gr.outputs.Image(type="numpy",),
40
- css=css,
41
- examples = examples,
42
- )
43
- iface.launch(debug=True, inline=True)
 
1
  import torch
 
 
2
  import gradio as gr
3
+ from torchvision.transforms import functional as F
4
  from PIL import Image
5
 
6
+ # Load the YOLOv8 model (assuming it is already converted to the Hugging Face format)
7
+ model = torch.hub.load('ultralytics/yolov8', 'custom', path='yolov5s.pt')
8
 
9
+ # Define the prediction function
10
+ def predict(image):
11
+ # Preprocess the input image
12
+ image_tensor = F.to_tensor(image)
13
+ image_tensor.unsqueeze_(0)
14
 
15
+ # Perform inference
16
+ results = model(image_tensor)
17
 
18
+ # Post-process the results
19
+ # Extract the bounding box coordinates and class labels
20
+ bboxes = results.xyxy[0].tolist()
21
+ labels = results.names[0]
 
22
 
23
+ return bboxes, labels
24
 
25
+ # Define the Gradio interface
26
+ inputs = gr.inputs.Image()
27
+ outputs = gr.outputs.Image()
28
 
29
+ interface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs, capture_session=True)
30
 
31
+ # Run the interface
32
+ interface.launch()