How to get the timestamp to start from 0ms when using live video on openCV

429 views Asked by At

I'm currently using OpenCV 4.5.1 and python 3.8.5. I want to plot the pixel value from each frame in my live video (using laptop webcam) to the value-time graph. But I don't know how to get the timestamp properly. I already try to use CAP_PROP_POS_MSEC, but it never starts from 0 ms. Right from the start, it showing me some big numbers like 8368000.67 , 8368036.64 , 8368068.64 (the rest is shown below). It just confuses me to evaluate the graph in millisecond intervals. I know that I can subtract all timestamps with 8368000.67 to get zero-based timestamp, but it just adds extra work to my machine. Other solutions like using the frame count (CAP_PROP_POS_FRAMES or CAP_PROP_FRAME_COUNT) just returning me -1 for some reason. Any thought to address this problem? Any bits of help are appreciated.

here's my source code

import cv2 as cv

# Open Camera and start streaming
cap = cv.VideoCapture(0)

if not cap.isOpened():
    print("Cannot open camera")
    exit()
    
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    
    fps          = cap.get(cv.CAP_PROP_FPS)
    milliseconds = cap.get(cv.CAP_PROP_POS_MSEC)
    posFrames    = cap.get(cv.CAP_PROP_POS_FRAMES) 
    frameCount   = cap.get(cv.CAP_PROP_FRAME_COUNT)
    
    print("fps: ",end="")
    print ('%.2f'%fps)
    print("milliseconds: : ",end="")
    print ('%.2f'%milliseconds)
    print("posFrames: ",end="")
    print ('%.2f'%posFrames)
    print("frameCount: ",end="")
    print ('%.2f'%frameCount)
    
    # Display the resulting frame
    cv.imshow("frame", frame)
        
    if cv.waitKey(1) == ord('q'):
        break
        
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

And the result showing this

  • fps: 30.00
  • milliseconds: : 8368000.67
  • posFrames: -1.00
  • frameCount: -1.00
  • fps: 30.00
  • milliseconds: : 8368036.64
  • posFrames: -1.00
  • frameCount: -1.00
  • fps: 30.00
  • milliseconds: : 8368068.64
  • posFrames: -1.00
  • frameCount: -1.00
  • fps: 30.00
  • milliseconds: : 8368100.60
  • posFrames: -1.00
  • frameCount: -1.00
  • fps: 30.00
  • milliseconds: : 8368136.62
  • posFrames: -1.00
  • frameCount: -1.00
  • fps: 30.00
  • milliseconds: : 8368168.62
  • posFrames: -1.00
  • frameCount: -1.00
0

There are 0 answers