-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathCamera.py
More file actions
56 lines (42 loc) · 1.68 KB
/
Camera.py
File metadata and controls
56 lines (42 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pyautogui
import PySimpleGUI as sg
import cv2
import numpy as np
"""
Demo program that displays a webcam using OpenCV
"""
def main():
sg.theme('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='Arial 14'),
sg.Button('Stop', size=(10, 1), font='Arial 14'),
sg.Button('Exit', size=(10, 1), font='Arial 14'),
sg.Button('Screenshot',size=(10,1),font='Arial 14') ]]
# create the window and show it without the plot
window = sg.Window('Demo Application - OpenCV Integration',
layout, location=(800, 400))
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
cap = cv2.VideoCapture(0)
recording = False
while True:
event, values = window.read(timeout=20)
if event == 'Exit' or event == sg.WIN_CLOSED:
return
elif event == 'Record':
recording = True
elif event=='Screenshot':
myScreenshot = pyautogui.screenshot()
myScreenshot.save(r'shot.png')
elif event == 'Stop':
recording = False
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)
if recording:
ret, frame = cap.read()
imgbytes = cv2.imencode('.png', frame)[1].tobytes() # ditto
window['image'].update(data=imgbytes)
main()