Delete app.py
Browse files
app.py
DELETED
@@ -1,43 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import mediapipe as mp
|
3 |
-
import cv2
|
4 |
-
import numpy as np
|
5 |
-
from PIL import Image
|
6 |
-
import torch
|
7 |
-
from transformers import AutoProcessor, AutoTokenizer, AutoModelForCausalLM
|
8 |
-
from posture_utils import analyze_posture_by_keypoints
|
9 |
-
|
10 |
-
model_id = "Qwen/Qwen2-VL-7B"
|
11 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
12 |
-
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
13 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, device_map="auto").eval()
|
14 |
-
|
15 |
-
mp_pose = mp.solutions.pose
|
16 |
-
|
17 |
-
def process(image: Image):
|
18 |
-
np_image = np.array(image)
|
19 |
-
with mp_pose.Pose(static_image_mode=True) as pose:
|
20 |
-
results = pose.process(cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR))
|
21 |
-
|
22 |
-
if not results.pose_landmarks:
|
23 |
-
return "❗ 无法检测到人体,请上传包含上半身的清晰坐姿照片。"
|
24 |
-
|
25 |
-
posture_analysis = analyze_posture_by_keypoints(results.pose_landmarks)
|
26 |
-
prompt = f"<|im_start|>user\n请根据以下坐姿描述生成中英文提醒:\n{posture_analysis}\n<|im_end|>\n<|im_start|>assistant\n"
|
27 |
-
inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
|
28 |
-
output = model.generate(**inputs, max_new_tokens=512)
|
29 |
-
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
30 |
-
return result.split("<|im_start|>assistant\n")[-1].strip()
|
31 |
-
|
32 |
-
demo = gr.Interface(
|
33 |
-
fn=process,
|
34 |
-
inputs=gr.Image(type="pil", label="上传你的坐姿照片"),
|
35 |
-
outputs=gr.Textbox(label="中英文坐姿分析结果"),
|
36 |
-
title="🪑 坐姿监测融合助手",
|
37 |
-
description="上传坐姿图像,先通过 Mediapipe 判断是否驼背、低头、含胸,再交由 Qwen-VL 生成提醒语。",
|
38 |
-
theme="soft",
|
39 |
-
allow_flagging="never"
|
40 |
-
)
|
41 |
-
|
42 |
-
if __name__ == "__main__":
|
43 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|