BrianChuan commited on
Commit
ca2aba8
·
1 Parent(s): 6c12d73

Camera zoom

Browse files
Files changed (1) hide show
  1. emotion.py +25 -0
emotion.py CHANGED
@@ -18,6 +18,7 @@ EMA_ALPHA = 0.45
18
  CONF_TH = 40.0
19
  SWITCH_CONFIRM_SEC = 2.5
20
  IOU_TH = 0.25
 
21
 
22
  DETECTOR_BACKEND = "mtcnn" # 想快一點可改 "opencv"
23
  ALIGN_FACE = True
@@ -177,6 +178,28 @@ def _downsample_rgb(frame: np.ndarray, target_w: int) -> np.ndarray:
177
  img = img.resize((target_w, new_h))
178
  return np.array(img)
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  def _iou(a: Tuple[int, int, int, int], b: Tuple[int, int, int, int]) -> float:
182
  ax, ay, aw, ah = a
@@ -279,6 +302,8 @@ def on_stream(frame_rgb: np.ndarray, st: AppState):
279
  st,
280
  gr.update(value="重新攝影機辨識"),
281
  )
 
 
282
 
283
  now = time.time()
284
  if now - st.last_analyze_t < ANALYZE_EVERY_SEC:
 
18
  CONF_TH = 40.0
19
  SWITCH_CONFIRM_SEC = 2.5
20
  IOU_TH = 0.25
21
+ ZOOM_FACTOR = 1.5
22
 
23
  DETECTOR_BACKEND = "mtcnn" # 想快一點可改 "opencv"
24
  ALIGN_FACE = True
 
178
  img = img.resize((target_w, new_h))
179
  return np.array(img)
180
 
181
+ def _zoom_center(frame: np.ndarray, zoom: float) -> np.ndarray:
182
+ """
183
+ 裁切畫面中心以模擬數位變焦。
184
+ zoom: 放大倍率 (例如 1.5)
185
+ """
186
+ if frame is None or zoom <= 1.0:
187
+ return frame
188
+
189
+ h, w = frame.shape[:2]
190
+
191
+ # 計算新的寬高
192
+ new_w = int(w / zoom)
193
+ new_h = int(h / zoom)
194
+
195
+ # 計算起始點 (讓裁切框置中)
196
+ start_x = (w - new_w) // 2
197
+ start_y = (h - new_h) // 2
198
+
199
+ # 進行裁切
200
+ cropped = frame[start_y : start_y + new_h, start_x : start_x + new_w]
201
+ return cropped
202
+
203
 
204
  def _iou(a: Tuple[int, int, int, int], b: Tuple[int, int, int, int]) -> float:
205
  ax, ay, aw, ah = a
 
302
  st,
303
  gr.update(value="重新攝影機辨識"),
304
  )
305
+
306
+ frame_rgb = _zoom_center(frame_rgb, ZOOM_FACTOR)
307
 
308
  now = time.time()
309
  if now - st.last_analyze_t < ANALYZE_EVERY_SEC: