1+ import pyautogui
2+ import PySimpleGUI as sg
3+ import cv2
4+ import numpy as np
5+ 6+ """
7+ Demo program that displays a webcam using OpenCV
8+ """
9+ 10+ 11+ def main ():
12+ 13+ sg .theme ('Black' )
14+ 15+ # define the window layout
16+ layout = [[sg .Text ('OpenCV Demo' , size = (40 , 1 ), justification = 'center' , font = 'Helvetica 20' )],
17+ [sg .Image (filename = '' , key = 'image' )],
18+ [sg .Button ('Record' , size = (10 , 1 ), font = 'Arial 14' ),
19+ sg .Button ('Stop' , size = (10 , 1 ), font = 'Arial 14' ),
20+ sg .Button ('Exit' , size = (10 , 1 ), font = 'Arial 14' ),
21+ sg .Button ('Screenshot' ,size = (10 ,1 ),font = 'Arial 14' ) ]]
22+ 23+ # create the window and show it without the plot
24+ window = sg .Window ('Demo Application - OpenCV Integration' ,
25+ layout , location = (800 , 400 ))
26+ 27+ # ---===--- Event LOOP Read and display frames, operate the GUI --- #
28+ cap = cv2 .VideoCapture (0 )
29+ recording = False
30+ 31+ while True :
32+ event , values = window .read (timeout = 20 )
33+ if event == 'Exit' or event == sg .WIN_CLOSED :
34+ return
35+ 36+ elif event == 'Record' :
37+ recording = True
38+ elif event == 'Screenshot' :
39+ myScreenshot = pyautogui .screenshot ()
40+ myScreenshot .save (r'shot.png' )
41+ 42+ elif event == 'Stop' :
43+ recording = False
44+ img = np .full ((480 , 640 ), 255 )
45+ # this is faster, shorter and needs less includes
46+ imgbytes = cv2 .imencode ('.png' , img )[1 ].tobytes ()
47+ window ['image' ].update (data = imgbytes )
48+ 49+ if recording :
50+ ret , frame = cap .read ()
51+ imgbytes = cv2 .imencode ('.png' , frame )[1 ].tobytes () # ditto
52+ window ['image' ].update (data = imgbytes )
53+ 54+ 55+ 56+ main ()
0 commit comments