opencv读取视频并显示,cv2.VideoCapture(video_path),cv2.VideoWriter()

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()
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值