之前的文章介绍了如何调用摄像头间隔拍照并保存图片(文章链接:Python+OpenCV调用摄像头固定间隔时间拍照并保存到本地同时应用到YOLO中检测目标),这篇文章再介绍一下如何调用摄像头并保存视频。
capture = cv2.VideoCapture(0) ref, frame = capture.read()
前文介绍过,cv2.VideoCapture()获取摄像头,read()方法读入,返回的ref是是否读取成功的标志(True/False),frame是视频的每一帧
写入视频用VideoWriter()方法:参数如下:
cv2.VideoWriter(路径, 保存视频文件格式, 视频帧速率, 视频分辨率)
其中,第二个参数为视频编解码器,为四字符代码(Four-Character Codes)具体用法如下:
cv2.VideoWriter_fourcc(‘I’, ‘4’, ‘2’, ‘0’),该参数是YUV编码类型,文件名后缀为.avi
cv2.VideoWriter_fourcc(‘P’, ‘I’, ‘M’, ‘I’),该参数是MPEG-1编码类型,文件名后缀为.avi
cv2.VideoWriter_fourcc(‘X’, ‘V’, ‘I’, ‘D’),该参数是MPEG-4编码类型,文件名后缀为.avi
cv2.VideoWriter_fourcc(‘T’, ‘H’, ‘E’, ‘O’),该参数是Ogg Vorbis,文件名后缀为.ogv
cv2.VideoWriter_fourcc(‘F’, ‘L’, ‘V’, ‘1’),该参数是Flash视频,文件名后缀为.flv
或直接用cv2.VideoWriter_fourcc()来直接指定视频格式
cv2.VideoWriter_fourcc(*'XVID')
第四个参数为元组格式,如1920*1080的分辨率则传入(1920,1080)。需要注意的是:一定要设置成和你用的摄像头相同的分辨率,不然无法保存。笔者是win10,查看相机使用的分辨率如下图:
则设置成(640,480)
import cv2 capture = cv2.VideoCapture(0) fourcc = cv2.VideoWriter_fourcc(*'XVID') #目录,视频格式,帧速率,分辨率 out = cv2.VideoWriter('D:/video/a.avi', fourcc, 20.0, (640, 480)) #通过write方法读取到视频帧即可 out.write(frame) cv2.imshow("video",frame) k = cv2.waitKey(1) if k == ord('q'): break
完整代码如下:根据自己需要选择保存的视频位置
import time import cv2 import numpy as np from PIL import Image from yolo import YOLO yolo = YOLO() capture = cv2.VideoCapture(0) #保存摄像头视频 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('D:/video/a.avi', fourcc, 20.0, (640, 480)) if capture.isOpened(): ref, frame = capture.read() else: ref = False fps = 0.0 timeF = 10 c=1 while ref: t1 = time.time() # 读取某一帧 ref,frame=capture.read() # 此处显示保存的图片上无检测结果 #if (c % timeF == 0): # cv2.imwrite("D:/photo/" + str(c) + '.jpg', frame) #c += 1 #保存视频,此处无检测结果 #out.write(frame) # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) # 转变成Imageq frame = Image.fromarray(np.uint8(frame)) # 进行检测 frame = np.array(yolo.detect_image(frame)) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame,cv2.COLOR_RGB2BGR) #此处显示保存的图片上有检测结果 #if (c % timeF == 0): # cv2.imwrite("D:/photo/" + str(c) + '.jpg', frame) #c += 1 #保存的视频有检测结果 #out.write(frame) fps = ( fps + (1./(time.time()-t1)) ) / 2 print("fps= %.2f"%(fps)) frame = cv2.putText(frame, "fps= %.2f"%(fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) #此处保存的图片上有fps #if (c % timeF == 0): # cv2.imwrite("D:/photo/" + str(c) + '.jpg', frame) #c += 1 # 保存的视频有检测结果和fps out.write(frame) cv2.imshow("video",frame) k = cv2.waitKey(1) if k == ord('q'): break