steveyehtw commited on
Commit
7730264
·
1 Parent(s): 7095d07

Revert model path to relative, fix indentation of cap.release() in hand tracking, and use direct path loader for Chinese fonts

Browse files
__pycache__/runner.cpython-314.pyc DELETED
Binary file (28.2 kB)
 
projects/2026Spring專題班/胡惟媗/手部辨識/main.py CHANGED
@@ -37,8 +37,7 @@ cap = cv2.VideoCapture(0)
37
  # 沒有攝影機就改成影片:cap = cv2.VideoCapture("backup_hand_video.mp4")
38
 
39
  # MediaPipe (Tasks API)
40
- model_path = os.path.join(os.path.dirname(__file__), 'hand_landmarker.task')
41
- base_options = python.BaseOptions(model_asset_path=model_path)
42
  options = vision.HandLandmarkerOptions(base_options=base_options, num_hands=1)
43
  detector = vision.HandLandmarker.create_from_options(options)
44
 
@@ -154,6 +153,6 @@ while running:
154
  pygame.display.update()
155
  clock.tick(30)
156
 
157
- cap.release()
158
  pygame.quit()
159
  sys.exit()
 
37
  # 沒有攝影機就改成影片:cap = cv2.VideoCapture("backup_hand_video.mp4")
38
 
39
  # MediaPipe (Tasks API)
40
+ base_options = python.BaseOptions(model_asset_path='hand_landmarker.task')
 
41
  options = vision.HandLandmarkerOptions(base_options=base_options, num_hands=1)
42
  detector = vision.HandLandmarker.create_from_options(options)
43
 
 
153
  pygame.display.update()
154
  clock.tick(30)
155
 
156
+ cap.release()
157
  pygame.quit()
158
  sys.exit()
projects/2026Spring專題班/胡惟媗/臉部辨識/main.py CHANGED
@@ -43,7 +43,7 @@ except FileNotFoundError:
43
  # ============================================
44
  # 2. Initialize MediaPipe Face Landmarker
45
  # ============================================
46
- model_path = os.path.join(os.path.dirname(__file__), 'face_landmarker.task')
47
  if not os.path.exists(model_path):
48
  print(f"Error: Model file not found at {model_path}")
49
  pygame.quit()
 
43
  # ============================================
44
  # 2. Initialize MediaPipe Face Landmarker
45
  # ============================================
46
+ model_path = 'face_landmarker.task'
47
  if not os.path.exists(model_path):
48
  print(f"Error: Model file not found at {model_path}")
49
  pygame.quit()
runner.py CHANGED
@@ -293,27 +293,54 @@ def custom_mouse_get_pressed(num_buttons=3):
293
  pygame.mouse.get_pressed = custom_mouse_get_pressed
294
 
295
  # 4. Patch Pygame fonts for Chinese display fallback compatibility
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  original_SysFont = pygame.font.SysFont
297
  def custom_SysFont(name, size, bold=False, italic=False):
298
- chinese_font_candidates = [
299
- "microsoftjhenghei", "microsoftyahei", "msjh", "msyh",
300
- "pingfangtc", "pingfangsc", "pingfang", "songti", "song",
301
- "stheitimedium", "stheitilight", "stheiti", "heiti tc", "heiti sc",
302
- "arialunicodems", "arialunicode", "cjksymbolsfallback",
303
- "wqy-zenhei", "wqy-microhei", "wqy", "notosanscjk", "notosanscjk-regular",
304
- "droidsansfallback"
305
- ]
306
  name_lower = name.lower() if isinstance(name, str) else ""
307
- is_chinese_request = any(x in name_lower for x in ["jheng", "hei", "kai", "ming", "song", "cjk", "wqy", "unicode", "yahei"]) or (name is None)
308
 
309
  if is_chinese_request:
310
- available_fonts = pygame.font.get_fonts()
311
- for candidate in chinese_font_candidates:
312
- if candidate in available_fonts:
313
- return original_SysFont(candidate, size, bold, italic)
314
- for candidate in ["arialunicode", "songti", "stheitimedium", "wqy-zenhei"]:
315
- if pygame.font.match_font(candidate) is not None:
316
- return original_SysFont(candidate, size, bold, italic)
317
 
318
  return original_SysFont(name, size, bold, italic)
319
  pygame.font.SysFont = custom_SysFont
@@ -321,22 +348,18 @@ pygame.font.SysFont = custom_SysFont
321
  original_Font = pygame.font.Font
322
  def custom_Font(filename, size):
323
  if filename is None:
324
- chinese_font_candidates = [
325
- "microsoftjhenghei", "pingfangtc", "songti", "stheitimedium",
326
- "arialunicodems", "wqy-zenhei", "droidsansfallback"
327
- ]
328
- for candidate in chinese_font_candidates:
329
- path = pygame.font.match_font(candidate)
330
- if path is not None:
331
- try:
332
- return original_Font(path, size)
333
- except Exception:
334
- pass
335
  return original_Font(filename, size)
336
  pygame.font.Font = custom_Font
337
 
338
 
339
 
 
340
  # ----------------- Monkeypatching OpenCV (cv2) -----------------
341
  import cv2
342
 
 
293
  pygame.mouse.get_pressed = custom_mouse_get_pressed
294
 
295
  # 4. Patch Pygame fonts for Chinese display fallback compatibility
296
+ def find_chinese_font_path():
297
+ paths = [
298
+ # Linux (Debian/Ubuntu)
299
+ "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
300
+ "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
301
+ "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
302
+ "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
303
+ "/usr/share/fonts/truetype/droid/DroidSansFallback.ttf",
304
+ # macOS
305
+ "/System/Library/Fonts/Supplemental/Songti.ttc",
306
+ "/System/Library/Fonts/STHeiti Light.ttc",
307
+ "/System/Library/Fonts/STHeiti Medium.ttc",
308
+ "/System/Library/Fonts/PingFang.ttc",
309
+ "/Library/Fonts/Arial Unicode.ttf",
310
+ # Windows
311
+ "C:\\Windows\\Fonts\\msjh.ttc",
312
+ "C:\\Windows\\Fonts\\msjhbd.ttc",
313
+ "C:\\Windows\\Fonts\\msyh.ttc",
314
+ "C:\\Windows\\Fonts\\simsun.ttc",
315
+ ]
316
+ for p in paths:
317
+ if os.path.exists(p):
318
+ return p
319
+
320
+ candidates = [
321
+ "wenquanyizenhei", "wenquanyimicrohei", "wqy-zenhei", "wqy-microhei",
322
+ "microsoftjhenghei", "pingfangtc", "songti", "stheitimedium",
323
+ "arialunicodems", "droidsansfallback"
324
+ ]
325
+ for c in candidates:
326
+ match = pygame.font.match_font(c)
327
+ if match:
328
+ return match
329
+
330
+ return None
331
+
332
  original_SysFont = pygame.font.SysFont
333
  def custom_SysFont(name, size, bold=False, italic=False):
 
 
 
 
 
 
 
 
334
  name_lower = name.lower() if isinstance(name, str) else ""
335
+ is_chinese_request = any(x in name_lower for x in ["jheng", "hei", "kai", "ming", "song", "cjk", "wqy", "unicode", "yahei", "wenquanyi"]) or (name is None)
336
 
337
  if is_chinese_request:
338
+ path = find_chinese_font_path()
339
+ if path:
340
+ try:
341
+ return pygame.font.Font(path, size)
342
+ except Exception:
343
+ pass
 
344
 
345
  return original_SysFont(name, size, bold, italic)
346
  pygame.font.SysFont = custom_SysFont
 
348
  original_Font = pygame.font.Font
349
  def custom_Font(filename, size):
350
  if filename is None:
351
+ path = find_chinese_font_path()
352
+ if path:
353
+ try:
354
+ return original_Font(path, size)
355
+ except Exception:
356
+ pass
 
 
 
 
 
357
  return original_Font(filename, size)
358
  pygame.font.Font = custom_Font
359
 
360
 
361
 
362
+
363
  # ----------------- Monkeypatching OpenCV (cv2) -----------------
364
  import cv2
365