Amazing-Python-Scripts
256 строк · 8.0 Кб
1from tkinter import *
2from PIL import Image, ImageTk
3from tkinter import filedialog
4import object_detection as od
5import imageio
6import cv2
7
8
9class Window(Frame):
10def __init__(self, master=None):
11Frame.__init__(self, master)
12
13self.master = master
14self.pos = []
15self.line = []
16self.rect = []
17self.master.title("GUI")
18self.pack(fill=BOTH, expand=1)
19
20self.counter = 0
21
22menu = Menu(self.master)
23self.master.config(menu=menu)
24
25file = Menu(menu)
26file.add_command(label="Open", command=self.open_file)
27file.add_command(label="Exit", command=self.client_exit)
28menu.add_cascade(label="File", menu=file)
29
30analyze = Menu(menu)
31analyze.add_command(label="Region of Interest",
32command=self.regionOfInterest)
33menu.add_cascade(label="Analyze", menu=analyze)
34
35self.filename = "Images/home.jpg"
36self.imgSize = Image.open(self.filename)
37self.tkimage = ImageTk.PhotoImage(self.imgSize)
38self.w, self.h = (1366, 768)
39
40self.canvas = Canvas(master=root, width=self.w, height=self.h)
41self.canvas.create_image(20, 20, image=self.tkimage, anchor='nw')
42self.canvas.pack()
43
44def open_file(self):
45self.filename = filedialog.askopenfilename()
46
47cap = cv2.VideoCapture(self.filename)
48
49reader = imageio.get_reader(self.filename)
50fps = reader.get_meta_data()['fps']
51
52ret, image = cap.read()
53cv2.imwrite(
54'G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Images/preview.jpg', image)
55
56self.show_image(
57'G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Images/preview.jpg')
58
59def show_image(self, frame):
60self.imgSize = Image.open(frame)
61self.tkimage = ImageTk.PhotoImage(self.imgSize)
62self.w, self.h = (1366, 768)
63
64self.canvas.destroy()
65
66self.canvas = Canvas(master=root, width=self.w, height=self.h)
67self.canvas.create_image(0, 0, image=self.tkimage, anchor='nw')
68self.canvas.pack()
69
70def regionOfInterest(self):
71root.config(cursor="plus")
72self.canvas.bind("<Button-1>", self.imgClick)
73
74def client_exit(self):
75exit()
76
77def imgClick(self, event):
78
79if self.counter < 2:
80x = int(self.canvas.canvasx(event.x))
81y = int(self.canvas.canvasy(event.y))
82self.line.append((x, y))
83self.pos.append(self.canvas.create_line(
84x - 5, y, x + 5, y, fill="red", tags="crosshair"))
85self.pos.append(self.canvas.create_line(
86x, y - 5, x, y + 5, fill="red", tags="crosshair"))
87self.counter += 1
88
89# elif self.counter < 4:
90# x = int(self.canvas.canvasx(event.x))
91# y = int(self.canvas.canvasy(event.y))
92# self.rect.append((x, y))
93# self.pos.append(self.canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair"))
94# self.pos.append(self.canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair"))
95# self.counter += 1
96
97if self.counter == 2:
98# unbinding action with mouse-click
99self.canvas.unbind("<Button-1>")
100root.config(cursor="arrow")
101self.counter = 0
102
103# show created virtual line
104print(self.line)
105print(self.rect)
106img = cv2.imread(
107'G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Images/preview.jpg')
108cv2.line(img, self.line[0], self.line[1], (0, 255, 0), 3)
109cv2.imwrite(
110'G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Images/copy.jpg', img)
111self.show_image(
112'G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Images/copy.jpg')
113
114# for demonstration
115# (rxmin, rymin) = self.rect[0]
116# (rxmax, rymax) = self.rect[1]
117
118# tf = False
119# tf |= self.intersection(self.line[0], self.line[1], (rxmin, rymin), (rxmin, rymax))
120# print(tf)
121# tf |= self.intersection(self.line[0], self.line[1], (rxmax, rymin), (rxmax, rymax))
122# print(tf)
123# tf |= self.intersection(self.line[0], self.line[1], (rxmin, rymin), (rxmax, rymin))
124# print(tf)
125# tf |= self.intersection(self.line[0], self.line[1], (rxmin, rymax), (rxmax, rymax))
126# print(tf)
127
128# cv2.line(img, self.line[0], self.line[1], (0, 255, 0), 3)
129
130# if tf:
131# cv2.rectangle(img, (rxmin,rymin), (rxmax,rymax), (255,0,0), 3)
132# else:
133# cv2.rectangle(img, (rxmin,rymin), (rxmax,rymax), (0,255,0), 3)
134
135# cv2.imshow('traffic violation', img)
136
137# image processing
138self.main_process()
139print("Executed Successfully!!!")
140
141# clearing things
142self.line.clear()
143self.rect.clear()
144for i in self.pos:
145self.canvas.delete(i)
146
147def intersection(self, p, q, r, t):
148print(p, q, r, t)
149(x1, y1) = p
150(x2, y2) = q
151
152(x3, y3) = r
153(x4, y4) = t
154
155a1 = y1-y2
156b1 = x2-x1
157c1 = x1*y2-x2*y1
158
159a2 = y3-y4
160b2 = x4-x3
161c2 = x3*y4-x4*y3
162
163if (a1*b2-a2*b1 == 0):
164return False
165print((a1, b1, c1), (a2, b2, c2))
166x = (b1*c2 - b2*c1) / (a1*b2 - a2*b1)
167y = (a2*c1 - a1*c2) / (a1*b2 - a2*b1)
168print((x, y))
169
170if x1 > x2:
171tmp = x1
172x1 = x2
173x2 = tmp
174if y1 > y2:
175tmp = y1
176y1 = y2
177y2 = tmp
178if x3 > x4:
179tmp = x3
180x3 = x4
181x4 = tmp
182if y3 > y4:
183tmp = y3
184y3 = y4
185y4 = tmp
186
187if x >= x1 and x <= x2 and y >= y1 and y <= y2 and x >= x3 and x <= x4 and y >= y3 and y <= y4:
188return True
189else:
190return False
191
192def main_process(self):
193
194video_src = self.filename
195
196cap = cv2.VideoCapture(video_src)
197
198reader = imageio.get_reader(video_src)
199fps = reader.get_meta_data()['fps']
200writer = imageio.get_writer(
201'G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Resources/output/output.mp4', fps=fps)
202
203j = 1
204while True:
205ret, image = cap.read()
206
207if (type(image) == type(None)):
208writer.close()
209break
210
211image_h, image_w, _ = image.shape
212new_image = od.preprocess_input(image, od.net_h, od.net_w)
213
214# run the prediction
215yolos = od.yolov3.predict(new_image)
216boxes = []
217
218for i in range(len(yolos)):
219# decode the output of the network
220boxes += od.decode_netout(yolos[i][0], od.anchors[i],
221od.obj_thresh, od.nms_thresh, od.net_h, od.net_w)
222
223# correct the sizes of the bounding boxes
224od.correct_yolo_boxes(boxes, image_h, image_w, od.net_h, od.net_w)
225
226# suppress non-maximal boxes
227od.do_nms(boxes, od.nms_thresh)
228
229# draw bounding boxes on the image using labels
230image2 = od.draw_boxes(
231image, boxes, self.line, od.labels, od.obj_thresh, j)
232
233writer.append_data(image2)
234
235# cv2.imwrite('E:/Virtual Traffic Light Violation Detection System/Images/frame'+str(j)+'.jpg', image2)
236# self.show_image('E:/Virtual Traffic Light Violation Detection System/Images/frame'+str(j)+'.jpg')
237
238cv2.imshow('Traffic Violation', image2)
239
240print(j)
241
242if cv2.waitKey(10) & 0xFF == ord('q'):
243writer.close()
244break
245
246j = j+1
247
248cv2.destroyAllWindows()
249
250
251root = Tk()
252app = Window(root)
253root.geometry("%dx%d" % (535, 380))
254root.title("Traffic Violation")
255
256root.mainloop()
257