# Flask ip cam - freezing

**URL:** <https://forum.opencv.org/t/flask-ip-cam-freezing/7990>\
**Category:** Python\
**Tags:** videoio, flask, iis\
**Created:** [March 18, 2022, 7:04am UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990 "2022-03-18T07:04:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antrac1t](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Antrac1t](https://forum.opencv.org/u/Antrac1t)\
**Post date:** [March 18, 2022, 7:04am UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/1 "2022-03-18T07:04:05Z")

</div>

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

```auto
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

```auto
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

```auto
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

```auto
            <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>

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 18, 2022, 1:32pm UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/2 "2022-03-18T13:32:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Antrac1t](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Antrac1t](https://forum.opencv.org/u/Antrac1t)\
**Post date:** [March 18, 2022, 2:02pm UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/3 "2022-03-18T14:02:11Z")

</div>

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

```auto
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')

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 18, 2022, 2:05pm UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/4 "2022-03-18T14:05:26Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Antrac1t](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Antrac1t](https://forum.opencv.org/u/Antrac1t)\
**Post date:** [March 18, 2022, 7:44pm UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/5 "2022-03-18T19:44:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mshabunin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mshabunin/32/15_2.png) [@mshabunin](https://forum.opencv.org/u/mshabunin)\
**Post date:** [March 19, 2022, 8:41pm UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/6 "2022-03-19T20:41:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Antrac1t](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Antrac1t](https://forum.opencv.org/u/Antrac1t)\
**Post date:** [March 20, 2022, 8:12am UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/7 "2022-03-20T08:12:09Z")

</div>

thx , tomorrow i will update code and post log

---

<div class="post-metadata">

**Author:** ![Antrac1t](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Antrac1t](https://forum.opencv.org/u/Antrac1t)\
**Post date:** [March 24, 2022, 11:31am UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/8 "2022-03-24T11:31:18Z")

</div>

ok seems that problem is not with cv2 or UV4L but with IIS and wfastcgi 😕 … 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 …

---

<div class="post-metadata">

**Author:** ![Antrac1t](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Antrac1t](https://forum.opencv.org/u/Antrac1t)\
**Post date:** [March 24, 2022, 12:12pm UTC](https://forum.opencv.org/t/flask-ip-cam-freezing/7990/9 "2022-03-24T12:12:39Z")

</div>

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
