视频文件的读写操作:
import cv2
videoCapture = cv2.VideoCapture('123'.avi)
fps = videoCapture.get(cv2.CAP_PROP_FPS)
size = int(int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('out.avi',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)
success,frame = videoCapture.read()
while success:
videoWriter.write(frame)
success,frame = videoCapture.read()
捕获摄像头的帧:捕获10s 电脑摄像头视频
import cv2
cameraCapture = cv2.VideoCapture(0)
fps =30
size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('out.avi',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)
success,frame = cameraCapture.read()
numFrameRemaining = 10 * fps -1
while success and numFrameRemaining > 0:
videoWriter.write(frame)
success,frame = cameraCapture.read()
numFrameRemaining -= 1
""""
一组摄像头或一个多头摄像头的使用方法
"""
# success0 = cameraCapture0.grab()
# success1 = cameraCapture1.grab()
# if success0 and success1:
# frame0 = cameraCapture0.retrieve()
# frame1 = cameraCapture1.retrieve()
cameraCapture.release()
图片显示:
import cv2
import numpy as np
img = cv2.imread('hcl.jpg')
cv2.imshow('hcl',img)
cv2.waitKey()
cv2.destroyAllWindows()
视频显示:
其实这里面代码是通过图片不断更新实现视频效果,可通过注释的sleep看到效果
import cv2
import numpy as np
import time
clicked = False
def onMouse(event,x,y,flags,param):
global clicked
if event == cv2.EVENT_LBUTTONUP:
clicked = True
cameraCapture = cv2.VideoCapture(0)
cv2.namedWindow('myWindow')
cv2.setMouseCallback('myWindow',onMouse)
print('showing camera feed.click window or press any key to stop')
success,frame = cameraCapture.read()
while success and cv2.waitKey(1) == -1 and not clicked:
cv2.imshow('myWindow',frame)
success,frame = cameraCapture.read()
#time.sleep(1)
cv2.destroyWindow('myWindow')
cameraCapture.release()