claustrophobic commited on
Commit
cabbee9
·
verified ·
1 Parent(s): f03b618

scorevision: push artifact

Browse files
Files changed (1) hide show
  1. miner.py +90 -0
miner.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ from pathlib import Path
4
+ from typing import List, Tuple
5
+ import sys
6
+ import os
7
+
8
+ from numpy import ndarray
9
+ from pydantic import BaseModel
10
+
11
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
12
+
13
+ # from inference import predict_batch, load_model
14
+ import importlib.util
15
+ from pathlib import Path
16
+
17
+
18
+ def manual_import(name, filename):
19
+ """
20
+ Manually loads a module (.so, .pyc, or .py) from a specific file path,
21
+ bypassing sys.meta_path import hooks.
22
+ """
23
+ # Locate the file relative to the current miner.py
24
+ curr_dir = Path(__file__).parent
25
+ file_path = curr_dir / filename
26
+
27
+ if not file_path.exists():
28
+ raise FileNotFoundError(f"Could not find {file_path}")
29
+
30
+ # Load the spec directly from the file path
31
+ spec = importlib.util.spec_from_file_location(name, file_path)
32
+ if spec is None:
33
+ raise ImportError(f"Could not load spec for {name} from {file_path}")
34
+
35
+ # Create the module and register it in sys.modules
36
+ module = importlib.util.module_from_spec(spec)
37
+
38
+ # Execute the module
39
+ spec.loader.exec_module(module)
40
+ return module
41
+
42
+
43
+ # import inference
44
+
45
+
46
+ class BoundingBox(BaseModel):
47
+ x1: int
48
+ y1: int
49
+ x2: int
50
+ y2: int
51
+ cls_id: int
52
+ conf: float
53
+
54
+
55
+ class TVFrameResult(BaseModel):
56
+ frame_id: int
57
+ boxes: List[BoundingBox]
58
+ keypoints: List[Tuple[int, int]]
59
+
60
+
61
+ class Miner:
62
+ def __init__(self, path_hf_repo):
63
+ print("model loading")
64
+ self.health = "Okay"
65
+ self.inference = None
66
+ self.path_hf_repo = path_hf_repo
67
+ self.is_start = False
68
+
69
+ def __repr__(self) -> str:
70
+ return self.health
71
+
72
+ def predict_batch(self, batch_images, offset, n_keypoints):
73
+ if self.is_start == False:
74
+ self.is_start = True
75
+ return None
76
+ if self.inference is None:
77
+ print("importing inference module")
78
+ # self.inference = manual_import("inference", "inference.py")
79
+ self.inference = manual_import(
80
+ "inference", "inference.cpython-312-x86_64-linux-gnu.so"
81
+ )
82
+ print("inference module loading...")
83
+ self.inference.load_model(self.path_hf_repo)
84
+ print("inference module imported")
85
+ results = self.inference.predict_batch(
86
+ batch_images,
87
+ offset,
88
+ n_keypoints,
89
+ )
90
+ return results