forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfps.py
More file actions
63 lines (45 loc) · 1.31 KB
/
fps.py
File metadata and controls
63 lines (45 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
This is part of the MSS Python's module.
Source: https://github.com/BoboTiG/python-mss
Simple naive benchmark to compare with:
https://pythonprogramming.net/game-frames-open-cv-python-plays-gta-v/
"""
import time
import cv2
import mss
import numpy
def screen_record():
try:
from PIL import ImageGrab
except ImportError:
return 0
# 800x600 windowed mode
mon = (0, 40, 800, 640)
title = "[PIL.ImageGrab] FPS benchmark"
fps = 0
last_time = time.time()
while time.time() - last_time < 1:
img = numpy.asarray(ImageGrab.grab(bbox=mon))
fps += 1
cv2.imshow(title, cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
return fps
def screen_record_efficient():
# 800x600 windowed mode
mon = {"top": 40, "left": 0, "width": 800, "height": 640}
title = "[MSS] FPS benchmark"
fps = 0
sct = mss.mss()
last_time = time.time()
while time.time() - last_time < 1:
img = numpy.asarray(sct.grab(mon))
fps += 1
cv2.imshow(title, img)
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
return fps
print("PIL:", screen_record())
print("MSS:", screen_record_efficient())