读取摄像头视频内容
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
while(True):
ret,frame=cap.read()
if ret!=True:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
写入视频
import cv2
import numpy as np
def zh_ch(string):
return string.encode("gbk").decode(errors="ignore")
cap=cv2.VideoCapture('out.mp4')
fps = cap.get(5)
width = cap.get(3)
height = cap.get(4)
fourcc=cv2.VideoWriter_fourcc(*'MJPG')
out=cv2.VideoWriter('1.avi',fourcc,fps,(int(width),int(height)))
while (True):
ret,frame=cap.read()
src = cv2.resize(frame, (int(width // 2), int(height // 2)), interpolation=cv2.INTER_CUBIC)
if ret==True:
frame=cv2.flip(frame,1)
out.write(frame)
cv2.imshow(zh_ch('frame'),src)
else:
break
if cv2.waitKey(1)&0xff==ord('q'):
break
out.release()
cap.release()
cv2.destroyAllWindows()