1000 Nerd Street
1000 Nerd Street

Reputation: 1

Raspberry Pi Camera won't Initlize

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'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
        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.

Upvotes: 0

Views: 184

Answers (2)

binaryjam
binaryjam

Reputation: 1

Selecting debug on the app.run allows for auto reloading. So the whole codebase runs again, and it didnt close the cam, this is supposed to be for code edits but I found it just did it all the time.

In theory moving the init to the main bit should stop it, it doesnt.

I have an init function that checks if the picam var is nothing, if so it sets it, otherwise does nothing, as its been set already. This works.

As does setting the debug to false.

Upvotes: 0

Luca Mattia Ferrari
Luca Mattia Ferrari

Reputation: 357

I have a similar structure for my python object detection app and had the following error in the logs:

ERROR Camera camera.cpp:685 
Camera in Running state trying acquire() requiring state Available 
Camera __init__ sequence did not complete.

Didn't realize I was calling multiple time the Picamera2 initialization (with each refresh of the page) so the camera was actually busy from the first initialization.

I moved out the Picamera2 initialization (just outside the app.Flask statement) and everything is working fine now.

You seem to initialize that only once, but would give my workaround a try.

Upvotes: 0

Related Questions