import streamlit as st from streamlit_webrtc import WebRtcMode, webrtc_streamer import cv2 import numpy as np def distance_calculate(p1, p2): """p1 and p2 in format (x1, y1) and (x2, y2) tuples""" dis = ((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2) ** 0.5 return dis def push_up_count_logic(nose, right_wrist, right_shoulder): if distance_calculate(right_shoulder, right_wrist) < 130: return 1 elif push_up_start and distance_calculate(right_shoulder, right_wrist) > 250: return 0 else: return 0 # Define initial values push_up_start = 0 push_up_count = 0 def video_frame_callback(frame): global push_up_start, push_up_count # Dummy values for demonstration nose = (100, 100) right_wrist = (200, 200) right_shoulder = (150, 150) push_up_count += push_up_count_logic(nose, right_wrist, right_shoulder) # Display the push-up count on the video frame image = frame.to_ndarray(format="bgr24") font = cv2.FONT_HERSHEY_SIMPLEX org = (50, 100) font_scale = 1 color = (255, 0, 0) thickness = 2 image = cv2.putText( image, f"Push-up count: {push_up_count}", org, font, font_scale, color, thickness, cv2.LINE_AA, ) return av.VideoFrame.from_ndarray(image, format="bgr24") def main(): st.title("AI Push-up Counter") webrtc_ctx = webrtc_streamer( key="pushup", mode=WebRtcMode.SENDRECV, # video_transformer_factory=None, video_frame_callback=video_frame_callback, # async_processing=True, ) if not webrtc_ctx.state.playing: return if __name__ == "__main__": main()