This repository has been archived by the owner on Mar 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgui.py
183 lines (159 loc) · 5.81 KB
/
gui.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
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 27 20:58:04 2019
@author: ACHU
"""
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
from Main import *
from GenData import *
import cv2
import os
import shutil
import time
import viewlogs
from tkintertable import TableCanvas, TableModel
import threading
cap = cv2.VideoCapture(1)
class gui:
def open_video_show(self):
global cap
if self.loclavideoststus:
if not cap.isOpened():
cap = cv2.VideoCapture(self.path)
_, self.frame = cap.read()
cv2image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGBA)
imgtk = ImageTk.PhotoImage(Image.fromarray(cv2image))
self.lmain.imgtk = imgtk
self.lmain.configure(image=imgtk)
self.lmain.after(1, self.open_video_show)
def video_show(self):
global cap
if (self.stopped):
if not cap.isOpened():
cap = cv2.VideoCapture(1)
_, self.frame = cap.read()
cv2image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
self.lmain.imgtk = imgtk
self.lmain.configure(image=imgtk)
self.lmain.after(1, self.video_show)
def video_process(self):
while(self.status):
if self.e.wait():
time.sleep(60)
main(self.frame)
def exit(self):
global cap
cap.release()
self.root.destroy()
self.stopped=False
self.status=False
def view_log(self):
viewlogs.view()
def clear_log(self):
try:
shutil.rmtree(r"logs/images")
open(r"logs/log.txt", 'w').close()
except OSError as e:
print ("Error: %s - %s." % (e.filename, e.strerror))
os.mkdir(r"logs/images")
def video_stream(self):
self.stopped=True
self.loclavideoststus=False
cap.release()
self.video_show()
time.sleep(10)
self.e.set()
if not self.t2.is_alive():
self.t2.start()
def onOpenVideo(self):
global cap
self.loclavideoststus=True
self.stopped=False
cap.release()
self.path=filedialog.askopenfilename(filetypes=[("Video File",'.mp4'),("All Files",'.*')])
if self.path:
self.open_video_show()
time.sleep(10)
self.e.set()
if not (self.t2.is_alive()):
self.t2.start()
else:
im = Image.open("trainData/no_display.jpg")
tkimage = ImageTk.PhotoImage(im)
self.lmain.image = tkimage
self.lmain.configure(image=tkimage)
self.lmain.grid()
def onOpen(self):
global cap
cap.release()
self.loclavideoststus=False
self.stopped=False
self.e.clear()
path=filedialog.askopenfilename(filetypes=[("Image File",'.jpg'),("All Files",'.*')])
if path:
try:
im = Image.open(path)
img = cv2.imread(path)
resize=im.resize((400,400),Image.ANTIALIAS)
tkimage = ImageTk.PhotoImage(resize)
self.lmain.imgtk = tkimage
self.lmain.configure(image=tkimage)
main(img)
except IOError:
print('An error occurred trying to read the file.')
else:
im = Image.open("trainData/no_display.jpg")
tkimage = ImageTk.PhotoImage(im)
self.lmain.image = tkimage
self.lmain.configure(image=tkimage)
self.lmain.grid()
def onTrain(self):
global cap
cap.release()
self.loclavideoststus=False
self.stopped=False
path=filedialog.askopenfilename(filetypes=[("Image File",'.png'),("All Files",'.*')])
if path:
tData(path)
else:
im = Image.open("trainData/no_display.jpg")
tkimage = ImageTk.PhotoImage(im)
self.lmain.image = tkimage
self.lmain.configure(image=tkimage)
self.lmain.grid()
def __init__(self,frame=None):
self.stopped=None
self.root = Tk()
self.root.title("NoParkingVehicleDetection")
self.root.geometry("600x400")
self.frame= None
self.loclavideoststus=None
self.t2=threading.Thread(target=self.video_process,args=())
self.e = threading.Event()
top = Frame(self.root, borderwidth=2, relief="solid")
top.pack(side="top", expand=True, fill="both")
self.lmain = Label(top)
im = Image.open("trainData/no_display.jpg")
tkimage = ImageTk.PhotoImage(im)
self.lmain.image = tkimage
self.lmain.configure(image=tkimage)
self.lmain.grid()
self.status=True
root_menu=Menu(self.root)
self.root.config(menu=root_menu)
file_menu=Menu(root_menu)
log_menu=Menu(root_menu)
root_menu.add_cascade(label="File",menu=file_menu)
root_menu.add_cascade(label="Logs",menu=log_menu)
log_menu.add_command(label="View Logs",command=self.view_log)
log_menu.add_command(label="Clear All Logs",command=self.clear_log)
file_menu.add_command(label="Open Image",command=self.onOpen)
file_menu.add_command(label="Train Data",command=self.onTrain)
file_menu.add_command(label="Open Video",command=self.onOpenVideo)
file_menu.add_command(label="Live Stream",command=self.video_stream)
file_menu.add_command(label="Exit",command=self.exit)
self.root.mainloop()