-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update video URL in Home component and add recording functiona…
…lity
- Loading branch information
1 parent
18f695d
commit 5e6daab
Showing
7 changed files
with
101 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import os | ||
import cv2 | ||
import threading | ||
from datetime import datetime | ||
|
||
# Lock for thread-safe access to the recording flag and writer | ||
lock = threading.Lock() | ||
out = None | ||
is_recording = False | ||
|
||
# Ensure recordings directory exists | ||
os.makedirs('./recordings', exist_ok=True) | ||
|
||
|
||
def generate_frames(): | ||
""" | ||
Captures frames from the default camera feed, encodes them as JPEG images, and yields them as byte streams. | ||
Yields: | ||
bytes: A sequence of JPEG-encoded image frames as byte streams, suitable for streaming in an HTTP response. | ||
Returns: | ||
None | ||
""" | ||
|
||
global out, is_recording | ||
cap = cv2.VideoCapture(0) | ||
|
||
while True: | ||
success, frame = cap.read() | ||
if not success: | ||
break | ||
|
||
with lock: | ||
if is_recording and out is not None: | ||
out.write(frame) | ||
|
||
ret, buffer = cv2.imencode('.jpg', frame) | ||
frame = buffer.tobytes() | ||
|
||
yield (b'--frame\r\n' | ||
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') | ||
|
||
cap.release() | ||
|
||
|
||
def start_recording() -> None: | ||
""" | ||
Starts recording video frames from the default camera feed and saves them to a file. | ||
Returns: | ||
None | ||
""" | ||
|
||
global out, is_recording | ||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | ||
filename = f'./recordings/output_{timestamp}.avi' | ||
|
||
fourcc = cv2.VideoWriter_fourcc(*'XVID') | ||
with lock: | ||
out = cv2.VideoWriter(filename, fourcc, 20.0, (640, 480)) | ||
is_recording = True | ||
|
||
|
||
def stop_recording() -> None: | ||
""" | ||
Stops recording video frames and releases the video writer. | ||
Returns: | ||
None | ||
""" | ||
|
||
global out, is_recording | ||
with lock: | ||
if out is not None: | ||
out.release() | ||
out = None | ||
is_recording = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters