所以我有这个代码,它记录我的屏幕并将其保存为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()
答案 0 :(得分:0)
我强烈建议您使用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