-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_camera.py
71 lines (58 loc) · 1.85 KB
/
stream_camera.py
1
2
3
4
5
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
import cv2
import boto3
import time
import numpy as np
import json
import signal
import sys
# Initialize AWS clients
kinesis_video = boto3.client('kinesisvideo', region_name='us-west-2')
stream_name = 'test-stream'
running = True
def signal_handler(sig, frame):
global running
print('\nStopping stream...')
running = False
signal.signal(signal.SIGINT, signal_handler)
def stream_camera():
global running
cap = cv2.VideoCapture(0)
try:
# Get Kinesis Video Stream endpoint
endpoint = kinesis_video.get_data_endpoint(
StreamName=stream_name,
APIName='PUT_MEDIA'
)['DataEndpoint']
# Create Kinesis Video client for PutMedia
kvs_client = boto3.client(
'kinesisvideo',
endpoint_url=endpoint,
region_name='us-west-2'
)
print("Streaming started. Press Ctrl+C to stop.")
while running:
ret, frame = cap.read()
if not ret:
break
# Convert frame to JPEG format
_, buffer = cv2.imencode('.jpg', frame)
data = buffer.tobytes()
try:
# Use PutMedia API directly
kvs_client.put_media(
StreamName=stream_name,
Data=data,
ProducerTimestamp=int(time.time() * 1000),
FragmentNumber=str(int(time.time())),
ContainerFormat='JPEG'
)
except Exception as e:
print(f"Error streaming: {e}")
time.sleep(0.1) # Reduce frame rate
except Exception as e:
print(f"Error: {e}")
finally:
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
stream_camera()