所以我有这个代码,它记录我的屏幕并将其保存为 output.avi 但它每秒只能捕获 10-15 帧。我如何让它至少捕获大约 50-60 帧。如果我没有错的话,cv2 是基于 CPU 的。我如何使用 GPU 来完成这项任务?
import cv2
from PIL import ImageGrab
import numpy as np
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
video = cv2.VideoWriter("output.avi",fourcc,8,(1920,1080))
while(True):
image = ImageGrab.grab()
image = np.array(image)
frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
video.write(frame)
key = cv2.waitKey(1)
cv2.imshow("Hello",frame)
if(key==27):
break
video.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
我强烈建议您使用MSS而不是 cv2 来捕获屏幕。cv2 对处理图像数据很有用,但不擅长捕捉。另一方面,mss 的运行速度比任何其他屏幕捕获 API 都要快得多。我使用 mss 来应用对象检测(YOLOv2、darkflow),它以每秒 40 多帧的速度运行。如果在没有任何对象检测的情况下使用它,它应该以更高的 fps 运行。这是脚本:
import numpy as np
import cv2
import glob
from moviepy.editor import VideoFileClip
from mss import mss
from PIL import Image
import time
color = (0, 255, 0) # bounding box color.
# This defines the area on the screen.
mon = {'top' : 10, 'left' : 10, 'width' : 1000, 'height' : 800}
sct = mss()
previous_time = 0
while True :
sct.get_pixels(mon)
frame = Image.frombytes( 'RGB', (sct.width, sct.height), sct.image )
frame = np.array(frame)
# image = image[ ::2, ::2, : ] # can be used to downgrade the input
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imshow ('frame', frame)
if cv2.waitKey ( 1 ) & 0xff == ord( 'q' ) :
cv2.destroyAllWindows()
txt1 = 'fps: %.1f' % ( 1./( time.time() - previous_time ))
previous_time = time.time()
print txt1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6523 次 |
| 最近记录: |