thienphuc12339 commited on
Commit
da702e3
·
verified ·
1 Parent(s): a2caa92

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +31 -16
inference.py CHANGED
@@ -6,10 +6,11 @@ import cv2
6
  import numpy as np
7
  from pathlib import Path
8
  import time
 
9
 
10
  from configs import ModelConfig, InferenceConfig
11
  from tools.models import load_pipeline
12
- from utils import POSE_BASED_MODELS
13
  from data import Arm, get_sample_timestamp, ok_to_get_frame
14
  from visualization.utils import draw_text_on_image
15
  from tools.models import Predictions
@@ -17,12 +18,12 @@ from tools.models import Predictions
17
  def inference(model_config: ModelConfig, inference_config: InferenceConfig, pipeline) -> dict:
18
  """
19
  Thực hiện quá trình suy luận trên video.
20
-
21
  Args:
22
  model_config (ModelConfig): Cấu hình mô hình.
23
  inference_config (InferenceConfig): Cấu hình suy luận.
24
- pipeline: Pipeline đã được tải.
25
-
26
  Returns:
27
  dict: Kết quả nhận diện.
28
  """
@@ -146,20 +147,34 @@ def inference(model_config: ModelConfig, inference_config: InferenceConfig, pipe
146
  )
147
  if left_arm_ok_to_get_frame or right_arm_ok_to_get_frame:
148
  predictions = Predictions()
149
- data.append(detection_results.pose_landmarks if inference_config.use_pose_model else frame)
150
-
151
- # Calculate the start and end time of sign
152
- start_time, end_time = get_sample_timestamp(left_arm, right_arm)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
- # Convert from miliseconds to seconds
155
- start_time /= 1_000
156
- end_time /= 1_000
157
 
158
- if start_time != 0 and end_time != 0:
159
- # Run inference
160
- start_inference_time = time.time()
161
- predictions = Predictions(predictions=pipeline(np.array(data)))
162
- predictions.inference_time = time.time() - start_inference_time
163
 
164
  predictions.start_time = start_time
165
  predictions.end_time = end_time
 
6
  import numpy as np
7
  from pathlib import Path
8
  import time
9
+ import onnxruntime as ort
10
 
11
  from configs import ModelConfig, InferenceConfig
12
  from tools.models import load_pipeline
13
+ from utils.constants import POSE_BASED_MODELS
14
  from data import Arm, get_sample_timestamp, ok_to_get_frame
15
  from visualization.utils import draw_text_on_image
16
  from tools.models import Predictions
 
18
  def inference(model_config: ModelConfig, inference_config: InferenceConfig, pipeline) -> dict:
19
  """
20
  Thực hiện quá trình suy luận trên video.
21
+
22
  Args:
23
  model_config (ModelConfig): Cấu hình mô hình.
24
  inference_config (InferenceConfig): Cấu hình suy luận.
25
+ pipeline: Pipeline hoặc ONNX InferenceSession đã được tải.
26
+
27
  Returns:
28
  dict: Kết quả nhận diện.
29
  """
 
147
  )
148
  if left_arm_ok_to_get_frame or right_arm_ok_to_get_frame:
149
  predictions = Predictions()
150
+ if model_config.pretrained.endswith(".onnx"):
151
+ # Sử dụng ONNX Runtime để suy luận
152
+ # Chuyển đổi dữ liệu thành định dạng phù hợp với mô hình ONNX
153
+ input_data = np.array(data).astype(np.float32) # Đảm bảo đúng dtype
154
+ # Giả sử mô hình ONNX chấp nhận input với tên "input"
155
+ ort_inputs = {pipeline.get_inputs()[0].name: input_data}
156
+ ort_outputs = pipeline.run(None, ort_inputs)
157
+ # Giả sử output là logits ở vị trí 0
158
+ logits = ort_outputs[0]
159
+ # Xử lý logits để lấy predictions
160
+ topk_indices = np.argsort(logits, axis=1)[:, -inference_config.top_k:]
161
+ topk_scores = np.sort(logits, axis=1)[:, -inference_config.top_k:]
162
+ predictions.predictions = [
163
+ {"gloss": str(idx), "score": float(score)}
164
+ for idx, score in zip(topk_indices.flatten(), topk_scores.flatten())
165
+ ]
166
+ else:
167
+ # Sử dụng pipeline của Transformers
168
+ predictions = Predictions(predictions=pipeline(np.array(data)))
169
+
170
+ predictions.inference_time = 0 # Có thể tính toán thời gian nếu cần
171
 
172
+ # Lấy thời gian bắt đầu và kết thúc
173
+ start_time, end_time = get_sample_timestamp(left_arm, right_arm)
 
174
 
175
+ # Convert từ miliseconds sang seconds
176
+ start_time /= 1_000
177
+ end_time /= 1_000
 
 
178
 
179
  predictions.start_time = start_time
180
  predictions.end_time = end_time