Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,8 +10,7 @@ DB_PATH = "my_db"
|
|
| 10 |
if not os.path.exists(DB_PATH):
|
| 11 |
os.makedirs(DB_PATH, exist_ok=True)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
# ※Hugging Faceの負荷対策
|
| 15 |
frame_count = 0
|
| 16 |
|
| 17 |
# --- 関数定義 ---
|
|
@@ -19,90 +18,86 @@ frame_count = 0
|
|
| 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.
|
| 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 |
-
#
|
| 33 |
-
if os.path.
|
| 34 |
-
os.remove(os.path.join(DB_PATH,"ds_model_vgg_face.pkl"))
|
| 35 |
-
|
| 36 |
-
|
| 37 |
except Exception as e:
|
| 38 |
return f"エラー: {str(e)}"
|
| 39 |
|
| 40 |
def track_oshi(frame):
|
| 41 |
-
"""
|
| 42 |
if frame is None:
|
| 43 |
return None
|
| 44 |
-
|
| 45 |
global frame_count
|
| 46 |
frame_count += 1
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
|
|
|
|
| 50 |
return frame
|
| 51 |
|
| 52 |
try:
|
| 53 |
-
#
|
| 54 |
-
# model_name="OpenFace"は比較的軽量で高速
|
| 55 |
results = DeepFace.find(
|
| 56 |
-
|
| 57 |
-
db_path=DB_PATH,
|
| 58 |
-
enforce_detection=False,
|
| 59 |
-
detector_backend='opencv',
|
| 60 |
-
silent
|
| 61 |
)
|
| 62 |
-
|
| 63 |
output_frame = frame.copy()
|
| 64 |
-
|
| 65 |
for df in results:
|
| 66 |
if not df.empty:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 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.
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
gr.
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# 起動
|
| 107 |
-
of __name__ == "__main__":
|
| 108 |
demo.launch()
|
|
|
|
| 10 |
if not os.path.exists(DB_PATH):
|
| 11 |
os.makedirs(DB_PATH, exist_ok=True)
|
| 12 |
|
| 13 |
+
# 処理フレームの間隔(例:2なら2フレームに1回解析)
|
|
|
|
| 14 |
frame_count = 0
|
| 15 |
|
| 16 |
# --- 関数定義 ---
|
|
|
|
| 18 |
def register_face(image, name):
|
| 19 |
"""英語名のみに制限して保存する安全版"""
|
| 20 |
if image is None or name.strip() == "":
|
| 21 |
+
return "名前(英数字)を入力してください。"
|
| 22 |
+
|
| 23 |
try:
|
| 24 |
+
# 記号やスペースを除去した安全な名前
|
| 25 |
+
safe_name = "".join([c for c in name if c.isalnum()])
|
| 26 |
file_path = os.path.join(DB_PATH, f"{safe_name}.jpg")
|
| 27 |
+
|
| 28 |
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 29 |
cv2.imwrite(file_path, image_bgr)
|
| 30 |
+
|
| 31 |
+
# 登録後にキャッシュを削除
|
| 32 |
+
if os.path.exists(os.path.join(DB_PATH, "ds_model_vgg_face.pkl")):
|
| 33 |
+
os.remove(os.path.join(DB_PATH, "ds_model_vgg_face.pkl"))
|
| 34 |
+
|
| 35 |
+
return f"「{safe_name}」を登録しました。②タブへ進んでください。"
|
| 36 |
except Exception as e:
|
| 37 |
return f"エラー: {str(e)}"
|
| 38 |
|
| 39 |
def track_oshi(frame):
|
| 40 |
+
"""カメラ映像から推しを判定する"""
|
| 41 |
if frame is None:
|
| 42 |
return None
|
| 43 |
+
|
| 44 |
global frame_count
|
| 45 |
frame_count += 1
|
| 46 |
+
|
| 47 |
+
# 【修正箇所】演算子と変数名を修正
|
| 48 |
+
# 2フレームに1回だけ重い処理を行うことで動作を軽くする
|
| 49 |
+
if frame_count % 2 != 0:
|
| 50 |
return frame
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
# 顔の検索(検出器に高速なopencvを指定)
|
|
|
|
| 54 |
results = DeepFace.find(
|
| 55 |
+
img_path=frame,
|
| 56 |
+
db_path=DB_PATH,
|
| 57 |
+
enforce_detection=False,
|
| 58 |
+
detector_backend='opencv',
|
| 59 |
+
silent=True
|
| 60 |
)
|
| 61 |
+
|
| 62 |
output_frame = frame.copy()
|
| 63 |
+
|
| 64 |
for df in results:
|
| 65 |
if not df.empty:
|
| 66 |
+
for _, row in df.iterrows():
|
| 67 |
+
name = os.path.basename(row['identity']).split('.')[0]
|
| 68 |
+
x, y, w, h = int(row['source_x']), int(row['source_y']), int(row['source_w']), int(row['source_h'])
|
| 69 |
+
|
| 70 |
+
# 枠と名前を描画
|
| 71 |
+
cv2.rectangle(output_frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 72 |
+
cv2.putText(output_frame, f"TARGET: {name}", (x, y-10),
|
| 73 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 74 |
+
|
|
|
|
| 75 |
return output_frame
|
| 76 |
|
| 77 |
except Exception as e:
|
|
|
|
| 78 |
return frame
|
| 79 |
|
| 80 |
+
# --- UI (Gradio) ---
|
| 81 |
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("# 🎥 リアルタイム推し追跡")
|
| 84 |
+
|
| 85 |
with gr.Tabs():
|
| 86 |
+
with gr.TabItem("① 推し登録"):
|
| 87 |
+
with gr.Row():
|
| 88 |
+
reg_in = gr.Image(label="推しの写真")
|
| 89 |
+
reg_name = gr.Textbox(label="名前(半角英数字)", value="oshi")
|
| 90 |
+
reg_btn = gr.Button("登録")
|
| 91 |
+
reg_status = gr.Textbox(label="ステータス")
|
| 92 |
+
reg_btn.click(register_face, inputs=[reg_in, reg_name], outputs=reg_status)
|
| 93 |
+
|
| 94 |
+
with gr.TabItem("② リアルタイム追跡"):
|
| 95 |
+
gr.Markdown("カメラを起動して自分や推しを映してください。")
|
| 96 |
+
input_video = gr.Image(sources=["webcam"], streaming=True)
|
| 97 |
+
output_video = gr.Image(label="解析結果")
|
| 98 |
+
|
| 99 |
+
# ストリーミング設定
|
| 100 |
+
input_video.stream(track_oshi, inputs=[input_video], outputs=[output_video])
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
| 103 |
demo.launch()
|