AMKhakbaz commited on
Commit
8c43771
·
verified ·
1 Parent(s): 970f461

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -23
app.py CHANGED
@@ -1,37 +1,24 @@
1
  # app.py for Hugging Face Spaces
2
 
3
  import gradio as gr
4
- from ultralytics import YOLO
5
- import torch
6
- import ultralytics.nn.tasks
7
- from huggingface_hub import hf_hub_download
8
  from PIL import Image
9
  import numpy as np
10
 
11
- # Patch torch_safe_load to use weights_only=False
12
- def patched_torch_safe_load(weight):
13
- try:
14
- return torch.load(weight, map_location='cpu', weights_only=False)
15
- except Exception as e:
16
- raise e
17
-
18
- ultralytics.nn.tasks.torch_safe_load = patched_torch_safe_load
19
-
20
- # Download and load the model
21
- model_path = hf_hub_download(repo_id="foduucom/product-detection-in-shelf-yolov8", filename="best.pt")
22
- model = YOLO(model_path)
23
 
24
  # Set model parameters
25
- model.conf = 0.25 # confidence threshold (note: in newer ultralytics, it's in predict)
26
- model.iou = 0.45 # IoU threshold
27
- model.agnostic_nms = False
28
- model.max_det = 1000
29
 
30
  def detect_skus(image):
31
- # image is PIL Image from Gradio
32
- results = model(image) # YOLO accepts PIL
33
 
34
- # Extract unique SKU names
35
  sku_set = set()
36
  for result in results:
37
  for box in result.boxes:
 
1
  # app.py for Hugging Face Spaces
2
 
3
  import gradio as gr
4
+ from ultralyticsplus import YOLO
 
 
 
5
  from PIL import Image
6
  import numpy as np
7
 
8
+ # Load the model
9
+ model = YOLO('foduucom/product-detection-in-shelf-yolov8')
 
 
 
 
 
 
 
 
 
 
10
 
11
  # Set model parameters
12
+ model.overrides['conf'] = 0.25 # NMS confidence threshold
13
+ model.overrides['iou'] = 0.45 # NMS IoU threshold
14
+ model.overrides['agnostic_nms'] = False # NMS class-agnostic
15
+ model.overrides['max_det'] = 1000 # maximum number of detections per image
16
 
17
  def detect_skus(image):
18
+ # Convert Gradio image to numpy array if needed, but YOLO accepts PIL
19
+ results = model(image)
20
 
21
+ # Extract unique SKU names (assuming classes are SKU names)
22
  sku_set = set()
23
  for result in results:
24
  for box in result.boxes: