Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take an input stream from the desktop in OpenCV

I am using Python 3.5.1 and OpenCV 3.0.0.

I am working on a python program that can play games, so it needs to 'see' what is going on, on the screen. How can this be achieved?

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
    #work with frames here

Is there a int 'a' such that cv2.VideoCapture(a) will take the desktop screen as video input? I tried making it and I followed a rather clumsy approach, I captured screen repeatedly using:

import os
os.system('screencapture test.jpg')

Then opening test.jpg using cv2.imread. This was a very slow approach, on searching online I found this question Screen Capture with OpenCV and Python-2.7 which does the same thing, but more efficiently. But the fact still remains that it is capturing individual screenshots and processing them one by one and not a true video stream. I also found this How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)? which I think is close to what I am trying but is in C++, if someone can help me convert this to Python, I will highly appreciate it.

The main thing is that the program will be doing something like MarI/O, so speed is a concern, any help is appreciated, go easy on me, I am (relatively) new to OpenCV.

Thanks.

like image 697
Divyanshu Kalra Avatar asked May 08 '16 23:05

Divyanshu Kalra


People also ask

How do I capture a specific window in Python?

We use the activate() method to show and focus the window, so if it's minimized, it will be shown on the screen immediately after calling this method. This time, we specify the size of the screen as the window size by passing tuple(w. size) . We also specify the region keyword argument in the pyautogui.

Can OpenCV capture video?

OpenCV provides a very simple interface to do this. Let's capture a video from the camera (I am using the built-in webcam on my laptop), convert it into grayscale video and display it. Just a simple task to get started. To capture a video, you need to create a VideoCapture object.

How do I record multiple camera streams with OpenCV?

To capture multiple streams with OpenCV, I recommend using threading which can improve performance by alleviating the heavy I/O operations to a separate thread. Since accessing the webcam/IP/RTSP stream using cv2. VideoCapture().


1 Answers

Just an update on this question in case anyone wants a solution.

Taking screenshot can be achieved by using module pyautogui

import pyautogui
import matplotlib.pyplot as plt

image = pyautogui.screenshot()
plt.imshow(image)

If you want to read it as a stream,

while(True):
    image = pyautogui.screenshot()
    #further processing
    if finished:
        break

According to the documentation,

On a 1920 x 1080 screen, the screenshot() function takes roughly 100 milliseconds

So this solution can be used if your application does not demand high fps rate.

like image 183
danyfang Avatar answered Oct 13 '22 06:10

danyfang