Delete app.py
Browse files
app.py
DELETED
@@ -1,55 +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 |
-
from transformers import AutoTokenizer, AutoProcessor, AutoModel
|
7 |
-
|
8 |
-
model_id = "Qwen/Qwen2-VL-7B"
|
9 |
-
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
11 |
-
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
12 |
-
model = AutoModel.from_pretrained(model_id, trust_remote_code=True, device_map="auto").eval()
|
13 |
-
|
14 |
-
mp_pose = mp.solutions.pose
|
15 |
-
|
16 |
-
def analyze_posture_by_keypoints(landmarks):
|
17 |
-
left_shoulder = landmarks.landmark[11]
|
18 |
-
right_shoulder = landmarks.landmark[12]
|
19 |
-
left_ear = landmarks.landmark[7]
|
20 |
-
right_ear = landmarks.landmark[8]
|
21 |
-
|
22 |
-
shoulder_x = (left_shoulder.x + right_shoulder.x) / 2
|
23 |
-
ear_x = (left_ear.x + right_ear.x) / 2
|
24 |
-
delta = ear_x - shoulder_x
|
25 |
-
if abs(delta) > 0.06:
|
26 |
-
return "该用户存在驼背或低头倾向,头部明显前倾。"
|
27 |
-
else:
|
28 |
-
return "该用户坐姿较为端正,头部与肩部对齐。"
|
29 |
-
|
30 |
-
def process(image: Image):
|
31 |
-
np_image = np.array(image)
|
32 |
-
with mp_pose.Pose(static_image_mode=True) as pose:
|
33 |
-
results = pose.process(cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR))
|
34 |
-
if not results.pose_landmarks:
|
35 |
-
return "❗ 无法检测到人体,请上传包含上半身的清晰坐姿照片。"
|
36 |
-
|
37 |
-
posture_analysis = analyze_posture_by_keypoints(results.pose_landmarks)
|
38 |
-
prompt = f"请根据以下坐姿描述生成中英文提醒:\n{posture_analysis}"
|
39 |
-
inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
|
40 |
-
outputs = model.generate(**inputs, max_new_tokens=512)
|
41 |
-
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
-
return result
|
43 |
-
|
44 |
-
demo = gr.Interface(
|
45 |
-
fn=process,
|
46 |
-
inputs=gr.Image(type="pil", label="上传你的坐姿照片"),
|
47 |
-
outputs=gr.Textbox(label="中英文坐姿分析结果"),
|
48 |
-
title="🪑 Qwen2-VL 坐姿识别助手(修复版)",
|
49 |
-
description="融合 Mediapipe 与 Qwen2-VL 模型,判断是否驼背并生成中英文提醒。",
|
50 |
-
theme="soft",
|
51 |
-
allow_flagging="never"
|
52 |
-
)
|
53 |
-
|
54 |
-
if __name__ == "__main__":
|
55 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|