Flask ip cam - freezing

Hi Folks,
i am working on web app to control material on correct positions. All cameras are on Rpi zeros where running UV4L streaming server (uv4l working like charm, stream works many days without any issues). Flask runs on win server (no any problem with another flask apps). When run these cameras on flask webpage using CV2 then all working fine but just some time, like 1-2 mins then all streams are stopped and needs to refresh page. So now i am not sure if its problem of flask or cv2. thx for any help

BR

Antra

main.py

RpiIpsList = GetRpiIps()

@app.route("/cameras", methods=["GET"])
def cameras(): 
    return render_template('cameras.html',camera_list=len(RpiIpsList), RpiIpsList=RpiIpsList)

@app.route('/video_feed/<string:campera_ip>/', methods=["GET"])
def video_feed(campera_ip):
    #Video streaming route. Put this in the src attribute of an img tag
    Status=RpiAvailability(campera_ip)
    if Status==1:     
        return Response(gen_frames(campera_ip), mimetype='multipart/x-mixed-replace; boundary=frame')
    else:

        return "Camera not avaliable"+ campera_ip


sql.py

import pymssql

server = "****"
user = "****"
password = "****"
DB = "****"

def GetRpiIps():
    conn = pymssql.connect(server, user, password, DB)
    cur = conn.cursor()
    cur.execute("select RpiName,RpiIP,Udt from [OPENCV].[dbo].[RPI_CamList]")

    results = cur.fetchall()
    conn.close
    return results

cameras.py

import cv2
import socket

def RpiAvailability(Ip):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(0.5)
    result = sock.connect_ex((Ip, 8080))
    if result == 0:
        return(1)
    else:
        return(0)

def gen_frames(Ip):  # generate frame by frame from camera
    camera = cv2.VideoCapture('http://'+ Ip +':8080/stream/video.mjpeg')  
    camera.set(cv2.CAP_PROP_BUFFERSIZE, 0)
    while True:
        success, image = camera.read()
        ret, jpeg = cv2.imencode('.jpg', image)
        frame = jpeg.tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

cameras.html

            <ol>


                {% for camera_number in range(0, camera_list) %}
                <li>
                    {{RpiIpsList[camera_number][0]}} - {{RpiIpsList[camera_number][1]}}, last connection  {{RpiIpsList[camera_number][2]}}<br />
                    <img src="{{ url_for('video_feed', campera_ip=RpiIpsList[camera_number][1]) }}"><br />
             
                </li>
                {% endfor %}


            </ol>

your use of VideoCapture lacks all error checking.
assert cap.isOpened()
if not success: break

then you should use logging to see what your code does after those 1-2 minutes.

see if you can pin down the exact number of seconds for this issue to happen.

there is too much going on for this to be solely an OpenCV problem. likely it’s not an OpenCV problem but some of your networking stuff decides to run into a timeout.

i changed code , but actually streaming was stopped without any breakpoint (in debugging mode in VS2019) :confused:

def gen_frames(Ip):  # generate frame by frame from camera
    camera = cv2.VideoCapture('http://'+ Ip +':8080/stream/video.mjpeg')  
    camera.set(cv2.CAP_PROP_BUFFERSIZE, 0)
    assert camera.isOpened()
    while True:
        success, image = camera.read()
        if not success: 
            break
        ret, jpeg = cv2.imencode('.jpg', image)
        frame = jpeg.tobytes()
        yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

i dont understand, why you try to re-stream, what are already ip cameras
is this really nessecary ? why not write html, that refers the original stream ?

because from video i am running CV code for recognize material and find cosmetic damage which will be shown in new stream in Flask :confused: .

Set environment variables OPENCV_VIDEOIO_DEBUG=1 and OPENCV_LOG_LEVEL=debug and check logs for more details. Probably some FFmpeg parameters should be tuned.

thx , tomorrow i will update code and post log

ok seems that problem is not with cv2 or UV4L but with IIS and wfastcgi :confused: … when i run project in debug mode in VS then all working withotu any freez, but after deploy and run application from server then stream start freezing …

ok seems that issue is IIS ,FastCGI settings where is Activity timeout, when i increase timeout then video stream works longer time … so topic can be closed and i will try to solve issue on IIS forum . thx guys for help and time