Python Computer Vision Tutorials — cv2 and mss basics / part 3

wsh
1 min readFeb 20, 2019

CONTENTS LIST

CONTENTS LIST

MSS Screen Capture

If you want to apply object detection on the screen, python-mss is the most recommended tool to capture the screen. As you know, there are a large number of screen capture API, but most of them are too slow to apply object detection in real-time speed.

Python-mss module is the fastest one at leas on my experience. So in this post, I’m going to show you how to use it .

How to use

We first import necessary modules:

from mss import mss
from PIL import Image
import cv2

Then specify where to capture within the screen, and enter a ‘while’ loop:

mon = {'top':300, 'left':500, 'width':600, 'height':600}
sct = mss()
While True:
sct.get_pixels(mon)
frame = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
frame = np.array(frame)
cv2.imshow('a crop of the screen', frame)
if cv2.waitKey(1) & 0xff == ord('q'):
cv2.destroyAllWindows()

This is quite simple, but runs unbelievably fast. On my poor computer with core i5, it runs at over 140 frames per second!

--

--

No responses yet