0

I am trying to stream the video feed of my FLIR Lepton to a webpage. I got myself some code that I don't yet fully understand. I am new to Flask. However, I got the camera working in VLC and a simple python script that captures 200 frames and saves them to a video file. As stated in the title, I use OpenCV to do all of this, but I seem unable to "get" the camera. When trying to connect to the website, I get the "could not read" printout. I should also add that I am also experiencing problems, although different issues, with the PiCam module when trying the same thing.

What makes this even more mysterious is that the camera seems to go into captivity. The LED starts to flash, much as it would be when capturing video in the "simple" script. This is the source of the file I am experiencing problems with:

from flask import Flask, render_template, Response
import cv2
from flirpy.camera.lepton import Lepton

fcamera = Lepton()


app = Flask(__name__)

camera = cv2.VideoCapture(1)  # use 0 for web camera
#  for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of camera
# for local webcam use cv2.VideoCapture(0)

def gen_frames():  # generate frame by frame from camera
   while True:
       # Capture frame-by-frame
       success, frame = camera.read()  # read the camera frame
       if not success:
           print("could not read")
           break
       else:
           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')  # concat frame one by one and show the result


@app.route('/video_feed')
def video_feed():
   #Video streaming route. Put this in the src attribute of an img tag
   return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/')
def index():
   """Video streaming home page."""
   return render_template('index.html')


if __name__ == '__main__':
   app.run(debug=True)


And this is the simple file that runs without problems:

from flirpy.camera.lepton import Lepton
import matplotlib.pyplot as plt
import cv2 as cv
from time import sleep
cap = cv.VideoCapture(1)
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
i = 0
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # write the flipped frame
    out.write(frame)
    if i > 200:
        break
    i = i + 1
# Release everything if the job is finished
cap.release()
out.release()


I did not attempt to use any of these scripts simultaneously. Also, VLC is not running.

Thanks for any help!

javamaster10000

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • some cameras may need time for `warm up` and maybe you should skip some frames when `ret` is False. – furas Jun 12 '21 at 21:20
  • you could also ask on similar portal for [Raspberry Pi](https://raspberrypi.stackexchange.com/) or on RPi's [official forum](https://www.raspberrypi.org/forums/) – furas Jun 12 '21 at 22:19

0 Answers0