forked from TrevorSatori/Scutti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScutti.py
353 lines (271 loc) · 11.2 KB
/
Scutti.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import os
from mss import mss
import mss.tools
import ctypes
import numpy
import cv2
import time
import keyboard
import random
import sys
from playsound import playsound
if sys.platform == 'darwin':
from AppKit import NSScreen
class Scutti:
# Attributes
res = 0
width = 0
height = 0
collection = None
items = None
soundSnippets = None
snapKey = ''
quitKey = ''
interval = 5
delay = 0
camera = 0
# Initializes Class
def __init__(self):
self.res = self.Resolution()
self.xStart = 0
self.yStart = 0
self.width = self.Width()
self.height = self.Height()
self.collection = os.path.join(os.getcwd(), 'collected-data')
self.items = self.getItems()
self.soundSnippets = self.getSounds()
self.snapKey = 'g'.lower()
self.quitKey = 'q'.lower()
self.interval = 5
self.delay = 0
self.camera = 0
# Gets Width of Windows Monitor
def Width(self):
if sys.platform == 'win32':
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
width = user32.GetSystemMetrics(0)
elif sys.platform == 'darwin':
width = NSScreen.mainScreen().frame().size.width
return width
# Gets Height of Windows Monitor
def Height(self):
if sys.platform == 'win32':
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
height = user32.GetSystemMetrics(1)
elif sys.platform == 'darwin':
height = NSScreen.mainScreen().frame().size.height
return height
def Resolution(self):
if sys.platform == 'win32':
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
[w, h] = [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)]
elif sys.platform == 'darwin':
[w, h] = [NSScreen.mainScreen().frame().size.width, NSScreen.mainScreen().frame().size.height]
return w,h
# Error handling
def seemsLegit(self):
if (self.xStart != 0):
if self.Width() - (self.xStart + self.getWidth()) < 0:
print('Screenshot out of bounds!')
exit()
if (self.yStart != 0):
if self.Height() - (self.yStart + self.getHeight()) < 0:
print('Screenshot out of bounds!')
exit()
if self.snapKey.lower() == self.quitKey.lower():
print('Capture Key and Quit key cannot be the same')
exit()
''' Getters '''
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def getcollection(self):
return self.collection
def getItems(self):
items = []
with os.scandir(self.getcollection()) as dir:
for entries in dir:
items.append(entries.name)
return items
def getSounds(self):
FunkySide = os.path.join(os.getcwd(), 'SoundSnippets')
# adds all soundSnippets to array
sounds = []
with os.scandir(FunkySide) as dirs:
for entry in dirs:
sounds.append(entry.name)
return sounds
def getSnapKey(self):
return self.snapKey
def getQuitKey(self):
return self.quitKey
def getInterval(self):
return self.interval
def getCamera(self):
return self.camera
def getXStart(self):
return self.xStart
def getYStart(self):
return self.yStart
def getDelay(self):
return self.delay
''' Setters '''
def setcollection(self, name):
collectionLoc = os.path.join(os.getcwd(), 'collected-data', name)
if os.path.exists(collectionLoc):
self.collection = collectionLoc
else:
os.mkdir(collectionLoc)
self.collection = collectionLoc
print('Subfolder for collection', name, 'created in collected-data')
def setWidth(self, width):
self.width = width
def setHeight(self, height):
self.height = height
def setSnapKey(self, snapKey):
self.snapKey = str(snapKey).lower()
def setQuitKey(self, quitKey):
self.quitKey = str(quitKey).lower()
def setInterval(self, interval):
self.interval = interval
def setCamera(self, camera):
self.camera = int(camera - 1)
def setXStart(self, xStart):
self.xStart = xStart
def setYStart(self, yStart):
self.yStart = yStart
def setDelay(self, delay):
self.delay = delay
''' Functions '''
# Coordinates start at the top left of the screen.
# The higher the ystart value, the lower the capture.
# The higher the xstart value, the farther right it begins.
def screenshot(self):
with mss.mss() as sct:
# The area of screen to be captured
monitor = {'top': self.yStart, 'left': self.xStart, 'width': self.width, 'height': self.height}
# filename gets name of working directory, and creates name of files based on collection.
filename = self.getcollection().split('\\')[-1] + str(len(self.getItems())) + '.png'
fileOut = os.path.join(self.getcollection(), filename)
# Grab the data
sct_img = sct.grab(monitor)
# Save to the picture file
mss.tools.to_png(sct_img.rgb, sct_img.size, output=fileOut)
# takes screenshots on keypress.
# snapkey is the key that triggers screenshot, benchmark is forra tha Borrat.
def sctManual(self, benchmark = 25):
print(self.snapKey, 'chosen as screenshot key, to quit press ' + self.quitKey + ' Enjoy!')
# Specifies directory of soundSnippets
FunkySide = os.path.join(os.getcwd(), 'SoundSnippets')
# 2 counts to adjust for the Borat
count = 0
imgCount = 0
while True:
# Selects random sound from soundSnippets
soundSelect = os.path.join(FunkySide, random.choice(self.soundSnippets))
# if key is pressed take screenshot.
if keyboard.is_pressed(self.snapKey):
self.screenshot()
count += 1
time.sleep(.01)
# every 25 images let us know. Great Success
if count % benchmark == 0:
playsound(soundSelect)
count += 1
imgCount = count - 1
print('Images Taken: ' + str(imgCount))
if keyboard.is_pressed(self.quitKey):
print('Exiting')
break
GreatSuccess = os.path.join(FunkySide, 'GreatSuccess.mp3')
playsound(GreatSuccess)
# takes screenshots on time interval.
def sctAuto(self):
print(self.interval, 'chosen as screenshot interval, to quit press', self.quitKey, 'Enjoy!')
if self.delay != 0:
print('delay set for:', self.delay)
time.sleep(self.delay)
print('starting capture')
# Specifies directory of soundSnippets
FunkySide = os.path.join(os.getcwd(), 'SoundSnippets')
imgCount = 0
t = int(time.time())
while True:
whileTime = int(time.time())
# every interval take a screenshot
if whileTime - t > 0 and (whileTime - t) % self.interval == 0:
self.screenshot()
imgCount += 1
time.sleep(1)
print('image Count: ' + str(imgCount))
if keyboard.is_pressed(self.quitKey):
print(self.quitKey + ' pressed, Exiting')
break
print(imgCount, 'Images gathered.')
GreatSuccess = os.path.join(FunkySide, 'GreatSuccess.mp3')
playsound(GreatSuccess)
def camManual(self):
print(self.snapKey, 'chosen as screenshot key, to quit press q. Enjoy!')
# Specifies directory of soundSnippets
FunkySide = os.path.join(os.getcwd(), 'SoundSnippets')
# Camera to be captured
vid = cv2.VideoCapture(self.camera)
count = 0
while True:
# filename gets name of working directory, and creates name of files based on collection.
filename = self.getcollection().split('\\')[-1] + str(len(self.getItems())) + '.png'
fileOut = os.path.join(self.getcollection(), filename)
ret, frame = vid.read()
cv2.imshow('Scutti', frame)
# if key is pressed take screenshot.
if keyboard.is_pressed(self.snapKey):
cv2.imwrite(fileOut, frame)
count += 1
time.sleep(.15)
cv2.waitKey(1)
# We implement keyboard rather than waitkey & 0xFF To remove lag as well as enable global macro
if keyboard.is_pressed(self.quitKey):
break
vid.release()
cv2.destroyAllWindows()
print(str(count), 'Images gathered.')
GreatSuccess = os.path.join(FunkySide, 'GreatSuccess.mp3')
playsound(GreatSuccess)
def camAuto(self):
print(self.interval, 'chosen as time interval, to quit press', self.quitKey, 'Enjoy!')
# Specifies directory of soundSnippets
FunkySide = os.path.join(os.getcwd(), 'SoundSnippets')
if self.delay != 0:
print('delay set for:', self.delay)
time.sleep(self.delay)
print('starting capture')
# Camera to be captured
vid = cv2.VideoCapture(self.camera)
count = 0
t = int(time.time())
while True:
whileTime = int(time.time())
# filename gets name of working directory, and creates name of files based on collection.
filename = self.getcollection().split('\\')[-1] + str(len(self.getItems())) + '.png'
fileOut = os.path.join(self.getcollection(), filename)
ret, frame = vid.read()
cv2.imshow('Scutti', frame)
# every interval take a screenshot
if (whileTime - t) > 0 and (whileTime - t) % self.interval == 0:
cv2.imwrite(fileOut, frame)
count += 1
time.sleep(1)
print('image Count: ' + str(count))
cv2.waitKey(1)
# We implement keyboard rather than waitkey & 0xFF To remove lag as well as enable global macro
if keyboard.is_pressed(self.quitKey):
break
vid.release()
cv2.destroyAllWindows()
print(str(count), 'Images gathered.')
GreatSuccess = os.path.join(FunkySide, 'GreatSuccess.mp3')
playsound(GreatSuccess)