ScreenGear API

With ScreenGear, we can easily define an area on the computer screen or an open window to record the live screen frames in real-time at the expense of insignificant latency. To achieve this, ScreenGear provides a high-level multi-threaded wrapper around mss python library API and also supports the flexible direct parameter manipulation. Furthermore, ScreenGear relies on Threaded Queue mode for ultra-fast live frame handling and which is enabled by default.


Important: :warning:

  • Python-mss dropped support for legacy Python 2.7 in the latest release (Related PR). Therefore latest Python-mss/ScreenGear may not work with python 2.7 builds, but you can surely try older versions of mss instead at your own risk!

 

Importing:

You can easily import ScreenGear as follows:

from vidgear.gears import ScreenGear

 

Usage: :hammer:

1. Basic Usage:

Below is a snapshot of a ScreenGear API in action:

ScreenGear in action!

Code to generate the above result:

# import libraries
from vidgear.gears import ScreenGear
import cv2

stream = ScreenGear().start()

# infinite loop
while True:
  
  frame = stream.read()
  # read frames

  # check if frame is None
  if frame is None:
    #if True break the infinite loop
    break
  
  cv2.imshow("Output Frame", frame)
  # Show output window

  key = cv2.waitKey(1) & 0xFF
  # check for 'q' key-press
  if key == ord("q"):
    #if 'q' key-pressed break out
    break

cv2.destroyAllWindows()
# close output window

stream.stop()
# safely close video stream.

 

2. ScreenGear with flexible parameter control:

ScreenGear contains certain parameters which allow us to directly control/set the input dimensions of screen and monitor to grab screen from. Here's an example:

# import libraries
from vidgear.gears import ScreenGear
import cv2


options = {'top': 40, 'left': 0, 'width': 100, 'height': 100} # define dimensions of screen w.r.t to given monitor to be captured
stream = ScreenGear(monitor=1, logging=True, **options).start() #Open Live Screencast on current monitor 

# infinite loop
while True:
	
	frame = stream.read()
	# read frames

	# check if frame is None
	if frame is None:
		#if True break the infinite loop
		break
	

	# {do something with frame here}
       
        # Show output window
	cv2.imshow("Output Frame", frame)

	key = cv2.waitKey(1) & 0xFF
	# check for 'q' key-press
	if key == ord("q"):
		#if 'q' key-pressed break out
		break

cv2.destroyAllWindows()
# close output window

stream.stop()
# safely close video stream

 

3. ScreenGear with WriteGear API:

ScreenGear can be used with WriteGear API directly without any compatibility issues as follows:

# import libraries
from vidgear.gears import ScreenGear
from vidgear.gears import WriteGear
import cv2


options = {'top': 40, 'left': 0, 'width': 100, 'height': 100} # define dimensions of screen w.r.t to given monitor to be captured

output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast"} #define (Codec,CRF,preset) FFmpeg tweak parameters for writer


stream = ScreenGear(monitor=1, logging=True, **options).start() #Open Live Screencast on current monitor 

writer = WriteGear(output_filename = 'Output.mp4', compression_mode = True, logging = True, **output_params) #Define writer with output filename 'Output.mp4' 

# infinite loop
while True:
	
	frame = stream.read()
	# read frames

	# check if frame is None
	if frame is None:
		#if True break the infinite loop
		break
	

	# {do something with frame here}
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

	# write a modified frame to writer
        writer.write(gray) 
       
        # Show output window
	cv2.imshow("Output Frame", frame)

	key = cv2.waitKey(1) & 0xFF
	# check for 'q' key-press
	if key == ord("q"):
		#if 'q' key-pressed break out
		break

cv2.destroyAllWindows()
# close output window

stream.stop()
# safely close video stream
writer.close()
# safely close writer

 

Parameters and Attributes: :wrench:

  1. monitor(int): sets the index of the monitor where to grab a frame from. More information can be found here. Its default value is 1 (means the current monitor will be used).

    :warning: monitor value cannot be negative, Otherwise, it will throw ValueError

  2. colorspace(string): set the colorspace of the video stream. Its default value is None. Check out its detailed Usage here

  3. **options(dict): can be used to pass tweaked parameters to ScreenGear API. This dictionary parameter provides us the flexibility to directly set the dimensions of the given monitor's region which are going to be cast (More information can be found here) and also supports various special parameter to tweak internal properties of ScreenGear. All these parameters can be passed using **option attribute as follows*:

    options = {'top': 40, 'left': 0, 'width': 100, 'height': 100} # define dimensions of screen w.r.t to given monitor to be captured
    • Currently supported valid parameters for a given monitor(default is 1) are as follows:
      • left: the x-coordinate of the upper-left corner of the region
      • top: the y-coordinate of the upper-left corner of the region
      • width: the width of the region
      • height: the height of the region
  4. logging(boolean): set this flag to enable/disable error logging essential for debugging. Its default value is False.

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::