1、cv2.VideoCapture(video_path)
#video_path用于指定视频的路径,当video_path=0时表示检测本地摄像头,video_path=1,表示检测外接的摄像头
2、cv2.VideoWriter(filename, fourcc, fps, frameSize[, isColor])
#写入处理后的视频
#第一个参数是要保存的文件的路径
#fourcc 指定编码器,fourcc 本身是一个 32 位的无符号数值,用 4 个字母表示采用的编码器。 常用的有 “DIVX"、”MJPG"、“XVID”、“X264"
#fps 要保存的视频的帧率
#frameSize 要保存的文件的画面尺寸
#isColor 指示是黑白画面还是彩色的画面
示例:
fourcc = cv2.VideoWriter_fourcc(*‘XVID’)
out = cv2.VideoWriter(‘testwrite.avi’,fourcc, 20.0, (1920,1080),True)
3、计算网络处理视频帧数公式
fps = ( fps + (1./(time.time()-t1)) ) / 2
读取显示代码:
import time
import cv2
import numpy as np
from PIL import Image
video_path = 0 # 调用本地摄像头
video_save_path = "D:\video\11.mp4"
video_fps = 25.0 # 设置保存视频的fps
capture=cv2.VideoCapture(video_path)
if video_save_path!="":
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
out = cv2.VideoWriter(video_save_path, fourcc, video_fps, size)
# out 保存视频--路径,格式,帧数,大小(高和宽)
fps = 0.0
while(True):
t1 = time.time()
# 读取某一帧
ref,frame=capture.read()
if ref == False:
break
# 格式转变,BGRtoRGB
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
# 转变成Image
frame = Image.fromarray(np.uint8(frame))
# 进行检测
frame = np.array(yolo.detect_image(frame))
# RGBtoBGR满足opencv显示格式
frame = cv2.cvtColor(frame,cv2.COLOR_RGB2BGR)
# 计算当前网络fps
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)
cv2.imshow("video",frame)
c= cv2.waitKey(1) & 0xff
if video_save_path!="":
out.write(frame)
if c==27:
capture.release()
break
capture.release()
out.release()
cv2.destroyAllWindows()