Update src/streamlit_app.py
Browse files- src/streamlit_app.py +136 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,142 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import time
|
| 6 |
+
import mediapipe as mp
|
| 7 |
|
| 8 |
+
mp_pose = mp.solutions.pose
|
| 9 |
+
pose = mp_pose.Pose(
|
| 10 |
+
min_detection_confidence=0.5,
|
| 11 |
+
min_tracking_confidence=0.5,
|
| 12 |
+
model_complexity=1
|
| 13 |
+
)
|
| 14 |
+
mp_drawing = mp.solutions.drawing_utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def analyze_posture(image):
|
| 17 |
+
"""Analyze posture on the image and return the image with keypoints and analysis text"""
|
| 18 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 19 |
+
results = pose.process(image_rgb)
|
| 20 |
+
|
| 21 |
+
annotated_image = image.copy()
|
| 22 |
+
if results.pose_landmarks:
|
| 23 |
+
mp_drawing.draw_landmarks(
|
| 24 |
+
annotated_image,
|
| 25 |
+
results.pose_landmarks,
|
| 26 |
+
mp_pose.POSE_CONNECTIONS,
|
| 27 |
+
mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
|
| 28 |
+
mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2)
|
| 29 |
+
)
|
| 30 |
+
posture_status = check_posture(results.pose_landmarks, image.shape)
|
| 31 |
+
else:
|
| 32 |
+
posture_status = "キーポイントが検出されませんでした (Key points not detected)"
|
| 33 |
+
|
| 34 |
+
return annotated_image, posture_status
|
| 35 |
|
| 36 |
+
def check_posture(landmarks, image_shape):
|
| 37 |
+
"""Analyze posture and return a text report"""
|
| 38 |
+
h, w, _ = image_shape
|
| 39 |
+
|
| 40 |
+
# Get key points
|
| 41 |
+
left_shoulder = landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
|
| 42 |
+
right_shoulder = landmarks.landmark[mp_pose.PoseLandmark.RIGHT_SHOULDER]
|
| 43 |
+
left_hip = landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP]
|
| 44 |
+
right_hip = landmarks.landmark[mp_pose.PoseLandmark.RIGHT_HIP]
|
| 45 |
+
left_ear = landmarks.landmark[mp_pose.PoseLandmark.LEFT_EAR]
|
| 46 |
+
right_ear = landmarks.landmark[mp_pose.PoseLandmark.RIGHT_EAR]
|
| 47 |
+
nose = landmarks.landmark[mp_pose.PoseLandmark.NOSE]
|
| 48 |
+
|
| 49 |
+
# Determine posture (sitting/standing)
|
| 50 |
+
sitting = left_hip.y < left_shoulder.y + 0.1 or right_hip.y < right_shoulder.y + 0.1
|
| 51 |
+
|
| 52 |
+
messages = []
|
| 53 |
+
|
| 54 |
+
# Check for forward head posture
|
| 55 |
+
head_forward = (left_ear.y > left_shoulder.y + 0.1 or right_ear.y > right_shoulder.y + 0.1) and \
|
| 56 |
+
(nose.y > left_shoulder.y or nose.y > right_shoulder.y)
|
| 57 |
+
if head_forward:
|
| 58 |
+
messages.append("• 頭が前に傾いています (テキストネック) (Head tilted forward - text neck)")
|
| 59 |
+
|
| 60 |
+
# Check for rounded shoulders
|
| 61 |
+
shoulders_rounded = left_shoulder.x > left_hip.x + 0.05 or right_shoulder.x < right_hip.x - 0.05
|
| 62 |
+
if shoulders_rounded:
|
| 63 |
+
messages.append("• 肩が丸まっています (Rounded shoulders)")
|
| 64 |
+
|
| 65 |
+
# Check for side tilt
|
| 66 |
+
shoulder_diff = abs(left_shoulder.y - right_shoulder.y)
|
| 67 |
+
hip_diff = abs(left_hip.y - right_hip.y)
|
| 68 |
+
if shoulder_diff > 0.05 or hip_diff > 0.05:
|
| 69 |
+
messages.append("• 体が横に傾いています (Asymmetrical posture)")
|
| 70 |
+
|
| 71 |
+
# Check pelvis position
|
| 72 |
+
if sitting and (left_hip.y < left_shoulder.y + 0.15 or right_hip.y < right_shoulder.y + 0.15):
|
| 73 |
+
messages.append("• 骨盤が前に傾いています (Pelvis tilted forward)")
|
| 74 |
+
|
| 75 |
+
# Generate final report
|
| 76 |
+
if messages:
|
| 77 |
+
report = [
|
| 78 |
+
f"**{'座り姿勢' if sitting else '立ち姿勢'} - 問題が検出されました ({'Sitting' if sitting else 'Standing'} - problems detected):**",
|
| 79 |
+
*messages,
|
| 80 |
+
"\n**アドバイス (Recommendations):**",
|
| 81 |
+
"• 頭をまっすぐに保ち、耳が肩の上にくるように (Keep your head straight, ears over shoulders)",
|
| 82 |
+
"• 肩を後ろに引き下げて (Pull shoulders back and down)",
|
| 83 |
+
"• 背中をまっすぐに保ち、横に傾かないように (Keep your back straight, avoid side tilting)",
|
| 84 |
+
"• 座るときは坐骨で支えるように (When sitting, support your weight on sitting bones)"
|
| 85 |
+
]
|
| 86 |
+
else:
|
| 87 |
+
report = [
|
| 88 |
+
f"**完璧な姿勢です ({'座り姿勢' if sitting else '立ち姿勢'})! (Perfect posture {'sitting' if sitting else 'standing'})**",
|
| 89 |
+
"すべてのキーポイントが正しい位置にあります (All key points are in correct position)",
|
| 90 |
+
"\n**アドバイス (Advice):**",
|
| 91 |
+
"• 一日中姿勢に気を付けてください (Continue to monitor your posture throughout the day)"
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
return "\n\n".join(report)
|
| 95 |
|
| 96 |
+
def main():
|
| 97 |
+
st.set_page_config(layout="wide")
|
| 98 |
+
st.title("📷 姿勢分析アプリ (Posture Analyzer)")
|
| 99 |
+
st.write("このアプリはリアルタイムで姿勢を分析します (This app analyzes your posture in real time)")
|
| 100 |
+
|
| 101 |
+
# Create two columns
|
| 102 |
+
col1, col2 = st.columns([2, 1])
|
| 103 |
+
|
| 104 |
+
with col1:
|
| 105 |
+
st.header("カメラビュー (Camera View)")
|
| 106 |
+
run = st.checkbox("ウェブカメラをオンにする (Enable webcam)", value=True)
|
| 107 |
+
FRAME_WINDOW = st.image([])
|
| 108 |
+
|
| 109 |
+
with col2:
|
| 110 |
+
st.header("姿勢分析 (Posture Analysis)")
|
| 111 |
+
status_placeholder = st.empty()
|
| 112 |
+
if not run:
|
| 113 |
+
status_placeholder.markdown("""
|
| 114 |
+
**カメラからのデータを待っています (Waiting for camera data)...**
|
| 115 |
+
|
| 116 |
+
姿勢分析のためにウェブカメラをオンにしてください (Please enable webcam for posture analysis)
|
| 117 |
+
""")
|
| 118 |
+
|
| 119 |
+
camera = cv2.VideoCapture(0)
|
| 120 |
+
|
| 121 |
+
while run:
|
| 122 |
+
_, frame = camera.read()
|
| 123 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 124 |
+
|
| 125 |
+
# Posture analysis
|
| 126 |
+
analyzed_frame, posture_status = analyze_posture(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
|
| 127 |
+
analyzed_frame = cv2.cvtColor(analyzed_frame, cv2.COLOR_BGR2RGB)
|
| 128 |
+
|
| 129 |
+
# Display in columns
|
| 130 |
+
with col1:
|
| 131 |
+
FRAME_WINDOW.image(analyzed_frame)
|
| 132 |
+
|
| 133 |
+
with col2:
|
| 134 |
+
status_placeholder.markdown(posture_status)
|
| 135 |
+
|
| 136 |
+
time.sleep(0.1)
|
| 137 |
+
|
| 138 |
+
camera.release()
|
| 139 |
+
cv2.destroyAllWindows()
|
| 140 |
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|