2018-10-09 01:02:43 +00:00
|
|
|
#!/usr/bin/env python
|
2019-08-13 20:54:17 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-10-19 17:57:25 +00:00
|
|
|
import cv2
|
2018-10-15 20:07:23 +00:00
|
|
|
import numpy as np
|
2018-10-09 01:02:43 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Demo program that displays a webcam using OpenCV
|
|
|
|
"""
|
2019-10-23 20:10:03 +00:00
|
|
|
|
|
|
|
|
2018-10-09 01:02:43 +00:00
|
|
|
def main():
|
|
|
|
|
2019-12-24 23:52:47 +00:00
|
|
|
sg.theme('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-10-23 20:10:03 +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-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Demo Application - OpenCV Integration',
|
|
|
|
layout, location=(800, 400))
|
2018-10-09 01:02:43 +00:00
|
|
|
|
|
|
|
# ---===--- 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
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-10-09 01:02:43 +00:00
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read(timeout=20)
|
2018-10-29 00:01:03 +00:00
|
|
|
if event == 'Exit' or event is None:
|
2019-10-23 20:10:03 +00:00
|
|
|
return
|
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Record':
|
|
|
|
recording = True
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Stop':
|
|
|
|
recording = False
|
2019-10-23 20:10:03 +00:00
|
|
|
img = np.full((480, 640), 255)
|
|
|
|
# this is faster, shorter and needs less includes
|
|
|
|
imgbytes = cv2.imencode('.png', img)[1].tobytes()
|
|
|
|
window['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()
|
2019-10-23 20:10:03 +00:00
|
|
|
imgbytes = cv2.imencode('.png', frame)[1].tobytes() # ditto
|
|
|
|
window['image'].update(data=imgbytes)
|
|
|
|
|
2018-10-09 01:02:43 +00:00
|
|
|
|
2018-10-19 17:57:25 +00:00
|
|
|
main()
|