Gainward777 commited on
Commit
6213eaf
·
verified ·
1 Parent(s): bad89b8

Update pages/1_Live_Stream.py

Browse files
Files changed (1) hide show
  1. pages/1_Live_Stream.py +47 -49
pages/1_Live_Stream.py CHANGED
@@ -1,12 +1,7 @@
1
- import av
2
- import os
3
- import sys
4
  import streamlit as st
5
- from streamlit_webrtc import VideoHTMLAttributes, webrtc_streamer
6
- from aiortc.contrib.media import MediaRecorder
7
-
8
- BASE_DIR = os.path.abspath(os.path.join(__file__, '../../'))
9
- sys.path.append(BASE_DIR)
10
 
11
  from utils import get_mediapipe_pose
12
  from process_frame import ProcessFrame
@@ -14,62 +9,65 @@ from thresholds import get_thresholds_beginner, get_thresholds_pro
14
 
15
  st.title('AI Fitness Trainer: Squats Analysis')
16
 
 
17
  mode = st.radio('Select Mode', ['Beginner', 'Pro'], horizontal=True)
18
 
19
- thresholds = None
20
  if mode == 'Beginner':
21
  thresholds = get_thresholds_beginner()
22
  elif mode == 'Pro':
23
  thresholds = get_thresholds_pro()
24
 
 
25
  live_process_frame = ProcessFrame(thresholds=thresholds, flip_frame=True)
26
-
27
- # Initialize face mesh solution
28
  pose = get_mediapipe_pose()
29
 
30
- if 'download' not in st.session_state:
31
- st.session_state['download'] = False
 
32
 
33
- output_video_file = f'output_live.flv'
 
 
34
 
35
- # RTSP stream URL (replace with your RTSP stream URL)
36
- RTSP_URL = st.text_input("Enter RTSP Stream URL", "rtsp://your-stream-url")
 
 
 
 
37
 
38
- def video_frame_callback(frame: av.VideoFrame):
39
- frame = frame.to_ndarray(format="rgb24") # Decode and get RGB frame
40
- frame, _ = live_process_frame.process(frame, pose) # Process frame
41
- return av.VideoFrame.from_ndarray(frame, format="rgb24") # Encode and return BGR frame
 
42
 
43
- def out_recorder_factory() -> MediaRecorder:
44
- return MediaRecorder(output_video_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- if RTSP_URL:
47
- ctx = webrtc_streamer(
48
- key="Squats-pose-analysis",
49
- video_frame_callback=video_frame_callback,
50
- rtc_configuration={"iceServers": [{"urls": "stun:stun.l.google.com:19302"}]}, # Add this config
51
- media_stream_constraints={
52
- "video": {"frameRate": 30, "deviceId": RTSP_URL},
53
- "audio": False
54
- },
55
- video_html_attrs=VideoHTMLAttributes(autoPlay=True, controls=False, muted=False),
56
- out_recorder_factory=out_recorder_factory
57
- )
58
-
59
- download_button = st.empty()
60
-
61
- if os.path.exists(output_video_file):
62
- with open(output_video_file, 'rb') as op_vid:
63
- download = download_button.download_button('Download Video', data=op_vid, file_name='output_live.flv')
64
- if download:
65
- st.session_state['download'] = True
66
-
67
- if os.path.exists(output_video_file) and st.session_state['download']:
68
- os.remove(output_video_file)
69
- st.session_state['download'] = False
70
- download_button.empty()
71
- else:
72
- st.warning("Please provide a valid RTSP stream URL.")
73
 
74
 
75
  '''
 
1
+ import cv2
 
 
2
  import streamlit as st
3
+ import tempfile
4
+ import os
 
 
 
5
 
6
  from utils import get_mediapipe_pose
7
  from process_frame import ProcessFrame
 
9
 
10
  st.title('AI Fitness Trainer: Squats Analysis')
11
 
12
+ # Select mode
13
  mode = st.radio('Select Mode', ['Beginner', 'Pro'], horizontal=True)
14
 
15
+ thresholds = None
16
  if mode == 'Beginner':
17
  thresholds = get_thresholds_beginner()
18
  elif mode == 'Pro':
19
  thresholds = get_thresholds_pro()
20
 
21
+ # Initialize pose estimation and processing
22
  live_process_frame = ProcessFrame(thresholds=thresholds, flip_frame=True)
 
 
23
  pose = get_mediapipe_pose()
24
 
25
+ # Temporary file to save processed video
26
+ temp_dir = tempfile.TemporaryDirectory()
27
+ output_video_file = os.path.join(temp_dir.name, 'output_live.avi')
28
 
29
+ # Initialize video writer
30
+ fourcc = cv2.VideoWriter_fourcc(*'XVID')
31
+ video_writer = None
32
 
33
+ # Start video capture
34
+ cap = cv2.VideoCapture(0)
35
+ if not cap.isOpened():
36
+ st.error("Unable to access the camera")
37
+ else:
38
+ stframe = st.empty()
39
 
40
+ while True:
41
+ ret, frame = cap.read()
42
+ if not ret:
43
+ st.error("Failed to capture video")
44
+ break
45
 
46
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
47
+ processed_frame, _ = live_process_frame.process(frame, pose)
48
+
49
+ # Write to video file
50
+ if video_writer is None:
51
+ height, width, _ = processed_frame.shape
52
+ video_writer = cv2.VideoWriter(output_video_file, fourcc, 30, (width, height))
53
+ video_writer.write(cv2.cvtColor(processed_frame, cv2.COLOR_RGB2BGR))
54
+
55
+ # Display in Streamlit
56
+ stframe.image(processed_frame, channels="RGB")
57
+
58
+ # Stop button
59
+ if st.button("Stop"):
60
+ break
61
+
62
+ cap.release()
63
+ if video_writer:
64
+ video_writer.release()
65
+
66
+ # Download processed video
67
+ if os.path.exists(output_video_file):
68
+ with open(output_video_file, 'rb') as f:
69
+ st.download_button("Download Processed Video", f, file_name="output_live.avi")
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
 
73
  '''