-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
347 lines (257 loc) · 11.3 KB
/
Copy pathMain.py
File metadata and controls
347 lines (257 loc) · 11.3 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
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
import Tool_Estimation as TE
import SlackBot as SB
import Spreadsheet as SS
import configparser
import cv2 as cv
import time
import joblib
import datetime
import tkinter
import tkinter.ttk as ttk
import sys
# import keyboard
from PIL import Image, ImageTk
import threading
import winsound
#デバック用
import random
# INTERVAL = 10 #[minute]
# notice_time = [["12:00", "16:20"], [False, False]] #一旦工具の返却を促す時間の設定(通知回数の増減は配列の要素数)
values = []
def init():
flag_once = 0
last_time = datetime.datetime.now()
print("カメラ起動中...")
cap = cv.VideoCapture(0)
detector = cv.QRCodeDetector()
print("カメラ起動完了")
print("各種初期データを取得中...")
config = configparser.ConfigParser()
#Slackのトークンとスプレッドシートのキーを取得
config.read("config.ini", encoding="utf-8")
slack_token = config['Slack']['token']
key = config['Spreadsheet']['Key']
print(key)
print(slack_token)
# 工具の初期データを読み込み
init_img, init_tool, init_center = joblib.load(open("initial_data.txt", 'rb'))
past_img = init_img
print("データ取得完了")
#スプレッドシートのキーを使用してスプレッドシートを開く
print("スプレッドシートとSlackBotの設定中...")
try:
SS.init(key)
SB.init(slack_token)
print("スプレッドシートとSlackBotの設定完了")
except TimeoutError:
print("[ERROR/SS] スプレッドシートに接続できません。終了しています...")
# 前回起動時のデータをスプレッドシートから取得
past_tool, past_center = TE.past_data_acquisition(init_tool, init_center)
interval = SS.get_interval_time()
notice_time = SS.get_notice_time()
print("interval:" + interval + ", noticetime:" + str(notice_time))
print(past_center)
print("QRコードをかざしてください")
return last_time, cap, detector, init_img, init_tool, init_center, past_img, past_tool, past_center, flag_once, interval, notice_time
def main(last_time, cap, detector, init_tool, init_center, past_tool, past_center, flag_once, interval, notice_time, cnt, num):
ret, frame = cap.read()
if flag_once == 0:
update_main_canvas(ret, frame)
#フレームが正しく読み取られた場合、retはTrue
if not ret:
print("[ERROR] フレームは受信できません。終了しています...")
judge_notice(notice_time)
try:
#QRコード読み込み処理
output = detector.detectAndDecode(frame)
# print("通知時間:" + str(notice_time))
# print("インターバル:" + str(interval))
if (output[0] != "" and flag_once == 0) or time_comparison(last_time, interval):
# cam1_frame.tkraise()
last_time = datetime.datetime.now()
print(last_time.strftime('%Y-%m-%d %H:%M:%S') + " id:" + str(output[0]))
flag_once = 1
#読み込み成功したら
if 'output' != None:
winsound.Beep(3000, 1000)
print("処理")
thread = threading.Thread(target=processing, args=(output[0], last_time, cap, frame, init_tool, init_center, past_tool, past_center, flag_once))
thread.start()
else:
if(cnt == 15):
num = update_label(num)
cnt = 0
else:
cnt = cnt + 1
if len(values) > 0:
main_frame.tkraise()
flag_once = values[0]
past_tool = values[1]
past_center = values[2]
values.clear()
except Exception as e: #QRコード周りでエラーがちょくちょく出るのでとりあえずこれで対応
print("error出たよ")
print('=== エラー内容 ===')
print('type:' + str(type(e)))
print('args:' + str(e.args))
print('e自身:' + str(e))
print('=================')
print("QRコードをかざしてください")
root.after(1,main, last_time, cap, detector, init_tool, init_center, past_tool, past_center, flag_once, interval, notice_time, cnt, num)
def processing(id, last_time, cap, frame, init_tool, init_center, past_tool, past_center, flag_once):
#画像撮影処理
time.sleep(1)
ret, frame = cap.read()
cv.imwrite('camera_test.jpg', frame)
#デバック用(不要時はコメントアウト)
# frame = cv.imread("now0.jpg")
# 0:initial 1:ペンチなし 2:ドライバーとラジオペンチなし 3:やすりとペンチとはさみなし 4:ドライバーなし 5:ペンチとラジオペンチとドライバーなし
# rand_num = random.randint(0, 5)
# frame = cv.imread("now_images/now" + str(rand_num) + ".jpg")
# print("rand_num:" + str(rand_num))
# update_canvas(ret, frame)
# time.sleep(0.5)
# cam2_frame.tkraise()
#工具判別処理
print("処理中")
#グレースケールで読み込み
gray_img = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
#画像のエッジ検出を行い、エッジの中を埋める
# now_img, tmp_tool, tmp_center = TE.img_processing_debug(gray_img) #デバック用
now_img, tmp_tool, tmp_center = TE.img_processing(gray_img)
update_main_canvas(ret, now_img)
# print(TE.maching(cv.imread("now0.jpg"), cv.imread("now0.jpg")))
time.sleep(1)
print("現在の工具数:" + str(len(tmp_tool)))
cam1_frame.tkraise()
# for i in range(len(tmp_tool)):
# if(tmp_tool[i] != "none"):
# cv.imwrite("test_img0"+str(i+1)+".jpg", tmp_tool[i])
# 今の工具の順番を初期の画像の順番と合わせる
now_tool, now_center = TE.number_sequencing(init_tool, init_center, tmp_tool, tmp_center)
# cv.imwrite("test_img10.jpg", now_img)
# デバク用
# for i in range(len(now_tool)):
# if(now_tool[i] != "none"):
# cv.imwrite("test_img0"+str(i+1)+".jpg", now_tool[i])
print(now_center)
# 前回の工具の画像と順番に比較していって
# 無くなっていたら貸し出し関数、戻っていたら返却関数を呼び出す
TE.comparison(id, init_tool, past_tool, past_center, now_tool, now_center, last_time)
#今回の値を次回に引き継ぎ
past_tool = now_tool
past_center = now_center
print("処理完了")
print("QRコードをかざしてください")
flag_once = 0
# return flag_once, past_tool, past_center
values.append(flag_once)
values.append(past_tool)
values.append(past_center)
# except Exception as e: #QRコード周りでエラーがちょくちょく出るのでとりあえずこれで対応
# print("error出たよ")
# print('=== エラー内容 ===')
# print('type:' + str(type(e)))
# print('args:' + str(e.args))
# print('e自身:' + str(e))
# print('=================')
# print("QRコードをかざしてください")
def time_comparison(last_time, interval):
now_time = datetime.datetime.now()
if now_time > last_time + datetime.timedelta(minutes = int(interval)): return True
else: return False
def judge_notice(notice_time):
now_time = datetime.datetime.now()
for i in range(len(notice_time[0])):
divided_time = notice_time[0][i].split(':')
comparison_time = now_time.replace(hour=int(divided_time[0]), minute=int(divided_time[1]), second=0, microsecond=0)
if now_time > comparison_time: # 指定時間を過ぎた
minute = now_time.minute - comparison_time.minute
hour = now_time.hour - comparison_time.hour
if (hour == 0) and (minute <= 10) and (notice_time[1][i] == False): # 指定時間+10分以内
print("通知")
SB.write_channel("#random", "時間です。使用していない工具は一旦返却しましょう!")
notice_time[1][i] = True
if len(notice_time[0])-1 >= i+1:
notice_time[1][i+1] = False
else:
notice_time[1][0] = False
def end_program():
root.destroy()
print("処理を終了しました。")
sys.exit()
def window(last_time, cap, detector, init_tool, init_center, past_tool, past_center, flag_once, interval, notice_time):
global root, main_canvas, titleLabel1
global main_frame, cam1_frame
root = tkinter.Tk()
root.state('zoomed')
# root.attributes("-fullscreen", True)
root.resizable(False, False)
m_width, m_height = get_monitor_size(root)
root.title("Tkinter_cv2")
# ウィンドウの大きさを決定
root.geometry("800x600")
# ウィンドウのグリッドを 1x1 にする
# この処理をコメントアウトすると配置がズレる
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
#-----------------------------main_frame---------------------------------
# メインページフレーム作成
main_frame = tkinter.Frame()
main_frame.grid(row=0, column=0, sticky="nsew")
margin = 0
# タイトルラベル作成
# titleLabel1 = tkinter.Label(main_frame, text="工具を取る or 工具を戻してから", font=('Helvetica', '35'))
# titleLabel1.grid(row=0, column=0, sticky="nsew")
titleLabel0 = tkinter.Label(main_frame, text="工具を取る or 工具を戻してから、QRコードをかざしてください。", font=('Helvetica', '35'))
titleLabel0.grid(row=0, column=0, sticky="nsew")
main_canvas = tkinter.Canvas(main_frame, width = m_width, height = m_height)
main_canvas.grid(row=1, column=0, sticky="nsew")
#-----------------------------cam1_frame---------------------------------
# カメラ1用フレーム作成
cam1_frame = tkinter.Frame()
cam1_frame.grid(row=0, column=0, sticky="nsew")
titleLabel1 = tkinter.Label(cam1_frame, text="処理中...", font=('Helvetica', '60'))
titleLabel1.grid(row=0, column=0, sticky=tkinter.W)
titleLabel2 = tkinter.Label(cam1_frame, text="処理の完了を待つ必要はありません。作業にお戻りください。", font=('Helvetica', '50'))
titleLabel2.grid(row=1, column=0, padx=margin, sticky=tkinter.W)
# # カメラ1用キャンバス作成
# cam11_canvas = tkinter.Canvas(cam1_frame, width = m_width/2, height = m_height)
# cam11_canvas.grid(row=1, column=0, padx=0, sticky="nsew")
# cam12_canvas = tkinter.Canvas(cam1_frame, width = m_width/2, height = m_height)
# cam12_canvas.grid(row=1, column=1, padx=0, sticky="nsew")
#main_frameを一番上に表示
main_frame.tkraise()
main(last_time, cap, detector, init_tool, init_center, past_tool, past_center, flag_once, interval, notice_time, 0, 0)
root.mainloop()
def changePage(page):
page.tkraise()
def update_main_canvas(ret, frame):#update
global img
if ret:
width, height = get_monitor_size(root)
img_resize = cv.resize(frame, (640*2, 480*2))
img = ImageTk.PhotoImage(Image.fromarray(cv.cvtColor(img_resize, cv.COLOR_BGR2RGB)))
main_canvas.create_image (width/2, height/2.2, image=img)
else:
print("u-Fail")
def update_label(num):
if(num == 0):
titleLabel1["text"] = "処理中"
return 1
elif(num == 1):
titleLabel1["text"] = "処理中."
return 2
elif(num == 2):
titleLabel1["text"] = "処理中.."
return 3
elif(num == 3):
titleLabel1["text"] = "処理中..."
return 0
def get_monitor_size(root):
m_width = root.winfo_screenwidth() #モニターの横幅を渡す
m_height = root.winfo_screenheight() #モニターの縦幅を渡す
return m_width, m_height
if __name__ == "__main__":
last_time, cap, detector, init_img, init_tool, init_center, past_img, past_tool, past_center, flag_once, interval, notice_time = init()
window(last_time, cap, detector, init_tool, init_center, past_tool, past_center, flag_once, interval, notice_time)