-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicturesScript.py
229 lines (192 loc) · 7.2 KB
/
picturesScript.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import simpleobsws
import asyncio
from tkinter import *
from tkinter.font import BOLD, Font
from PIL import ImageTk, Image
import cv2
import os
import time
from threading import Timer
import glob
from obsServerSettings import *
root = Tk()
## settings
cameraID = 3
camera_name = "Video Capture Device"
backgrounds = ["Berg 1", "Berg 2", "Berg 3", "Blumenwiese"]
sceneName = "Scena"
actual_width = 3840
actual_height = 2160
display_width = actual_width/4
display_height = actual_height/4
default_font = Font(size=18)
button_font = Font(size=16)
# image source
link_background_source = 'Freepik.com'
## obs requests
ws = simpleobsws.WebSocketClient(
url='ws://localhost:' + serverPort, password=serverPassword)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
async def make_request(requestType, requestData=None):
await ws.connect() # Make the connection to obs-websocket
# Wait for the identification handshake to complete
await ws.wait_until_identified()
request = simpleobsws.Request(
requestType, requestData) # Build a Request object
ret = await ws.call(request) # Perform the request
response_data = None
if ret.ok(): # Check if the request succeeded
print("Request succeeded! Response data: {}".format(ret.responseData))
response_data = ret.responseData
await ws.disconnect() # Disconnect from the websocket server cleanly
return response_data
def get_items():
loop = asyncio.get_event_loop()
item_list = loop.run_until_complete(make_request('GetInputList'))
return item_list.get("inputs")
def get_item_id(item_name):
loop = asyncio.get_event_loop()
item_id = loop.run_until_complete(make_request('GetSceneItemId', {"sceneName": sceneName,
"sourceName": item_name}))
print(item_id)
if item_id != None:
return item_id.get("sceneItemId")
else:
return None
def enable_background(item_id):
loop = asyncio.get_event_loop()
loop.run_until_complete(make_request('SetSceneItemEnabled', {"sceneName": sceneName,
"sceneItemId": item_id,
"sceneItemEnabled": True}))
def disable_background(item_id):
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
loop.run_until_complete(make_request('SetSceneItemEnabled', {"sceneName": sceneName,
"sceneItemId": item_id,
"sceneItemEnabled": False}))
## GUI
# video frame
video = Frame(root, bg="white", width=display_width, height=display_height)
video.grid(column=1)
lmain = Label(video)
lmain.grid()
# Capture from camera
def resize_to_diplay_size(cap):
cap.set(cv2.CAP_PROP_FRAME_WIDTH, display_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, display_height)
def resize_to_picture_size(cap):
cap.set(cv2.CAP_PROP_FRAME_WIDTH, actual_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, actual_height)
cap = cv2.VideoCapture(cameraID)
# rawCap = cv2.VideoCapture(rawCameraID)
resize_to_diplay_size(cap)
# resize_to_diplay_size(rawCap)
# function for video streaming
def video_stream():
try:
_, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img, height=display_height, width=display_width)
lmain.imgtk = imgtk
lmain.configure(image=imgtk, height=display_height, width=display_width)
except:
print("An exception occurred in video_stream")
lmain.after(3, video_stream)
video_stream()
# backgrounds
change_background_frame = Frame(root)
change_background_frame.grid(column=0, row=0)
change_background_text = Label(
change_background_frame, text="Hintergrund ändern", font=default_font)
change_background_text.grid()
def change_background(background_name):
print("Enabling background: " + background_name)
item_list = get_items()
for item in item_list:
name = item.get("inputName")
item_id = get_item_id(name)
if item_id != None:
if name == background_name:
enable_background(item_id)
elif name != camera_name:
disable_background(item_id)
background_buttons = []
for background in backgrounds:
background_button = Button(
change_background_frame, text=background, font=button_font)
background_button.bind("<Button-1>", lambda event,
bg=background: change_background(bg))
background_button.grid()
background_buttons.append(background_button)
## take pictures
# frame for buttons
take_pictures_frame = Frame(root)
take_pictures_frame.grid(column=1, row=1)
take_pictures_label = Label(
take_pictures_frame, text="Name des Bildes (optional)", font=default_font)
take_pictures_label.grid()
picture_name_entry = Entry(take_pictures_frame)
picture_name_entry.grid()
# picture naming
photoIndex = 0
def get_picture_name():
typed_name = picture_name_entry.get()
if typed_name == "":
picture_name = ""
else:
picture_name = " - " + typed_name
picture_name_entry.delete(0, END)
return picture_name + ".jpg"
def file_number_exists(number):
return len(glob.glob("img/" + str(number) + ".jpg")) > 0 or len(glob.glob("img/" + str(number) + " - *")) > 0
def get_new_picture_name():
global photoIndex
while file_number_exists(photoIndex):
photoIndex += 1
name = str(photoIndex) + get_picture_name()
photoIndex += 1
return name
button = Button(take_pictures_frame, text="Foto aufnehmen!", font=button_font)
success_text = Label(take_pictures_frame)
success_text.grid()
def take_picture(event):
global photoIndex
name = get_new_picture_name()
# _, rawFrame = rawCap.read()
# if not cv2.imwrite("raw/" + name, frame):
# success_text.config(text="Fehler beim speichern des Fotos.")
resize_to_picture_size(cap)
_, frame = cap.read()
resize_to_diplay_size(cap)
if cv2.imwrite("img/" + name, frame):
success_text.config(
text="Bild erfolgreich gespeichert unter " + os.getcwd() + "\\img\\" + name)
else:
success_text.config(text="Fehler beim speichern des Bildes.")
button.bind("<Button-1>", take_picture)
button.grid()
ten_sec_button = Button(
take_pictures_frame, text="Foto aufnehmen in 10 Sekunden", font=button_font)
ten_sec_frame = Frame(root)
ten_sec_frame.grid(column=2, row=0)
ten_sec_label = Label(ten_sec_frame, font=("Arial", 35, BOLD))
ten_sec_label.grid()
def take_picture_in_n_sec(event, n):
for i in range(n, 0, -1):
ten_sec_label.config(text=str(i))
print(i)
time.sleep(1)
ten_sec_label.config(text="0")
take_picture(event)
ten_sec_label.config(text="")
def take_picture_in_10_sec_in_new_thread(event):
t = Timer(0, take_picture_in_n_sec, args=[None, 10], kwargs=None)
t.start()
ten_sec_button.bind("<Button-1>", take_picture_in_10_sec_in_new_thread)
ten_sec_button.grid()
source_text = Label(take_pictures_frame, text="Quelle der Hintergrundbilder: "+ link_background_source)
source_text.grid()
# start GUI
root.mainloop()