2018-10-09 01:02:43 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
import PySimpleGUI as sg
|
|
|
|
else:
|
|
|
|
import PySimpleGUI27 as sg
|
|
|
|
import cv2 as cv
|
|
|
|
from PIL import Image
|
2018-10-15 20:07:23 +00:00
|
|
|
import numpy as np
|
2018-10-09 01:02:43 +00:00
|
|
|
import io
|
|
|
|
from sys import exit as exit
|
|
|
|
|
|
|
|
"""
|
|
|
|
Demo program that displays a webcam using OpenCV
|
|
|
|
"""
|
|
|
|
def main():
|
|
|
|
|
|
|
|
sg.ChangeLookAndFeel('LightGreen')
|
|
|
|
|
|
|
|
# define the window layout
|
|
|
|
layout = [[sg.Text('OpenCV Demo', size=(40, 1), justification='center', font='Helvetica 20')],
|
|
|
|
[sg.Image(filename='', key='image')],
|
2018-10-15 20:07:23 +00:00
|
|
|
[sg.ReadButton('Record', size=(10, 1), font='Helvetica 14'),
|
|
|
|
sg.RButton('Stop', size=(10, 1), font='Any 14'),
|
|
|
|
sg.ReadButton('Exit', size=(10, 1), font='Helvetica 14'),
|
2018-10-09 01:02:43 +00:00
|
|
|
sg.RButton('About', size=(10,1), font='Any 14')]]
|
|
|
|
|
|
|
|
# create the window and show it without the plot
|
|
|
|
window = sg.Window('Demo Application - OpenCV Integration',
|
|
|
|
location=(800,400))
|
2018-10-15 20:07:23 +00:00
|
|
|
window.Layout(layout).Finalize()
|
2018-10-09 01:02:43 +00:00
|
|
|
|
|
|
|
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
|
|
|
|
cap = cv.VideoCapture(0)
|
2018-10-15 20:07:23 +00:00
|
|
|
recording = False
|
2018-10-09 01:02:43 +00:00
|
|
|
while True:
|
2018-10-15 20:07:23 +00:00
|
|
|
event, values = window.ReadNonBlocking()
|
2018-10-09 01:02:43 +00:00
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
if event == 'Exit' or values is None:
|
2018-10-09 01:02:43 +00:00
|
|
|
sys.exit(0)
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Record':
|
|
|
|
recording = True
|
|
|
|
elif event == 'Stop':
|
|
|
|
recording = False
|
|
|
|
img = Image.new('RGB', (640, 480), (255, 255, 255))
|
|
|
|
bio = io.BytesIO() # a binary memory resident stream
|
|
|
|
img.save(bio, format='PNG') # save image as png to it
|
|
|
|
imgbytes = bio.getvalue()
|
|
|
|
window.FindElement('image').Update(data=imgbytes)
|
|
|
|
elif event == 'About':
|
2018-10-09 01:02:43 +00:00
|
|
|
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)
|
2018-10-15 20:07:23 +00:00
|
|
|
if recording:
|
|
|
|
ret, frame = cap.read()
|
2018-10-09 01:02:43 +00:00
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
2018-10-09 01:02:43 +00:00
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
# let img be the PIL image
|
|
|
|
img = Image.fromarray(gray) # create PIL image from frame
|
|
|
|
bio = io.BytesIO() # a binary memory resident stream
|
|
|
|
img.save(bio, format= 'PNG') # save image as png to it
|
|
|
|
imgbytes = bio.getvalue() # this can be used by OpenCV hopefully
|
|
|
|
window.FindElement('image').Update(data=imgbytes)
|
2018-10-09 01:02:43 +00:00
|
|
|
|
|
|
|
main()
|