raspberrypi / picamera2

New libcamera based python library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'Picamera2' object has no attribute '_preview'

bartjan2008 opened this issue · comments

When running my python script that was perfectly running a week ago, it gives the error
AttributeError: 'Picamera2' object has no attribute '_preview'

from flask import Flask, render_template, url_for, request, Response
import numpy as np
import cv2
import io
import picamera2
import atexit
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(22, GPIO.OUT)

global global_lightOn
global_lightOn = False
flippedhorizontal = 0

camera = picamera2.Picamera2()
camera.configure(
camera.create_preview_configuration(
main={"format": "XRGB8888", "size": (3280, 2464)}
)
)
camera.start()
app = Flask(name)

def generate_frames():
while True:
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")

@app.route("/")
@app.route("/home")
def home():
return render_template("home.html")

@app.route("/up", methods=["POST", "GET"])
def up():
print("up")
return render_template("home.html")

@app.route("/down", methods=["POST", "GET"])
def down():
print("down")
return render_template("home.html")

@app.route("/home-focus", methods=["POST", "GET"])
def homefocus():
print("home-focus")
return render_template("home.html")

@app.route("/left", methods=["POST", "GET"])
def left():
print("left")
return render_template("home.html")

@app.route("/right", methods=["POST", "GET"])
def right():
print("right")
return render_template("home.html")

@app.route("/forward", methods=["POST", "GET"])
def forward():
print("forward")
return render_template("home.html")

@app.route("/back", methods=["POST", "GET"])
def back():
print("back")
return render_template("home.html")

@app.route("/home-xy", methods=["POST", "GET"])
def homexy():
print("home-xy")
return render_template("home.html")

@app.route("/light", methods=["POST", "GET"])
def light():
global global_lightOn
if global_lightOn:
print("light off")
GPIO.output(22, GPIO.LOW)
global_lightOn = False

else:
    print("light on")
    GPIO.output(22, GPIO.HIGH)
    global_lightOn = True
print("light")
return render_template("home.html")

@app.route("/flip-horizontal", methods=["POST", "GET"])
def fliphorizontal():
global flippedhorizontal
flippedhorizontal = flippedhorizontal + 90
print("flip-horizontal")
return render_template("home.html")

@app.route("/flip-vertical", methods=["POST", "GET"])
def flipvertical():
global flippedhorizontal
flippedhorizontal = flippedhorizontal - 90
print("flip-vertical")
return render_template("home.html")

@app.route("/stream")
def stream():
return Response(
generate_frames(), mimetype="multipart/x-mixed-replace; boundary=frame"
)

if name == "main":
app.run(host="0.0.0.0", threaded=True)

Expected behaviour
I expected the Pi to just run

Console Output, Screenshots
Traceback (most recent call last):
File "/home/raspi/picamera2-WebUI-Lite/app.py", line 19, in
picam2 = Picamera2()
^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 242, in init
camera_num = self.global_camera_info()[camera_num]['Num']
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
IndexError: list index out of range
Exception ignored in: <function Picamera2.del at 0x7f6f6d6f20>
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 404, in del
self.close()
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 604, in close
if self._preview:
^^^^^^^^^^^^^
AttributeError: 'Picamera2' object has no attribute '_preview'

Hardware :
Raspberry pi 4 8gb ram HQ camera

Additional context
I have already tried updating my Raspi OS and updating the picamera2 but no result