首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python中从屏幕捕获视频数据

在Python中从屏幕捕获视频数据
EN

Stack Overflow用户
提问于 2016-01-30 12:03:56
回答 7查看 113.9K关注 0票数 36

有没有办法用Python (可能是OpenCV或PIL)连续抓取全部或部分屏幕的帧,至少每秒15fps或更多?我已经在其他语言中看到过,所以理论上它应该是可能的。

我不需要将图像数据保存到文件中。实际上,我只想让它输出一个包含原始RGB数据的数组(比如在numpy数组中),因为我将把它发送到一个大的LED显示器上(可能是在调整大小之后)。

EN

回答 7

Stack Overflow用户

发布于 2017-04-22 22:02:33

还有另一种使用mss的解决方案,它提供了更好的帧速率。(在装有MacOS Sierra的Macbook Pro上进行了测试)

import numpy as np
import cv2
from mss import mss
from PIL import Image

mon = {'left': 160, 'top': 160, 'width': 200, 'height': 200}

with mss() as sct:
    while True:
        screenShot = sct.grab(mon)
        img = Image.frombytes(
            'RGB', 
            (screenShot.width, screenShot.height), 
            screenShot.rgb, 
        )
        cv2.imshow('test', np.array(img))
        if cv2.waitKey(33) & 0xFF in (
            ord('q'), 
            27, 
        ):
            break
票数 26
EN

Stack Overflow用户

发布于 2019-01-18 08:40:16

使用上面的所有解决方案,我无法获得可用的帧率,直到我以以下方式修改代码:

import numpy as np
import cv2
from mss import mss
from PIL import Image

bounding_box = {'top': 100, 'left': 0, 'width': 400, 'height': 300}

sct = mss()

while True:
    sct_img = sct.grab(bounding_box)
    cv2.imshow('screen', np.array(sct_img))

    if (cv2.waitKey(1) & 0xFF) == ord('q'):
        cv2.destroyAllWindows()
        break

使用此解决方案,我可以轻松获得每秒20+帧。

有关参考信息,请查看此链接:OpenCV/Numpy example with mss

票数 19
EN

Stack Overflow用户

发布于 2018-12-17 02:52:49

基于这篇文章和其他文章,我做了这样的事情。这是一个屏幕截图和写入到一个视频文件,而不保存的img。

import cv2
import numpy as np
import os
import pyautogui

output = "video.avi"
img = pyautogui.screenshot()
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
#get info from img
height, width, channels = img.shape
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output, fourcc, 20.0, (width, height))

while(True):
 try:
  img = pyautogui.screenshot()
  image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
  out.write(image)
  StopIteration(0.5)
 except KeyboardInterrupt:
  break

out.release()
cv2.destroyAllWindows()
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35097837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档