diff --git a/DemoPrograms/Demo_OpenCV_7_Line_Program.py b/DemoPrograms/Demo_OpenCV_7_Line_Program.py new file mode 100644 index 00000000..b1cc0ff7 --- /dev/null +++ b/DemoPrograms/Demo_OpenCV_7_Line_Program.py @@ -0,0 +1,13 @@ +import cv2, PySimpleGUI as sg +window = sg.Window('Demo Application - OpenCV Integration', [[sg.Image(filename='', key='image')],], location=(800,400)) +cap = cv2.VideoCapture(0) # Setup the camera as a capture device +while True: # The PSG "Event Loop" + event, values = window.Read(timeout=20, timeout_key='timeout') # get events for the window with 20ms max wait + if event is None: break # if user closed window, quit + window.FindElement('image').Update(data=cv2.imencode('.png', cap.read()[1])[1].tobytes()) # Update image in window + +""" +Putting the comment at the bottom so that you can see that the code is indeed 7 lines long. And, there is nothing +done out of the ordinary to make it 7 lines long. There are no ; for example. OK, so the if statement is on one line +but that's the only place that you would traditionally see one more line. So, call it 8 if you want. +""" \ No newline at end of file diff --git a/DemoPrograms/Demo_OpenCV_Webcam.py b/DemoPrograms/Demo_OpenCV_Webcam.py index 6efb97b2..287f485a 100644 --- a/DemoPrograms/Demo_OpenCV_Webcam.py +++ b/DemoPrograms/Demo_OpenCV_Webcam.py @@ -1,11 +1,9 @@ #!/usr/bin/env python -import sys -if sys.version_info[0] >= 3: - import PySimpleGUI as sg -else: - import PySimpleGUI27 as sg +import PySimpleGUI as sg +# import PySimpleGUIQt as sg import cv2 import numpy as np +import sys from sys import exit as exit """ @@ -13,26 +11,24 @@ Demo program that displays a webcam using OpenCV """ def main(): - sg.ChangeLookAndFeel('LightGreen') + sg.ChangeLookAndFeel('Black') # define the window layout layout = [[sg.Text('OpenCV Demo', size=(40, 1), justification='center', font='Helvetica 20')], [sg.Image(filename='', key='image')], [sg.Button('Record', size=(10, 1), font='Helvetica 14'), sg.Button('Stop', size=(10, 1), font='Any 14'), - sg.Button('Exit', size=(10, 1), font='Helvetica 14'), - sg.Button('About', size=(10,1), font='Any 14')]] + sg.Button('Exit', size=(10, 1), font='Helvetica 14'),]] # create the window and show it without the plot - window = sg.Window('Demo Application - OpenCV Integration', + window = sg.Window('Demo Application - OpenCV Integration', layout, location=(800,400)) - window.Layout(layout).Finalize() # ---===--- Event LOOP Read and display frames, operate the GUI --- # cap = cv2.VideoCapture(0) recording = False while True: - event, values = window.Read(timeout=0, timeout_key='timeout') + event, values = window.Read(timeout=20) if event == 'Exit' or event is None: sys.exit(0) elif event == 'Record': @@ -42,13 +38,7 @@ def main(): img = np.full((480, 640),255) imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes window.FindElement('image').Update(data=imgbytes) - elif event == 'About': - sg.PopupNoWait('Made with PySimpleGUI', - 'www.PySimpleGUI.org', - 'Check out how the video keeps playing behind this window.', - 'I finally figured out how to display frames from a webcam.', - 'ENJOY! Go make something really cool with this... please!', - keep_on_top=True) + if recording: ret, frame = cap.read() imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto diff --git a/DemoPrograms/Demo_OpenCV_Webcam_Minimal.py b/DemoPrograms/Demo_OpenCV_Webcam_Minimal.py new file mode 100644 index 00000000..3d08fa03 --- /dev/null +++ b/DemoPrograms/Demo_OpenCV_Webcam_Minimal.py @@ -0,0 +1,31 @@ +import PySimpleGUI as sg +# import PySimpleGUIQt as sg +# import PySimpleGUIWeb as sg # has a known flicker problem that's being worked +import cv2 + +""" + Demo of using OpenCV to show your webcam in a GUI window. + This demo will run on tkinter, Qt, and Web(Remi). The web version flickers at the moment though + To exit, right click and choose exit. If on Qt, you'll have to kill the program as there are no right click menus + in PySimpleGUIQt (yet). +""" + +sg.ChangeLookAndFeel('Black') + +# define the window layout +layout = [[sg.Image(filename='', key='_IMAGE_', tooltip='Right click for exit menu')],] + +# create the window and show it without the plot +window = sg.Window('Demo Application - OpenCV Integration', layout, location=(800,400), + no_titlebar=True, grab_anywhere=True, + right_click_menu=['&Right', ['E&xit']], ) # if trying Qt, you will need to remove this right click menu + +# ---===--- Event LOOP Read and display frames, operate the GUI --- # +cap = cv2.VideoCapture(0) # Setup the OpenCV capture device (webcam) +while True: + event, values = window.Read(timeout=20, timeout_key='timeout') + if event in ('Exit', None): + break + ret, frame = cap.read() # Read image from capture device (camera) + imgbytes=cv2.imencode('.png', frame)[1].tobytes() # Convert the image to PNG Bytes + window.FindElement('_IMAGE_').Update(data=imgbytes) # Change the Image Element to show the new image