Martin Tomov commited on
Commit
30ce0d8
β€’
1 Parent(s): 302e52c

@spaces.GPU detectron_utils.py

Browse files
Files changed (1) hide show
  1. detectron_utils.py +3 -6
detectron_utils.py CHANGED
@@ -2,14 +2,12 @@ import torch
2
  import numpy as np
3
  import cv2
4
  from huggingface_hub import hf_hub_download
 
5
 
6
  REPO_ID = "idml/Detectron2-FasterRCNN_InsectDetect"
7
  FILENAME = "model.pth"
8
  FILENAME_CONFIG = "config.yml"
9
 
10
-
11
- # Ensure you have the model file
12
-
13
  import cv2
14
  from detectron2.config import get_cfg
15
  from detectron2.engine import DefaultPredictor
@@ -104,19 +102,18 @@ viz_classes = {'thing_classes': ['Acrididae',
104
 
105
 
106
 
 
107
  def detectron_process_image(image):
108
  cfg = get_cfg()
109
 
110
-
111
  cfg.merge_from_file(hf_hub_download(repo_id=REPO_ID, filename=FILENAME_CONFIG))
112
  cfg.MODEL.WEIGHTS = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
113
  cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.2
114
- cfg.MODEL.DEVICE='cpu'
115
  predictor = DefaultPredictor(cfg)
116
 
117
  numpy_image = np.array(image)
118
 
119
-
120
  im = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
121
 
122
  v = Visualizer(im[:, :, ::-1],
 
2
  import numpy as np
3
  import cv2
4
  from huggingface_hub import hf_hub_download
5
+ import spaces
6
 
7
  REPO_ID = "idml/Detectron2-FasterRCNN_InsectDetect"
8
  FILENAME = "model.pth"
9
  FILENAME_CONFIG = "config.yml"
10
 
 
 
 
11
  import cv2
12
  from detectron2.config import get_cfg
13
  from detectron2.engine import DefaultPredictor
 
102
 
103
 
104
 
105
+ @spaces.GPU
106
  def detectron_process_image(image):
107
  cfg = get_cfg()
108
 
 
109
  cfg.merge_from_file(hf_hub_download(repo_id=REPO_ID, filename=FILENAME_CONFIG))
110
  cfg.MODEL.WEIGHTS = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
111
  cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.2
112
+ cfg.MODEL.DEVICE = 'cpu'
113
  predictor = DefaultPredictor(cfg)
114
 
115
  numpy_image = np.array(image)
116
 
 
117
  im = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
118
 
119
  v = Visualizer(im[:, :, ::-1],