2018-10-09 01:02:43 +00:00
|
|
|
#!/usr/bin/env python
|
2019-08-13 20:54:17 +00:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
# import PySimpleGUIQt as sg
|
2018-10-19 17:57:25 +00:00
|
|
|
import cv2
|
2018-10-15 20:07:23 +00:00
|
|
|
import numpy as np
|
2019-08-13 20:54:17 +00:00
|
|
|
import sys
|
2018-10-09 01:02:43 +00:00
|
|
|
from sys import exit as exit
|
|
|
|
|
|
|
|
"""
|
|
|
|
Demo program that displays a webcam using OpenCV
|
|
|
|
"""
|
|
|
|
def main():
|
|
|
|
|
2019-08-13 20:54:17 +00:00
|
|
|
sg.ChangeLookAndFeel('Black')
|
2018-10-09 01:02:43 +00:00
|
|
|
|
|
|
|
# define the window layout
|
|
|
|
layout = [[sg.Text('OpenCV Demo', size=(40, 1), justification='center', font='Helvetica 20')],
|
|
|
|
[sg.Image(filename='', key='image')],
|
2018-10-29 00:01:03 +00:00
|
|
|
[sg.Button('Record', size=(10, 1), font='Helvetica 14'),
|
|
|
|
sg.Button('Stop', size=(10, 1), font='Any 14'),
|
2019-08-13 20:54:17 +00:00
|
|
|
sg.Button('Exit', size=(10, 1), font='Helvetica 14'),]]
|
2018-10-09 01:02:43 +00:00
|
|
|
|
|
|
|
# create the window and show it without the plot
|
2019-08-13 20:54:17 +00:00
|
|
|
window = sg.Window('Demo Application - OpenCV Integration', layout,
|
2018-10-09 01:02:43 +00:00
|
|
|
location=(800,400))
|
|
|
|
|
|
|
|
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
|
2018-10-19 17:57:25 +00:00
|
|
|
cap = cv2.VideoCapture(0)
|
2018-10-15 20:07:23 +00:00
|
|
|
recording = False
|
2018-10-09 01:02:43 +00:00
|
|
|
while True:
|
2019-08-13 20:54:17 +00:00
|
|
|
event, values = window.Read(timeout=20)
|
2018-10-29 00:01:03 +00:00
|
|
|
if event == 'Exit' or event 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
|
2018-10-19 17:57:25 +00:00
|
|
|
img = np.full((480, 640),255)
|
|
|
|
imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes
|
2018-10-15 20:07:23 +00:00
|
|
|
window.FindElement('image').Update(data=imgbytes)
|
2019-08-13 20:54:17 +00:00
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
if recording:
|
|
|
|
ret, frame = cap.read()
|
2018-10-19 17:57:25 +00:00
|
|
|
imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto
|
2018-10-15 20:07:23 +00:00
|
|
|
window.FindElement('image').Update(data=imgbytes)
|
2018-10-09 01:02:43 +00:00
|
|
|
|
2018-10-19 17:57:25 +00:00
|
|
|
main()
|
2018-10-29 00:01:03 +00:00
|
|
|
exit()
|