Python opencv videocapture 获取最新帧 获取最后帧 获取实时帧
获取最新图片 关键代码
allcount = cap.get(cv2.CAP_PROP_FRAME_COUNT)
cap.set(cv2.CAP_PROP_POS_FRAMES,allcount)
完整代码
import cv2
import time
cap = cv2.VideoCapture('http://192.168.1.5/0.ts')
fps =cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
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()