-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROV_GUI_cv2.py
195 lines (131 loc) · 5.41 KB
/
ROV_GUI_cv2.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
from asyncio.windows_events import NULL
from distutils.cmd import Command
from doctest import master
import multiprocessing
from pickle import TRUE
import queue
from tkinter import Button, Tk, Label, X, Frame, Y, LEFT, BOTH
import cv2
from tkinter import *
from PIL import Image, ImageTk
import threading
from collections import deque
import random
import os
root = Tk(master)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
big_Screen = deque()
root.geometry("%dx%d" % (width, height))
root.title("ROV")
# Initialize frames
root.attributes('-fullscreen', True)
camera_queue = deque()
def camera_reader(source, camera_queue):
print("Cam Loading...")
cap = cv2.VideoCapture(source)
print("Cam Loaded...")
while(True):
ret, frame = cap.read()
if frame is not None:
camera_queue.append([frame,source])
else:
cap = cv2.VideoCapture(source)
camera_read = threading.Thread(target=camera_reader, args=(0, camera_queue,))
camera_read.daemon = True
camera_read.start()
camera_read = threading.Thread(target=camera_reader, args=(1, camera_queue,))
camera_read.daemon = True
camera_read.start()
camera_read = threading.Thread(target=camera_reader, args=(2, camera_queue,))
camera_read.daemon = True
camera_read.start()
def close():
root.destroy()
def rebuild(new_frame):
new_frame.destroy()
build_view()
def touch_1(width,height,index,f1,f2):
f1.pack_forget()
f2.pack_forget()
dim = (width,height)
New_frame = Frame(root, bg="pink")
frame1 =Frame(root)
label =Label(frame1, text="Red", bg="black", fg="white",height=3)
buttom_exit = Button(label,text = "Back",bg="red",fg="white",font=3,relief="sunken",width=10,command=lambda: build_view(1,New_frame,frame1))
frame1.pack(fill=X)
New_frame.pack(fill=BOTH, expand=True)
w4 = Label(New_frame, text="", bg="white", fg="black",relief="sunken")
label.pack(fill=X)
buttom_exit.pack(side="right")
w4.pack(side=LEFT, fill=BOTH, expand=True)
while True:
if len(camera_queue)!= 0:
frame ,source = camera_queue.popleft()
if source == index:
cv2image= cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
Screen_thread =threading.Thread(target=Resize, args=(cv2image,dim))
Screen_thread.daemon = True
Screen_thread.start()
Screen_thread.join()
img1 = big_Screen.popleft()
img1 = Image.fromarray(img1)
imgtk = ImageTk.PhotoImage(image = img1)
w4.imgtk = imgtk
w4.configure(image=imgtk)
root.update_idletasks()
root.update()
def Resize(img,dim):
cv2image =cv2.resize(img, dim,fx = 2, fy = 2, interpolation = cv2.INTER_AREA)
big_Screen.append(cv2image)
def build_view(i,frame1,frame2):
if i == 1:
frame1.pack_forget()
frame2.pack_forget()
f1 = Frame(root)
f2 = Frame(root)
img_arr=[]
for image in os.listdir('photos'):
imag = os.path.join('photos',image)
img_arr.append(imag)
photos=random.sample(img_arr,2)
photo1=photos[0]
photo2=photos[1]
img_right=Image.open(photo2)
img_left=Image.open(photo1)
img_right=img_right.resize((width//4,height//2))
img_left=img_left.resize((width//4,height//2))
img_right=ImageTk.PhotoImage(img_right)
img_left=ImageTk.PhotoImage(img_left)
photo_left=Label(f1,bg="white", image=img_left)
photo_right=Label(f1,bg="white", image=img_right)
labels = (Label(f1, text="Red", bg="black", fg="white",height=3),
Button(f1, text="", bg="white", fg="black",relief="sunken",command=lambda:touch_1(width,height,0,f1,f2))
,Button(f2, text="", bg="white", fg="black",relief="sunken",command=lambda:touch_1(width,height,1,f1,f2))
, Button(f2, text="", bg="white", fg="black",relief="sunken",command=lambda:touch_1(width,height,2,f1,f2) )
)
buttom_exit = Button(labels[0],text = "X",bg="red",fg="white",font=3,relief="sunken",width=10,command=close)
f1.pack(fill=X)
f2.pack(fill=BOTH, expand=True)
labels[0].pack(fill=X)
photo_left.pack(side=LEFT, fill=BOTH, expand=True)
photo_right.pack(side=RIGHT, fill=BOTH, expand=True)
labels[1].pack(fill=X)
buttom_exit.pack(side="right")
labels[2].pack(side=LEFT, fill=BOTH, expand=True)
labels[3].pack(side=LEFT, fill=BOTH, expand=True)
dim1 = (width//2,height//2)
dim = [(width//2,height//2),(width//2,height//2),(width//2,height//2)]
while True:
if len(camera_queue)!=0 :
frame ,source = camera_queue.popleft()
cv2image= cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
img1 = cv2.resize(cv2image,dim[source+1],fx=1,fy=1, interpolation = cv2.INTER_AREA)
img1 = Image.fromarray(img1)
#img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
imgtk = ImageTk.PhotoImage(image = img1)
labels[source+1].imgtk = imgtk
labels[source+1].configure(image=imgtk)
root.update_idletasks()
root.update()
build_view(0,NULL,NULL)