gpbhupinder commited on
Commit
3e3ae52
·
verified ·
1 Parent(s): cd3f1ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -19
app.py CHANGED
@@ -1,26 +1,44 @@
1
- ### app.py
2
- import torch
3
- import requests
4
- from PIL import Image
5
- from torchvision import transforms
6
  import gradio as gr
7
- model = torch.hub.load('https://huggingface.co/spaces/gpbhupinder/test/blob/main/model_-%2023%20june%202024%2019_22.pt', pretrained=True).eval()
8
 
 
9
 
10
- # Download human-readable labels for ImageNet.
11
- response = requests.get("https://git.io/JJkYN")
12
- labels = response.text.split("\n")
13
 
14
- def predict(inp):
15
- inp = transforms.ToTensor()(inp).unsqueeze(0)
16
- with torch.no_grad():
17
- prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
18
- confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
19
- return confidences
20
 
 
 
 
 
 
 
 
 
 
 
21
 
 
 
 
22
 
23
- gr.Interface(fn=predict,
24
- inputs=gr.Image(type="pil"),
25
- outputs=gr.Label(num_top_classes=3),
26
- examples=["lion.jpg", "cheetah.jpg"]).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import PIL.Image as Image
3
 
4
+ from ultralytics import ASSETS, YOLO
5
 
6
+ model = YOLO("yolov8n.pt")
 
 
7
 
 
 
 
 
 
 
8
 
9
+ def predict_image(img, conf_threshold, iou_threshold):
10
+ """Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
11
+ results = model.predict(
12
+ source=img,
13
+ conf=conf_threshold,
14
+ iou=iou_threshold,
15
+ show_labels=True,
16
+ show_conf=True,
17
+ imgsz=640,
18
+ )
19
 
20
+ for r in results:
21
+ im_array = r.plot()
22
+ im = Image.fromarray(im_array[..., ::-1])
23
 
24
+ return im
25
+
26
+
27
+ iface = gr.Interface(
28
+ fn=predict_image,
29
+ inputs=[
30
+ gr.Image(type="pil", label="Upload Image"),
31
+ gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
32
+ gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
33
+ ],
34
+ outputs=gr.Image(type="pil", label="Result"),
35
+ title="Ultralytics Gradio",
36
+ description="Upload images for inference. The Ultralytics YOLOv8n model is used by default.",
37
+ examples=[
38
+ [ASSETS / "bus.jpg", 0.25, 0.45],
39
+ [ASSETS / "zidane.jpg", 0.25, 0.45],
40
+ ],
41
+ )
42
+
43
+ if __name__ == "__main__":
44
+ iface.launch()