VietCat Amp commited on
Commit
98a8397
·
1 Parent(s): a633bfa

Download model from HF Hub at runtime

Browse files

Amp-Thread-ID: https://ampcode.com/threads/T-22e83726-d77c-475f-b185-6d55f37c5979
Co-authored-by: Amp <amp@ampcode.com>

Files changed (2) hide show
  1. config.yaml +1 -1
  2. model.py +13 -2
config.yaml CHANGED
@@ -1,5 +1,5 @@
1
  model:
2
- path: 'VietCat/GTSRB-Model/models/GTSRB.pt' # Path to the YOLO model on Hugging Face Hub
3
  confidence_threshold: 0.5 # Minimum confidence for detections
4
 
5
  inference:
 
1
  model:
2
+ path: 'VietCat/GTSRB-Model/models/GTSRB.pt' # Path to the YOLO model on Hugging Face Hub (will be downloaded automatically)
3
  confidence_threshold: 0.5 # Minimum confidence for detections
4
 
5
  inference:
model.py CHANGED
@@ -2,13 +2,24 @@ import cv2
2
  import numpy as np
3
  from ultralytics import YOLO
4
  import yaml
 
 
5
 
6
  class TrafficSignDetector:
7
  def __init__(self, config_path):
8
  with open(config_path, 'r') as f:
9
  config = yaml.safe_load(f)
10
-
11
- self.model = YOLO(config['model']['path'])
 
 
 
 
 
 
 
 
 
12
  self.conf_threshold = config['model']['confidence_threshold']
13
  self.box_color = config['inference']['box_color']
14
  self.text_color = config['inference']['text_color']
 
2
  import numpy as np
3
  from ultralytics import YOLO
4
  import yaml
5
+ from huggingface_hub import hf_hub_download
6
+ import os
7
 
8
  class TrafficSignDetector:
9
  def __init__(self, config_path):
10
  with open(config_path, 'r') as f:
11
  config = yaml.safe_load(f)
12
+
13
+ # Download model from HF Hub if not local
14
+ model_path = config['model']['path']
15
+ if model_path.startswith('VietCat/'):
16
+ # Extract repo and file path
17
+ repo_id, file_path = model_path.split('/', 1)
18
+ local_model_path = hf_hub_download(repo_id=repo_id, filename=file_path)
19
+ self.model = YOLO(local_model_path)
20
+ else:
21
+ self.model = YOLO(model_path)
22
+
23
  self.conf_threshold = config['model']['confidence_threshold']
24
  self.box_color = config['inference']['box_color']
25
  self.text_color = config['inference']['text_color']