Upload 3 files
Browse files- app.py +43 -0
- posture_utils.py +14 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/Qwen-VL-Chat"
|
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()
|
posture_utils.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def analyze_posture_by_keypoints(landmarks):
|
2 |
+
left_shoulder = landmarks.landmark[11]
|
3 |
+
right_shoulder = landmarks.landmark[12]
|
4 |
+
left_ear = landmarks.landmark[7]
|
5 |
+
right_ear = landmarks.landmark[8]
|
6 |
+
|
7 |
+
shoulder_x = (left_shoulder.x + right_shoulder.x) / 2
|
8 |
+
ear_x = (left_ear.x + right_ear.x) / 2
|
9 |
+
|
10 |
+
delta = ear_x - shoulder_x
|
11 |
+
if abs(delta) > 0.06:
|
12 |
+
return "该用户存在驼背或低头倾向,头部明显前倾。"
|
13 |
+
else:
|
14 |
+
return "该用户坐姿较为端正,头部与肩部对齐。"
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
gradio==4.16.0
|
4 |
+
mediapipe
|
5 |
+
opencv-python
|
6 |
+
Pillow
|