FPS with cv2.VideoCapture

# -*- coding: utf-8 -*-
"""
Created on Thu Nov 15 22:37:02 2018
#QQ群:476842922(欢迎加群讨论学习)
@author: Administrator
"""
# import the necessary packages
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import cv2
 
# construct the argument parse and parse the arguments解析参数
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required=True,
	help="path to input video file")
args = vars(ap.parse_args())
 
# open a pointer to the video stream and start the FPS timer视频流帧
stream = cv2.VideoCapture(args["video"])
fps = FPS().start()

# loop over frames from the video file stream
while True:
	# grab the frame from the threaded video file stream
	(grabbed, frame) = stream.read()
 
	# if the frame was not grabbed, then we have reached the end
	# of the stream
	if not grabbed:
		break
 
	# resize the frame and convert it to grayscale (while still
	# retaining 3 channels)
	frame = imutils.resize(frame, width=450)
	frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	frame = np.dstack([frame, frame, frame])
 
	# display a piece of text to the frame (so we can benchmark
	# fairly against the fast method)
	cv2.putText(frame, "Slow Method", (10, 30),
		cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)	
 
	# show the frame and update the FPS counter
	cv2.imshow("Frame", frame)
	cv2.waitKey(1)
	fps.update()

# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
stream.release()
cv2.destroyAllWindows()

(base) C:\Users\Administrator>python Videos.py --video aa.mp4
[INFO] elasped time: 72.97
[INFO] approx. FPS: 67.82

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 `opencv` 和 `imageio` 两个库来录制 `cv.show()` 内容并制作为 `gif` 文件。下面是代码示例: ```python import cv2 import imageio # 初始化一个VideoCapture对象 cap = cv2.VideoCapture(0) # 创建一个空列表,用于存储图像帧 frames = [] # 循环录制图像帧 while True: ret, frame = cap.read() if not ret: break cv2.imshow("frame", frame) frames.append(frame) if cv2.waitKey(1) & 0xFF == ord("q"): break # 释放VideoCapture对象 cap.release() cv2.destroyAllWindows() # 将图像帧列表保存为gif文件 imageio.mimsave("output.gif", frames, "GIF", fps=30) ``` 以上代码会连续录制图像帧,直到按下 `q` 键为止。录制的图像帧会存储在 `frames` 列表中,最后使用 `imageio.mimsave` 函数将其保存为 `gif` 文件。 ### 回答2: 要实现将cv2.show()的内容录制为GIF文件,首先需要安装imageio和opencv-python库。 下面是一个示例代码: ```python import cv2 import imageio # 创建一个视频捕获对象 cap = cv2.VideoCapture(0) # 创建一个GIF编写器 output_gif = imageio.get_writer('output.gif', mode='I', duration=0.1) while True: # 读取每一帧 ret, frame = cap.read() if not ret: break # 显示每一帧 cv2.imshow('frame', frame) # 将每一帧添加到GIF中 output_gif.append_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # 按下 'q' 键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放视频捕获对象和关闭窗口 cap.release() cv2.destroyAllWindows() # 完成GIF的写入 output_gif.close() ``` 上述代码首先导入了必要的库,然后创建了一个视频捕获对象`cap`,并创建了一个GIF编写器`output_gif`。在一个无限循环中,代码读取每一帧数据并显示在窗口中,并将每一帧添加到GIF中。当按下'q'键时,循环中断。在代码的最后,释放了捕获对象和关闭了窗口,并完成了GIF的写入。 可以根据需要对代码进行修改和优化,例如可以设置录制时间、调整帧率等。 ### 回答3: 要实现将cv.show的内容录制为gif文件,首先需要安装相应的库: ```python pip install opencv-python pip install imageio pip install imageio-ffmpeg ``` 接下来,可以按照以下方式编写Python代码: ```python import cv2 import imageio import numpy as np # 设置录制参数 output_file = 'output.gif' # 输出gif文件名 duration = 0.1 # GIF每帧间隔时间(秒) # 创建视频录制对象 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) # 开启摄像头录制 cap = cv2.VideoCapture(0) # 读取帧并录制 while True: ret, frame = cap.read() if ret: out.write(frame) # 录制帧 cv2.imshow('frame', frame) # 显示帧 if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # 释放资源 cap.release() out.release() cv2.destroyAllWindows() # 读取录制的视频文件 video = imageio.get_reader('output.avi') # 写入GIF文件 with imageio.get_writer(output_file, mode='I', duration=duration) as writer: for i, frame in enumerate(video): writer.append_data(frame) print('GIF文件已生成:' + output_file) ``` 该代码首先使用OpenCV库开启摄像头录制,将录制到的帧逐帧保存在一个视频文件(output.avi)中。然后使用imageio库从视频文件中逐帧读取,并将每一帧写入GIF文件中。最后,输出生成的GIF文件的文件名。 请注意,录制内容为摄像头的实时画面,您可以根据需要调整代码的输入源,例如录制屏幕的内容或指定视频文件的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值