Checking into Dev Branch.... accidently checked directly into Master previously!

This commit is contained in:
MikeTheWatchGuy 2019-08-13 16:54:17 -04:00
parent 7d541255e6
commit 4c155d79e7
3 changed files with 52 additions and 18 deletions

View File

@ -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.
"""

View File

@ -1,11 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys import PySimpleGUI as sg
if sys.version_info[0] >= 3: # import PySimpleGUIQt as sg
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
import cv2 import cv2
import numpy as np import numpy as np
import sys
from sys import exit as exit from sys import exit as exit
""" """
@ -13,26 +11,24 @@ Demo program that displays a webcam using OpenCV
""" """
def main(): def main():
sg.ChangeLookAndFeel('LightGreen') sg.ChangeLookAndFeel('Black')
# define the window layout # define the window layout
layout = [[sg.Text('OpenCV Demo', size=(40, 1), justification='center', font='Helvetica 20')], layout = [[sg.Text('OpenCV Demo', size=(40, 1), justification='center', font='Helvetica 20')],
[sg.Image(filename='', key='image')], [sg.Image(filename='', key='image')],
[sg.Button('Record', size=(10, 1), font='Helvetica 14'), [sg.Button('Record', size=(10, 1), font='Helvetica 14'),
sg.Button('Stop', size=(10, 1), font='Any 14'), sg.Button('Stop', size=(10, 1), font='Any 14'),
sg.Button('Exit', size=(10, 1), font='Helvetica 14'), sg.Button('Exit', size=(10, 1), font='Helvetica 14'),]]
sg.Button('About', size=(10,1), font='Any 14')]]
# create the window and show it without the plot # 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)) location=(800,400))
window.Layout(layout).Finalize()
# ---===--- Event LOOP Read and display frames, operate the GUI --- # # ---===--- Event LOOP Read and display frames, operate the GUI --- #
cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(0)
recording = False recording = False
while True: while True:
event, values = window.Read(timeout=0, timeout_key='timeout') event, values = window.Read(timeout=20)
if event == 'Exit' or event is None: if event == 'Exit' or event is None:
sys.exit(0) sys.exit(0)
elif event == 'Record': elif event == 'Record':
@ -42,13 +38,7 @@ def main():
img = np.full((480, 640),255) img = np.full((480, 640),255)
imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes
window.FindElement('image').Update(data=imgbytes) 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: if recording:
ret, frame = cap.read() ret, frame = cap.read()
imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto

View File

@ -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