Skip to content

Commit

Permalink
Bullet 10 fix live errors (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElBeenMachine authored Dec 21, 2023
2 parents a52c9e9 + cc3669f commit 0bdbf11
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
34 changes: 12 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import platform
from aiohttp import web
from utils import *
import os
import subprocess
import asyncio
import io
import base64
import time

VERSION = "1.6.4"
VERSION = "1.7.2"

# Create a new Socket.IO server with specified port
sio = socketio.AsyncServer(cors_allowed_origins='*')
app = web.Application()
sio.attach(app)

# Set up the camera
from picamera2 import Picamera2
cam = Picamera2()
cam.set_controls({"ExposureTime": 1000, "AnalogueGain": 1.0})

# Define a connection event
@sio.event
async def connect(sid, environ):
Expand All @@ -34,31 +36,19 @@ async def CAPTURE_IMAGE(sid, data):
# Stream event
@sio.event
async def START_STREAM(sid):
# Initialise stream to store encoded frames
stream = io.BytesIO
start_time = time.time()
max_duration = 9

while True:
while time.time() - start_time < max_duration:
try:
# Rewind the stream for reading and encode to base64
stream.seek(0)
frameData = base64.b64encode(captureFrame(stream))

# Send the frame over socket
await sio.emit("VIDEO_FRAME", {"frame_data": frameData})
await asyncio.sleep(0.1) # Rate limiting

# Reset the stream for the next frame
stream.seek(0)
stream.truncate()
await sio.emit("VIDEO_FRAME", {"frame_data": captureFrame(cam=cam)})
await asyncio.sleep(0.1) # Rate limiting

except Exception as e:
print(f"Streaming error or user disconnected:{e}")
break

finally:
cam.close()
stream.close()

# Define an error event
@sio.event
def event_error(sid, error):
Expand Down
41 changes: 21 additions & 20 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
import base64
from datetime import datetime, timedelta

# Set up the camera
from picamera2 import Picamera2
cam = Picamera2()
cam.set_controls({"ExposureTime": 1000, "AnalogueGain": 1.0})

def captureImage(x = 1920, y = 1920, time = datetime.now() + timedelta(0, 10)):
def captureImage(cam, x = 1920, y = 1920, time = datetime.now() + timedelta(0, 10)):
wait_state = True
while wait_state:
if time <= datetime.now():
Expand All @@ -20,27 +15,33 @@ def captureImage(x = 1920, y = 1920, time = datetime.now() + timedelta(0, 10)):
cam.start()
print("🟢 | Capturing image")
cam.capture_file("img.jpg")
cam.stop()

# Open the image and return the data as a base64 encoded string
with open("img.jpg", "rb") as image_file:
data = image_file.read()
return data
except Exception as e:
print(f"🔴 | {e}")
finally:
cam.stop()


def captureFrame(stream):

# Configure video settings
cam.configure(cam.create_video_configuration())
cam.resolution = (1920, 1080)
cam.start()

# Capture frame into stream
cam.capture(stream, format='jpeg', use_video_port=True)

# Load image from stream
frameData = stream.read()
def captureFrame(cam):
try:
# Configure camera
camera_config = cam.create_preview_configuration(main={"size": (1920, 1080)})
cam.configure(camera_config)

# Configure video settings
cam.start()

return frameData
# Capture frame into stream
cam.capture_file("live_frame.jpg")

# Open the image and return the data as a base64 encoded string
with open("live_frame.jpg", "rb") as image_file:
data = image_file.read()
return data
finally:
# Close Camera
cam.stop()

0 comments on commit 0bdbf11

Please sign in to comment.