steveyehtw commited on
Commit
657d6ef
·
1 Parent(s): 9d97138

Add tensorflow, keras, and mediapipe dependencies with auto-generation fallback in runner

Browse files
Files changed (2) hide show
  1. requirements.txt +3 -0
  2. runner.py +37 -0
requirements.txt CHANGED
@@ -6,3 +6,6 @@ websockets>=11.0.3
6
  numpy>=1.24.0
7
  opencv-python-headless>=4.7.0.0
8
  pygame-ce>=2.3.0
 
 
 
 
6
  numpy>=1.24.0
7
  opencv-python-headless>=4.7.0.0
8
  pygame-ce>=2.3.0
9
+ tensorflow
10
+ keras
11
+ mediapipe
runner.py CHANGED
@@ -393,10 +393,47 @@ print(f"[Runner] 切換工作目錄至: {project_dir}")
393
  os.chdir(project_dir)
394
  sys.path.insert(0, project_dir)
395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
  try:
397
  print(f"[Runner] 開始執行學生專案: {os.path.basename(project_path)}")
398
  runpy.run_path(project_path, run_name='__main__')
399
  except SystemExit as e:
 
400
  print(f"[Runner] 專案正常結束 (SystemExit: {e})")
401
  except Exception as e:
402
  import traceback
 
393
  os.chdir(project_dir)
394
  sys.path.insert(0, project_dir)
395
 
396
+ # 🚀 Auto-generate dummy keras_model.h5 and labels.txt if missing in a Keras project
397
+ try:
398
+ script_content = open(project_path, "r", errors="ignore").read()
399
+ if "keras_model.h5" in script_content:
400
+ h5_path = os.path.join(project_dir, "keras_model.h5")
401
+ labels_path = os.path.join(project_dir, "labels.txt")
402
+
403
+ if not os.path.exists(h5_path):
404
+ print("[Runner] 偵測到 Keras 專案且缺乏 keras_model.h5,嘗試自動生成隨機 Dummy 模型...")
405
+ try:
406
+ try:
407
+ import keras
408
+ except ImportError:
409
+ from tensorflow import keras
410
+
411
+ model = keras.models.Sequential([
412
+ keras.layers.Input(shape=(224, 224, 3)),
413
+ keras.layers.Flatten(),
414
+ keras.layers.Dense(3, activation='softmax')
415
+ ])
416
+ model.save(h5_path)
417
+ print("[Runner] 成功生成 dummy keras_model.h5")
418
+ except Exception as ex:
419
+ print(f"[Runner] 無法自動生成模型(可能是本地缺乏 tensorflow/keras 套件):{ex}")
420
+
421
+ if not os.path.exists(labels_path):
422
+ print("[Runner] 偵測到缺乏 labels.txt,自動生成預設分類標籤...")
423
+ try:
424
+ with open(labels_path, "w", encoding="utf-8") as f:
425
+ f.write("0 背景\n1 喝水中\n2 其他動作\n")
426
+ print("[Runner] 成功生成 labels.txt")
427
+ except Exception as ex:
428
+ print(f"[Runner] 無法自動生成 labels.txt:{ex}")
429
+ except Exception as e:
430
+ print(f"[Runner] 檢查/生成 Keras 依賴資源時出錯: {e}")
431
+
432
  try:
433
  print(f"[Runner] 開始執行學生專案: {os.path.basename(project_path)}")
434
  runpy.run_path(project_path, run_name='__main__')
435
  except SystemExit as e:
436
+
437
  print(f"[Runner] 專案正常結束 (SystemExit: {e})")
438
  except Exception as e:
439
  import traceback