简体   繁体   中英

Python CV2 reads out-of-date frames from video stream

I am using Python 3.9 and Open-CV (cv2) to read frames from a video stream and save them as JPGs.

My program seems to run OK. It captures the video stream fine, obtains frames, and saves them as JPGs.

However, the frames it is obtaining from the stream are out-of-date - sometimes by several minutes. The clock in the video stream is running accurately, but the clock displays in the JPGs are all identical (to the second - but one or more minutes prior to the datetime in the program's "print()" output (and the saved JPG file time), and moving objects that were in view at the time they were saved are missing completely.

Strangely:

  1. The JPG images are not identical in size. They grow by 10K - 20K as the sequence progresses. Even though they look identical to the eye, they show significant difference when compared using CV2 - but no difference if compared using PIL (which is about 10 - 15 times slower for image comparisons).
  2. The camera can be configured to send a snapshot by email when it detects motion. These snapshots are up-to-date, and show moving objects that were in frame at the time (but no clock display). Enabling or disabling this facility has no effect on the out-of-date issue with JPGs extracted from the video stream. And, sadly, the snapshots are only about 60K, and too low resolution for our purposes (which is an AI application that needs images to be 600K or more).

The camera itself is ONVIF - and things like PTZ work nicely from Python code. Synology Surveillance Station works really well with it in every aspect. This model has reasonably good specs - zoom and good LPR anti-glare functionality. It is made in China - but I don't want to be 'a poor workman who blames his tools'.

Can anyone spot something in the program code that may be causing this?

Has anyone encountered this issue, and can suggest a work-around or different library / methodology?

(And if it is indeed an issue with this brand / model of camera, you are welcome to put in a plug for a mid-range LPR camera that works well for you in an application like this.)

Here is the current program code:

import datetime
from time import sleep

import cv2

goCapturedStream = None
# gcCameraLogin, gcCameraURL, & gcPhotoFolder are defined in the program, but omitted for simplicity / obfuscation.

def CaptureVideoStream():
    global goCapturedStream
    print(f"CaptureVideoStream({datetime.datetime.now()}):  Capturing video stream...")
    goCapturedStream = cv2.VideoCapture(f"rtsp://{gcCameraLogin}@{gcCameraURL}:554/stream0")
    if not goCapturedStream.isOpened():  print(f"Error:  Video Capture Stream was not opened.")
    return

def TakePhotoFromVideoStream(pcPhotoName):
    llResult = False ;  laFrame = None
    llResult, laFrame = goCapturedStream.read()
    print(f"TakePhotoFromVideoStream({datetime.datetime.now()}):  Result is {llResult},  Frame data type is {type(laFrame)}, Frame length is {len(laFrame)}")
    if not ".jpg" in pcPhotoName.lower():  pcPhotoName += ".jpg"
    lcFullPathName = f"{gcPhotoFolder}/{pcPhotoName}"
    cv2.imwrite(lcFullPathName, laFrame)

def ReleaseVideoStream():
    global goCapturedStream
    goCapturedStream.release()
    goCapturedStream = None

# Main Program:  Obtain sequence of JPG images from captured video stream
CaptureVideoStream()
for N in range(1,7):
    TakePhotoFromVideoStream(f"Test{N}.jpg")
    sleep(2)             # 2 seconds
ReleaseVideoStream()

Dan Masek's suggestions were very valuable.

The program (now enhanced significantly) saves up-to-date images correctly, when triggered by the camera's inbuilt motion detection (running in a separate thread and communicating through global variables).

The key tricks were:

  1. A much faster loop reading the frames (and discarding most of them). I reduced the sleep to 0.1 (and even further to 0.01), and saved relatively few frames to JPG files only when required
  2. Slowing down the frame rate on the camera (from 25 to 10 fps - even tried 5 at one point). This meant that the camera didn't get ahead of the software and send unpredictable frames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM