Camera.read() Frame reading function freez

Hello,
i have read RTSP stream of my ESP32Cam module for image processing and object detection project.

OpenCV Version :- 4.8.1.78
Python Verson :- 3.11
Camera : ESP32Cam Module running Tasmota firmware V 13.1.0
OS WIndows 10 and ubuntu 23.04

workflow of my code is as below.

  1. Opening camera Stream
  2. Checking camera is opened or not.
  3. if camera stream not opened then retry after 10 seconds
  4. if camera stream is opened then start reading frame.
  5. if frame read status is OK then display frame.
  6. if frame read status is not OK then restart release camera and retry after 10 seconds.

my code freeze at point no 4 sometime.
frequency of freezing code is once withing two or three days.

my code is as below

import numpy as np
import cv2
import time

brk = 0
CamUrl = 'rtsp://192.168.1.211:8554/mjpeg/1'


def work_with_captured_video(camera):
    global brk
    while True:
        print("Frame Reading Started")
        ret, frame = camera.read()
        print("Frame Reading Completed")
        if not ret:
            print("Frame Reading Fail")
            camera.release()
            return False
        else:
            print("Showing Frame")
            cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            print("Exit Key pressed")
            brk = 1
            break
    return True


def cam_open():
    print("Opening Camera")
    while True:
        camera = cv2.VideoCapture(CamUrl)
        if camera.isOpened():
            print('Camera is opened')
            response = work_with_captured_video(camera)
            if not response:
                print("frame failed wait for 10 Seconds")
                time.sleep(10)
                continue
        else:
            print('Camera not opened retry after 10 Seconds')
            camera.release()
            time.sleep(10)
            continue
        if brk:
            print("Good Bye")
            break


while True:
    cam_open()

problem i faced is my code freeze at camera.read() function. after printing A as showing in below line of code.

A print(“Frame Reading Started”)
B ret, frame = camera.read()
C print(“Frame Reading Completed”)

i thing during camera.read() function execution RTSP stream get disconnected or something happen with ESP32Cam module WiFi connectivity. however camera is working fine every time i have checked. so it loose connectivity for a wile which create this problem.

how to implement timeout function in this code so if code freeze during camera.read() function for more then predefined time, it will return with error or something like that.

if i restart my script then it will start working again.

my final goal is to implement yolov8 inference on this camera.

Hi are you using the FFMpeg backend or GStreamer? If FFMpeg can you try ffplay and see if you have the same issue?

I’m asking because I have been observing weird stalls with RTSP but they are incosistent sometimes after a day sometimes after a week. I thought it was something I was doing wrong (and it could be as I’m always changing the combination of the OpenCV and FFMpeg I’m running) but it “could” be a bug in the RTSP streaming code. If you don’t have the same issue with ffplay then I will start looking into it.

I am not pro user of python & opencv so may be my question seems dumb.

I don’t know how to use ffplay in my code. Can you provide some documentation reference for that.

My observation is when during execution of ret, frame = camera.read()
My camera got disconnected or something bad happening with RTSP stream. This. Cause not reaching end of camera.read() function.
In that case if camera.read timeout velidation mechanism can resolve this kind of problem.

So is there any way to do watchdog function in python which detects freeze of code and restart the script??

I would expect read to return after a failure without a watchdog function being required.

If you have FFMpeg installed then ffplay is a command line application you can just call it as

ffplay rtsp://…

and leave it running.

The point of the test would be to check ffplay which is built against the same version of FFMpeg as OpenCV to see if the OpenCV implementation is buggy.

Installation will depend on your OS, if its linux then just use the package manager if windows you can download from

but you won’t be able to get the same version as is used in OpenCV so the test may be invalid.

Hi,
You’d better review construction of your code.
You have too many ‘while True’ loop.

That’s true.
It’s because I wanted to re-connect camera if Camera read is not open or frame not read successfully. & if both condition are true then display captured frame on real time.

I don’t think it will cause of that freeze problem.

I have changed RTSP Stream with HTTP Stream and now it seems work without freezing. code is working since more then 48 hours without any problems.

code is as below…

import numpy as np
import cv2
import time

brk = 0
#CamUrl = 'rtsp://192.168.1.211:8554/mjpeg/1'
CamUrl = "http://192.168.1.211:81/cam.mjpeg"

def work_with_captured_video(camera):
    global brk
    while True:
        print("Frame Reading Started")
        ret, frame = camera.read()
        print("Frame Reading Completed")
        if not ret:
            print("Frame Reading Fail")
            camera.release()
            return False
        else:
            print("Showing Frame")
            cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            print("Exit Key pressed")
            brk = 1
            break
    return True


while True:
    camera = cv2.VideoCapture(CamUrl)
    if camera.isOpened():
        print('Camera is opened')
        response = work_with_captured_video(camera)
        if not response:
            print("frame failed wait for 10 Seconds")
            time.sleep(10)
            continue
    else:
        print('Camera not opened retry after 10 Seconds')
        camera.release()
        time.sleep(10)
        continue
    if brk:
        print("Good Bye")
        break

I have also tried running ffplay using command prompt as you suggested but it seems not working.

output of command prompt with RTSP Stream is as below

C:\Users\ashis\Desktop\ff\bin>ffplay -i rtsp://192.168.1.211:8554/mjpeg/1 -vf scale=1280x720
ffplay version n4.4.4-6-gd5fa6e3a91-20231019 Copyright (c) 2003-2023 the FFmpeg developers
  built with gcc 13.2.0 (crosstool-NG 1.25.0.232_c175b21)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --disable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --disable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --disable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --disable-vulkan --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20231019
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
    nan    :  0.000 fd=   0 aq=    0KB vq=    0KB sq=    0B f=0/0

but when i tried to use same ffplay command with http stream video started normally.

C:\Users\ashis\Desktop\ff\bin>ffplay.exe -i http://192.168.1.211:81/cam.mjpeg
ffplay version n4.4.4-6-gd5fa6e3a91-20231019 Copyright (c) 2003-2023 the FFmpeg developers
  built with gcc 13.2.0 (crosstool-NG 1.25.0.232_c175b21)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --disable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --disable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --disable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --disable-vulkan --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20231019
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mpjpeg, from 'http://192.168.1.211:81/cam.mjpeg': f=0/0
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 800x600, 25 tbr, 25 tbn, 25 tbc
[swscaler @ 000001e3cfb76000] deprecated pixel format used, make sure you did set range correctly
   2.64 M-V:  1.920 fd=   0 aq=    0KB vq=    0KB sq=    0B f=0/0