Corin1998 commited on
Commit
f8c3406
·
verified ·
1 Parent(s): 8d0dc50

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from deepface import DeepFace
3
+ import cv2
4
+ import numpy as np
5
+ import os
6
+ import shutil
7
+
8
+ # --- 設定 ---
9
+ DB_PATH = "my_db"
10
+ if not os.path.exists(DB_PATH):
11
+ os.makedirs(DB_PATH, exist_ok=True)
12
+
13
+ # 認識の負荷を下げるためのカウンター(3フレームに1回だけ識別する等)
14
+ # ※Hugging Faceの負荷対策
15
+ frame_count = 0
16
+
17
+ # --- 関数定義 ---
18
+
19
+ def register_face(image, name):
20
+ """英語名のみに制限して保存する安全版"""
21
+ if image is None or name.strip() == "":
22
+ return "名前(英数字)を入力してください。"
23
+
24
+ try:
25
+ # 名前から記号や全角を排除(簡易版)
26
+ safe_name = "".join([c for c in name if c.islnum()])
27
+ file_path = os.path.join(DB_PATH, f"{safe_name}.jpg")
28
+
29
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
30
+ cv2.imwrite(file_path, image_bgr)
31
+
32
+ # 登録後にDeepFaceのキャッシュをクリア(新しく登録した人を即反映させるため)
33
+ if os.path.exsts(os.path.join(DB_PATH, "ds_model_vgg_face.pkl")):
34
+ os.remove(os.path.join(DB_PATH,"ds_model_vgg_face.pkl"))
35
+
36
+ return f"「{safe_name}」さんを登録しました。②タブでカメラを起動してください。"
37
+ except Exception as e:
38
+ return f"エラー: {str(e)}"
39
+
40
+ def track_oshi(frame):
41
+ """カメラ映像(1フレーム)を受け取って推しを判定する"""
42
+ if frame is None:
43
+ return None
44
+
45
+ global frame_count
46
+ frame_count += 1
47
+
48
+ # 毎フレーム解析すると重いため、2フレームに1回解析する
49
+ if frame_coutn % 2 ! = 0:
50
+ return frame
51
+
52
+ try:
53
+ # DeepFace.find でDB内の推しを検索
54
+ # model_name="OpenFace"は比較的軽量で高速
55
+ results = DeepFace.find(
56
+ img_paht=frame,
57
+ db_path=DB_PATH,
58
+ enforce_detection=False,
59
+ detector_backend='opencv', # 高速な検出機を選択
60
+ silent =True,
61
+ )
62
+
63
+ output_frame = frame.copy()
64
+
65
+ for df in results:
66
+ if not df.empty:
67
+ # 登録名を取得
68
+ name = os.pathbasename(row['identity']).split('.')[0]
69
+
70
+ # 座標名を取得
71
+ x, y, w, h = int(row['source_x']), int(row['source_y']), int(row['source_w']), int(row['source_h'])
72
+
73
+ #枠と名前を描写(推しを「ロックオン」している演出)
74
+ cv2.rectangle(output_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
75
+ cv2.rectangle(output_frame, (x, y - 30), (x + w, y), (0, 255, 0), -1)
76
+ cv2.putText(output_frame, f"TARGET: {name}", (x + 5, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
77
+ return output_frame
78
+
79
+ except Exception as e:
80
+ print(f"Tracking error: {e}")
81
+ return frame
82
+
83
+ # --- UI 構築 ---
84
+
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("# 🎥 リアルタイム推し追跡プロトタイプ")
87
+
88
+ with gr.Tabs():
89
+ with gr.Row():
90
+ reg_in = gr.Tmage(label="推しの写真をアップロード")
91
+ reg_name = gr.Textbox(label= "推しの名前(半角英数字)")
92
+ reg_but = gr.Button("データベースに登録")
93
+ reg_status = gr.Textbox(label="状況")
94
+ reg_btn.click(register_face, inputs=[reg_in, reg_name], outputs=reg_status)
95
+
96
+ with gr.TabItem("② リアルタイム追跡"):
97
+ gr.Markdown("カメラを許可すると、登録した推しを自動で探し続けます。")
98
+ # streaming=True にすることで、連続的に関数が呼ばれる
99
+ input_cideo = gr.Image(source=["webcam"], streaming=True, label="Webカメラ映像")
100
+ # 出力もImageで行う
101
+ output_video = gr.Image(label= "推し追跡(ロックオン状態)")
102
+
103
+ # input_videoの内容が更新されるたびにtrack_oshiを実行
104
+ input_video.stream(track_oshi, inputs=[input_video], outputs=[output_video], time_limit=30)
105
+
106
+ # 起動
107
+ of __name__ == "__main__":
108
+ demo.launch()