-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
198 lines (152 loc) · 5.81 KB
/
main.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
import cv2
import numpy as np
import datetime
import statistics
catCascade = cv2.CascadeClassifier('cascade.xml')
def interperet_code(c1, c2, gray):
"""
Calculates the positions of all cells
Scans cells and generates a string of the binary number
"""
x_diff = abs(c2[0] - c1[0])
row1 = [x_diff * 0.25, x_diff * 0.5, x_diff * 0.75, x_diff]
row2 = [0, x_diff * 0.25, x_diff * 0.5, x_diff * 0.75]
dark_level = 60 # lower this if white cells are being interpreted as black
bin_str = ""
for i in row1:
x = c1[0] + int(i)
y = c1[1]
if np.any(gray[y, x] < dark_level):
bin_str += "1"
else:
bin_str += "0"
for i in row2:
x = c1[0] + int(i)
y = c2[1]
if np.any(gray[y, x] < dark_level):
bin_str += "1"
else:
bin_str += "0"
return bin_str
def code_scanner():
"""
Scans video feed for cat matches
Gets positions of the centers of cats if 2 are detected
Calls interperet_code
"""
print("Video Starting...")
video_capture = cv2.VideoCapture(0)
print("Awaiting Code...")
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cats = catCascade.detectMultiScale(
gray,
scaleFactor=1.005, # should be 1.01
minNeighbors=5, # should be 4
)
for (x, y, w, h) in cats:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
if len(cats) >= 2:
x, y, w, h = cats[0]
c1 = [int((2 * x + w) / 2), int((2 * y + h) / 2)]
x, y, w, h = cats[1]
c2 = [int((2 * x + w) / 2), int((2 * y + h) / 2)]
if c1[0] < c2[0]: # making sure c1 is the one on the left
pass
elif c1[0] > c2[0]:
c1, c2 = c2, c1
if int(interperet_code(c1, c2, gray), 2) < 128: # arbitrary number, I just know my codes dont go past 128
video_capture.release()
cv2.destroyAllWindows()
return int(interperet_code(c1, c2, gray), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'): # so you can still close the window if it doesnt find anything
break
video_capture.release()
cv2.destroyAllWindows()
def main():
"""
Managing files and CLI
"""
code = code_scanner()
print(f'Code Found: {code}')
with open(f'HoursFiles/hoursfile{code}.txt', 'r') as f:
lines = f.readlines()
f.close()
if lines[0] != "None\n":
clin = input(f"\nWelcome, {lines[0]}What would you like to do?\n1 - Clock In\n2 - Clock Out\n3 - Check Hours\n")
try:
clin = int(clin)
except ValueError:
print("Input was not an int!")
if clin == 1:
now = datetime.datetime.now().isoformat()
with open('clockinlog.txt', 'a') as f:
f.write(f"{str(code)}${now}\n")
f.close()
with open(f'HoursFiles/hoursfile{code}.txt', 'r') as f:
lines = f.readlines()
with open(f'HoursFiles/hoursfile{code}.txt', 'w') as f:
lines[2] = "1"
for i in lines:
f.write(i)
f.close()
f.close()
print(f"\nClocked in at {now}")
elif clin == 2:
now = datetime.datetime.now()
with open('clockinlog.txt', 'r') as f:
logs = f.readlines()
logs = logs[::-1]
f.close()
past = datetime.datetime.now() # failsafe
with open(f'HoursFiles/hoursfile{code}.txt', 'r') as f:
lines = f.readlines()
for i in logs:
if int(i.split('$')[0]) == code and lines[2] == "1":
isostr = i.split('$')[1]
past = datetime.datetime.fromisoformat(isostr[:len(isostr) - 1])
break
time_to_add = (now - past).total_seconds()
time_to_add = time_to_add / 3600 # converting to hours
hours = float(lines[1][:len(lines[1]) - 1])
hours += time_to_add
lines[1] = str(hours) + "\n"
lines[2] = "0"
with open(f'HoursFiles/hoursfile{code}.txt', 'w') as f:
f.writelines(lines)
f.close()
f.close()
with open(f'HoursFiles/hoursfile{code}.txt', 'r') as f:
lines = f.readlines()
print(f"\nHours: {lines[1]}")
elif clin == 3:
with open(f'HoursFiles/hoursfile{code}.txt', 'r') as f:
lines = f.readlines()
print(f"\nHours: {lines[1]}")
elif lines[0] == "None\n":
clin = input(f"\nWould you like to create a file for code # {code}?\n1 - Yes\n2 - No\n")
try:
clin = int(clin)
except ValueError:
print("Input was not an int!")
if clin == 1:
name = input("\nEnter Your Name: ")
with open(f'HoursFiles/hoursfile{code}.txt', 'r') as f:
lines = f.readlines()
lines[0] = name + "\n"
lines[1] = "0" + "\n"
lines[2] = "0"
with open(f'HoursFiles/hoursfile{code}.txt', 'w') as f:
f.writelines(lines)
f.close()
f.close()
print(f"\nFile created - Code:{code} User:{name}")
elif clin == 2:
pass
clin = input("\nWould you like to run the program again?\n1 - Yes (Suggested)\n2 - No (Quit)\n")
if clin == "1": main()
if clin == "2": pass
if __name__ == "__main__":
main()