我目前正在使用 OpenCV 4.5.1 和 python 3.8.5。我想将实时视频(使用笔记本电脑网络摄像头)中每一帧的像素值绘制到值时间图上。但我不知道如何正确获取时间戳。我已经尝试使用 CAP_PROP_POS_MSEC,但它永远不会从 0 毫秒开始。从一开始,它就向我展示了一些大数字,例如 8368000.67 、 8368036.64 、 8368068.64 (其余如下所示)。以毫秒为间隔评估图表只会让我感到困惑。我知道我可以用 8368000.67 减去所有时间戳以获得从零开始的时间戳,但这只会给我的机器增加额外的工作。其他解决方案,例如使用帧数(CAP_PROP_POS_FRAMES 或 CAP_PROP_FRAME_COUNT)只是出于某种原因返回我 -1。有没有想过解决这个问题?感谢您提供任何帮助。
这是我的源代码
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()
结果显示了这一点
- 每秒帧数:30.00
- 毫秒: : 8368000.67
- posFrames:-1.00
- 帧数:-1.00
- 每秒帧数:30.00
- 毫秒: : 8368036.64
- posFrames:-1.00
- 帧数:-1.00
- 每秒帧数:30.00
- 毫秒: : 8368068.64
- posFrames:-1.00
- 帧数:-1.00
- 每秒帧数:30.00
- 毫秒::8368100.60
- posFrames:-1.00
- 帧数:-1.00
- 每秒帧数:30.00
- 毫秒: : 8368136.62
- posFrames:-1.00
- 帧数:-1.00
- 每秒帧数:30.00
- 毫秒: : 8368168.62
- posFrames:-1.00
- 帧数:-1.00