From 2b1721fcb1142437df54b2891ec05af1ec0c4fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Sat, 15 Dec 2018 18:04:02 +0100 Subject: [PATCH] Update 09_grab_screen_multiprocessing.py Do you see better results with those changes? Also, keep in mind to move the OpenCV preview window outside the recorded area. And the smaller the captured aea, the faster. --- .../09_grab_screen_multiprocessing.py | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/9_part grab screen multiprocessing/09_grab_screen_multiprocessing.py b/9_part grab screen multiprocessing/09_grab_screen_multiprocessing.py index e5f8be6..6c97fc7 100644 --- a/9_part grab screen multiprocessing/09_grab_screen_multiprocessing.py +++ b/9_part grab screen multiprocessing/09_grab_screen_multiprocessing.py @@ -1,49 +1,51 @@ import multiprocessing +from queue import Empty import time import cv2 import mss import numpy - -title = "FPS benchmark" -start_time = time.time() -display_time = 2 # displays the frame rate every 2 second -fps = 0 -sct = mss.mss() -# Set monitor size to capture -monitor = {"top": 40, "left": 0, "width": 800, "height": 640} +import sys def GRABMSS_screen(q): - while True: - # Get raw pixels from the screen, save it to a Numpy array - img = numpy.array(sct.grab(monitor)) - # To get real color we do this: - #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - q.put_nowait(img) - q.join() + # Set monitor size to capture + monitor = {"top": 40, "left": 0, "width": 800, "height": 640} + + with mss.mss() as sct: + while True: + # Get raw pixels from the screen, save it to a Numpy array + img = numpy.array(sct.grab(monitor)) + q.put(img) def SHOWMSS_screen(q): - global fps, start_time + title = "FPS benchmark" + start_time = time.time() + display_time = 2 # displays the frame rate every 2 second + fps = 0 + while True: - if not q.empty(): + try: img = q.get_nowait() - q.task_done() - # To get real color we do this: - img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - # Display the picture - cv2.imshow(title, cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) - # Display the picture in grayscale - fps+=1 - TIME = time.time() - start_time - if (TIME) >= display_time : - print("FPS: ", fps / (TIME)) - fps = 0 - start_time = time.time() - # Press "q" to quit - if cv2.waitKey(25) & 0xFF == ord("q"): - cv2.destroyAllWindows() - break - + except Empty: + continue + + # Display the picture + cv2.imshow(title, img) + + # Display the picture + fps += 1 + TIME = time.time() - start_time + if TIME >= display_time : + print("FPS:", fps / TIME) + fps = 0 + start_time = time.time() + + # Press "q" to quit + if cv2.waitKey(25) & 0xFF == ord("q"): + cv2.destroyAllWindows() + break + + if __name__=="__main__": # Queue q = multiprocessing.JoinableQueue()