taybeyond commited on
Commit
a726d04
·
verified ·
1 Parent(s): 1d213ed

Upload 2 files

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