I am trying to get the video feed off of my raspberry pi camera on to my laptop or a cloud server. My camera works, all of the packages are up to date, the preview also works but every time run the program it keeps having this error; “ERROR in app: Camera initiation failed: Camera init sequence did not complete” I have tried everything that I can think of and there is something on the html page where the video should go but there’s nothing there.
Here’s my script:
from flask import Flask, Response, render_template
from picamera2 import Picamera2
import cv2
import time
app = Flask(__name__)
def initialize_camera():
try:
camera = Picamera2()
config = camera.create_preview_configuration(main={"format": "RGB888", "size": (640, 480)})
camera.configure(config)
camera.start()
time.sleep(2) # Allow camera to initialize
return camera
except Exception as e:
app.logger.error(f"Camera initialization failed: {e}")
return None
camera = initialize_camera()
def generate_frames():
while camera:
try:
frame = camera.capture_array()
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rnrn')
except Exception as e:
app.logger.error(f"Error capturing frame: {e}")
break
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
I have check all of the connections, I made sure all of my packages where up to date (this is a brand new pi 4) and I’ve been in a circular conversation with chatGPT and nothing works.