Koni commited on
Commit
b81e5e4
·
1 Parent(s): c865dfa

Adding images

Browse files
Files changed (4) hide show
  1. app.py +11 -72
  2. data/forlift.jpg +0 -0
  3. data/hall.jpg +0 -0
  4. loco.yaml +7 -0
app.py CHANGED
@@ -1,18 +1,12 @@
1
  import gradio as gr
2
- # import spaces
3
- from huggingface_hub import hf_hub_download
4
  import os
 
 
 
 
5
 
6
- # make sure you have the following dependencies
7
- # import torch
8
- import numpy as np
9
- # from models.common import DetectMultiBackend
10
- # from utils.general import non_max_suppression, scale_boxes
11
- # from utils.torch_utils import select_device, smart_inference_mode
12
- # from utils.augmentations import letterbox
13
- # import PIL.Image
14
 
15
- #@smart_inference_mode()
16
  @spaces.GPU
17
  def yolov9_inference(img_path, model_id='YOLOv9-S_X_LOCO-converted.pt', img_size=640, conf_thres=0.1, iou_thres=0.4):
18
  """
@@ -27,36 +21,13 @@ def yolov9_inference(img_path, model_id='YOLOv9-S_X_LOCO-converted.pt', img_size
27
  :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
28
  """
29
 
30
- return np.array(PIL.Image.open(img_path))
31
-
32
- # # Load the model
33
- # model_path = download_models(model_id)
34
-
35
- # # Initialize
36
- # device = select_device('0')
37
- # model = DetectMultiBackend(model_path, device="0", fp16=False, data='data/coco.yaml')
38
- # stride, names, pt = model.stride, model.names, model.pt
39
-
40
- # # Load image
41
- # img = np.array(PIL.Image.open(img_path))
42
- # img = letterbox(img0, img_size, stride=stride, auto=True)[0]
43
- # img = img[:, :, ::-1].transpose(2, 0, 1)
44
- # img = np.ascontiguousarray(img)
45
- # img = torch.from_numpy(img).to(device).float()
46
- # img /= 255.0
47
- # if img.ndimension() == 3:
48
- # img = img.unsqueeze(0)
49
-
50
- # # Inference
51
- # results = model(img, augment=False, visualize=False)
52
 
53
- # # Apply NMS
54
- # results = non_max_suppression(results[0][0], conf_thres, iou_thres, classes=None, max_det=1000)
55
 
56
- # output = results.render()
57
-
58
- # return output[0]
59
 
 
60
 
61
 
62
  def download_models(model_id):
@@ -64,38 +35,6 @@ def download_models(model_id):
64
  token=os.getenv("HF_TOKEN"))
65
  return f"./{model_id}"
66
 
67
- # @spaces.GPU
68
- # def yolov9_inference(img_path, model_id, image_size, conf_threshold, iou_threshold):
69
- # """
70
- # Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
71
- # the input size and apply test time augmentation.
72
-
73
- # :param model_path: Path to the YOLOv9 model file.
74
- # :param conf_threshold: Confidence threshold for NMS.
75
- # :param iou_threshold: IoU threshold for NMS.
76
- # :param img_path: Path to the image file.
77
- # :param size: Optional, input size for inference.
78
- # :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
79
- # """
80
- # # Import YOLOv9
81
- # import yolov9
82
-
83
- # # Load the model
84
- # model_path = download_models(model_id)
85
- # model = yolov9.load(model_path, device="cuda:0")
86
-
87
- # # Set model parameters
88
- # model.conf = conf_threshold
89
- # model.iou = iou_threshold
90
-
91
- # # Perform inference
92
- # results = model(img_path, size=image_size)
93
-
94
- # # Optionally, show detection bounding boxes on image
95
- # output = results.render()
96
-
97
- # return output[0]
98
-
99
 
100
  def app():
101
  with gr.Blocks():
@@ -106,9 +45,9 @@ def app():
106
  label="Model",
107
  choices=[
108
  "YOLOv9-S_X_LOCO-converted.pt",
109
- "YOLOv9_S_X_LOCO.pt",
110
  "YOLOv9-E_X_LOCO-converted.pt",
111
- "YOLOv9_E_X_LOCO.pt",
112
  ],
113
  value="YOLOv9-S_X_LOCO-converted.pt",
114
  )
 
1
  import gradio as gr
2
+ import numpy as np
 
3
  import os
4
+ import spaces
5
+
6
+ from huggingface_hub import hf_hub_download
7
+ from ultralytics import YOLO
8
 
 
 
 
 
 
 
 
 
9
 
 
10
  @spaces.GPU
11
  def yolov9_inference(img_path, model_id='YOLOv9-S_X_LOCO-converted.pt', img_size=640, conf_thres=0.1, iou_thres=0.4):
12
  """
 
21
  :return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
22
  """
23
 
24
+ model_path = download_models(model_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ model = YOLO(model_path)
 
27
 
28
+ results = model.predict(img_path, conf=conf_thres, iou=iou_thres, imgsz=img_size)
 
 
29
 
30
+ return np.array(PIL.Image.open(img_path))
31
 
32
 
33
  def download_models(model_id):
 
35
  token=os.getenv("HF_TOKEN"))
36
  return f"./{model_id}"
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  def app():
40
  with gr.Blocks():
 
45
  label="Model",
46
  choices=[
47
  "YOLOv9-S_X_LOCO-converted.pt",
48
+ "YOLOv9-S_X_LOCO.pt",
49
  "YOLOv9-E_X_LOCO-converted.pt",
50
+ "YOLOv9-E_X_LOCO.pt",
51
  ],
52
  value="YOLOv9-S_X_LOCO-converted.pt",
53
  )
data/forlift.jpg ADDED
data/hall.jpg ADDED
loco.yaml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ nc: 5
2
+ names:
3
+ 0: small_load_carrier
4
+ 1: forklift
5
+ 2: pallet
6
+ 3: stillage
7
+ 4: pallet_truck