Python opencv videocapture 网络推流/本地视频播放 获取最新帧 获取最后帧 获取实时帧

Python opencv videocapture 获取最新帧 获取最后帧 获取实时帧

获取最新图片 关键代码

allcount = cap.get(cv2.CAP_PROP_FRAME_COUNT)
cap.set(cv2.CAP_PROP_POS_FRAMES,allcount)

完整代码

# -*- coding: utf-8 -*-
import cv2
import time

# 可以是网络推流地址 也可以是本地视频地址
cap = cv2.VideoCapture('http://192.168.1.5/0.ts')
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps =cap.get(cv2.CAP_PROP_FPS)
#fps=30
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#size=(960,544)
i=0
while(cap.isOpened()):
    i=i+1
    ret, frame = cap.read()
    
    # 获取最新图片
    allcount = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    cap.set(cv2.CAP_PROP_POS_FRAMES,allcount)

    time.sleep(2)
    ss = cap.get(2)
    print(ss)
    if ret==True:
        cv2.imwrite('./image/'+str(i)+'.jpg',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()

cv2.destroyAllWindows()

你可能感兴趣的:(OpenCV,python,opencv)