Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,151 +4,151 @@ import subprocess
|
|
| 4 |
subprocess.run(['chmod', '+x', 'setup.sh'])
|
| 5 |
subprocess.run(['bash', 'setup.sh'], check=True)
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
# import streamlit as st
|
| 153 |
# import cv2
|
| 154 |
# import mediapipe as mp
|
|
|
|
| 4 |
subprocess.run(['chmod', '+x', 'setup.sh'])
|
| 5 |
subprocess.run(['bash', 'setup.sh'], check=True)
|
| 6 |
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import cv2
|
| 9 |
+
import mediapipe as mp
|
| 10 |
+
import numpy as np
|
| 11 |
+
import tempfile
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
# Initialize MediaPipe Pose
|
| 15 |
+
mp_pose = mp.solutions.pose
|
| 16 |
+
pose = mp_pose.Pose(static_image_mode=False, model_complexity=1, enable_segmentation=True, min_detection_confidence=0.5, min_tracking_confidence=0.5)
|
| 17 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 18 |
+
|
| 19 |
+
def calculate_angle_between_vectors(v1, v2):
|
| 20 |
+
unit_vector_1 = v1 / np.linalg.norm(v1)
|
| 21 |
+
unit_vector_2 = v2 / np.linalg.norm(v2)
|
| 22 |
+
dot_product = np.dot(unit_vector_1, unit_vector_2)
|
| 23 |
+
angle = np.arccos(dot_product)
|
| 24 |
+
return np.degrees(angle)
|
| 25 |
+
|
| 26 |
+
def process_video(video_path):
|
| 27 |
+
cap = cv2.VideoCapture(video_path)
|
| 28 |
+
output_dir = tempfile.mkdtemp()
|
| 29 |
+
|
| 30 |
+
current_phase = "Not Setup phase"
|
| 31 |
+
prev_wrist_left_y = None
|
| 32 |
+
prev_wrist_right_y = None
|
| 33 |
+
top_backswing_detected = False
|
| 34 |
+
mid_downswing_detected = False
|
| 35 |
+
ball_impact_detected = False
|
| 36 |
+
top_backswing_frame = -2
|
| 37 |
+
mid_downswing_frame = -2
|
| 38 |
+
ball_impact_frame = -2
|
| 39 |
+
|
| 40 |
+
BALL_IMPACT_DURATION = 2 # Duration in frames to display Ball Impact phase
|
| 41 |
+
|
| 42 |
+
MIN_MOVEMENT_THRESHOLD = 0.01
|
| 43 |
+
HIP_NEAR_THRESHOLD = 0.05
|
| 44 |
+
MID_SWING_THRESHOLD = 0.05
|
| 45 |
+
|
| 46 |
+
saved_phases = set()
|
| 47 |
+
|
| 48 |
+
while cap.isOpened():
|
| 49 |
+
ret, frame = cap.read()
|
| 50 |
+
if not ret:
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
frame_no = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
|
| 54 |
+
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 55 |
+
result = pose.process(image_rgb)
|
| 56 |
+
h, w, c = frame.shape
|
| 57 |
+
|
| 58 |
+
if result.pose_landmarks:
|
| 59 |
+
mp_drawing.draw_landmarks(
|
| 60 |
+
frame, result.pose_landmarks, mp_pose.POSE_CONNECTIONS,
|
| 61 |
+
mp_drawing.DrawingSpec(color=(255, 0, 0), thickness=2, circle_radius=2),
|
| 62 |
+
mp_drawing.DrawingSpec(color=(255, 0, 255), thickness=2, circle_radius=2)
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
landmarks = result.pose_landmarks.landmark
|
| 66 |
+
wrist_left_y = landmarks[mp_pose.PoseLandmark.LEFT_WRIST].y
|
| 67 |
+
wrist_right_y = landmarks[mp_pose.PoseLandmark.RIGHT_WRIST].y
|
| 68 |
+
hip_left_y = landmarks[mp_pose.PoseLandmark.LEFT_HIP].y
|
| 69 |
+
hip_right_y = landmarks[mp_pose.PoseLandmark.RIGHT_HIP].y
|
| 70 |
+
shoulder_left_y = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER].y
|
| 71 |
+
shoulder_right_y = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER].y
|
| 72 |
+
|
| 73 |
+
hip_y_avg = (hip_left_y + hip_right_y) / 2
|
| 74 |
+
shoulder_y_avg = (shoulder_left_y + shoulder_right_y) / 2
|
| 75 |
+
mid_swing_y = (shoulder_y_avg + hip_y_avg) / 2
|
| 76 |
+
|
| 77 |
+
# Ensure the current phase persists for a few more milliseconds if it's Ball Impact
|
| 78 |
+
if ball_impact_detected and frame_no <= ball_impact_frame + BALL_IMPACT_DURATION:
|
| 79 |
+
current_phase = "Ball impact phase"
|
| 80 |
+
elif (abs(wrist_left_y - hip_y_avg) < HIP_NEAR_THRESHOLD and abs(wrist_right_y - hip_y_avg) < HIP_NEAR_THRESHOLD):
|
| 81 |
+
if prev_wrist_left_y is not None and prev_wrist_right_y is not None:
|
| 82 |
+
if (abs(wrist_left_y - prev_wrist_left_y) < MIN_MOVEMENT_THRESHOLD and abs(wrist_right_y - prev_wrist_right_y) < MIN_MOVEMENT_THRESHOLD):
|
| 83 |
+
if mid_downswing_detected and frame_no > mid_downswing_frame:
|
| 84 |
+
current_phase = "Ball impact phase"
|
| 85 |
+
ball_impact_detected = True
|
| 86 |
+
ball_impact_frame = frame_no
|
| 87 |
+
else:
|
| 88 |
+
current_phase = "Setup phase"
|
| 89 |
+
top_backswing_detected = False
|
| 90 |
+
mid_downswing_detected = False
|
| 91 |
+
else:
|
| 92 |
+
current_phase = ""
|
| 93 |
+
else:
|
| 94 |
+
if mid_downswing_detected and frame_no > mid_downswing_frame:
|
| 95 |
+
current_phase = "Ball impact phase"
|
| 96 |
+
ball_impact_detected = True
|
| 97 |
+
ball_impact_frame = frame_no
|
| 98 |
+
else:
|
| 99 |
+
current_phase = "Setup phase"
|
| 100 |
+
top_backswing_detected = False
|
| 101 |
+
mid_downswing_detected = False
|
| 102 |
+
elif (abs(wrist_left_y - mid_swing_y) < MID_SWING_THRESHOLD and abs(wrist_right_y - mid_swing_y) < MID_SWING_THRESHOLD and not top_backswing_detected and not ball_impact_detected):
|
| 103 |
+
current_phase = "Mid backswing phase"
|
| 104 |
+
elif (wrist_left_y < shoulder_left_y and wrist_right_y < shoulder_right_y and not mid_downswing_detected and not ball_impact_detected):
|
| 105 |
+
current_phase = "Top backswing phase"
|
| 106 |
+
top_backswing_detected = True
|
| 107 |
+
top_backswing_frame = frame_no
|
| 108 |
+
elif (abs(wrist_left_y - mid_swing_y) < MID_SWING_THRESHOLD and abs(wrist_right_y - mid_swing_y) < MID_SWING_THRESHOLD and top_backswing_detected and frame_no > top_backswing_frame):
|
| 109 |
+
current_phase = "Mid downswing phase"
|
| 110 |
+
mid_downswing_detected = True
|
| 111 |
+
mid_downswing_frame = frame_no
|
| 112 |
+
elif (wrist_left_y < shoulder_left_y and wrist_right_y < shoulder_right_y and ball_impact_detected and frame_no > ball_impact_frame):
|
| 113 |
+
current_phase = "Follow through phase"
|
| 114 |
+
else:
|
| 115 |
+
current_phase = ""
|
| 116 |
+
|
| 117 |
+
prev_wrist_left_y = wrist_left_y
|
| 118 |
+
prev_wrist_right_y = wrist_right_y
|
| 119 |
+
|
| 120 |
+
cv2.putText(frame, f"Phase: {current_phase}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
|
| 121 |
+
|
| 122 |
+
# Save the frame for each detected phase
|
| 123 |
+
if current_phase and current_phase not in saved_phases:
|
| 124 |
+
phase_filename = os.path.join(output_dir, f"{current_phase.replace(' ', '_')}.png")
|
| 125 |
+
cv2.imwrite(phase_filename, frame)
|
| 126 |
+
saved_phases.add(current_phase)
|
| 127 |
+
|
| 128 |
+
cap.release()
|
| 129 |
+
cv2.destroyAllWindows()
|
| 130 |
+
pose.close()
|
| 131 |
+
|
| 132 |
+
return output_dir
|
| 133 |
+
|
| 134 |
+
st.title("Golf Swing Phase Detection")
|
| 135 |
+
st.write("Upload a video to detect different phases of a golf swing.")
|
| 136 |
+
|
| 137 |
+
video_file = st.file_uploader("Upload Video", type=["mp4", "avi", "mov", "mkv"])
|
| 138 |
+
|
| 139 |
+
if video_file is not None:
|
| 140 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 141 |
+
tfile.write(video_file.read())
|
| 142 |
+
tfile_path = tfile.name
|
| 143 |
+
|
| 144 |
+
st.write("Processing video...")
|
| 145 |
+
output_dir = process_video(tfile_path)
|
| 146 |
+
|
| 147 |
+
st.write("Detected phases saved to:", output_dir)
|
| 148 |
+
st.write("Example frames from detected phases:")
|
| 149 |
|
| 150 |
+
for phase_image in os.listdir(output_dir):
|
| 151 |
+
st.image(os.path.join(output_dir, phase_image), caption=phase_image)
|
| 152 |
# import streamlit as st
|
| 153 |
# import cv2
|
| 154 |
# import mediapipe as mp
|