Screen capture using OpenCV and Python-2.7

I am using Python 2.7 and OpenCV 2.4.9.

I need to capture the current frame that is displayed to the user and load it as a cv :: Mat object in Python.

Do you guys know a quick way to do this recursively?

I need something like what was done in the example below that recursively captures Mat frames from a webcam:

import cv2 cap = cv2.VideoCapture(0) while(cap.isOpened()): ret, frame = cap.read() cv2.imshow('WindowName', frame) if cv2.waitKey(25) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows() break 

In this example, he used the VideoCapture class to work with a captured image from a webcam.

With VideoCapture.read (), a new frame is always read and saved in the Mother object.

Can I load a β€œprint screen stream” into a VideoCapture object? Can I create a streaming display of my computer from OpenCV to Python without saving and deleting a lot of .bmp files per second?

I need these frames as Mat objects or NumPy arrays , so I can execute some Computer Vision routines with these frames in real time.

+7
source share
2 answers

This is the solution code I wrote using @Raoul's tips.

I used the PIL ImageGrab module to capture print screen frames.

 import numpy as np from PIL import ImageGrab import cv2 while(True): printscreen_pil = ImageGrab.grab() printscreen_numpy = np.array(printscreen_pil.getdata(),dtype='uint8')\ .reshape((printscreen_pil.size[1],printscreen_pil.size[0],3)) cv2.imshow('window',printscreen_numpy) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break 
+17
source

I had frame rate problems with other solutions, mss solve them.

 import numpy as np import cv2 from mss import mss from PIL import Image mon = {'top': 160, 'left': 160, 'width': 200, 'height': 200} sct = mss() while 1: sct.get_pixels(mon) img = Image.frombytes('RGB', (sct.width, sct.height), sct.image) cv2.imshow('test', np.array(img)) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break 
+16
source

Source: https://habr.com/ru/post/1241866/


All Articles