-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraCV2
171 lines (133 loc) · 5.43 KB
/
CameraCV2
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
import numpy as np
import cv2
from tkinter import *
from PIL import Image, ImageTk
from drive import go_forward, go_backwardward, go_left, go_right, camera_down, camera_up, go_stop, camera_left, camera_right
forward_pressed = False
reverse_pressed = False
left_pressed = False
right_pressed = False
def detect_corrosion(frame):
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
corr_lower = np.array([0, 70, 70], np.uint8)
corr_upper = np.array([20, 200, 150], np.uint8)
corr_mask = cv2.inRange(hsv_frame, corr_lower, corr_upper)
kernel = np.ones((5, 5), np.uint8)
corr_mask = cv2.dilate(corr_mask, kernel)
contours, _ = cv2.findContours(corr_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
area = cv2.contourArea(contour)
if area > 300:
x, y, w, h = cv2.boundingRect(contour)
frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, "Corrosion", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
return frame
def live_detection():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
frame = detect_corrosion(frame)
if forward_pressed:
go_forward()
elif reverse_pressed:
go_backwardward()
elif left_pressed:
go_left()
elif right_pressed:
go_right()
else:
go_stop()
frame = cv2.resize(frame, (400, 400))
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
imgtk = ImageTk.PhotoImage(image=img)
x.imgtk = imgtk
x.configure(image=imgtk)
x.update()
def forward():
global forward_pressed
forward_pressed = not forward_pressed
if forward_pressed:
print("Move forward:", forward_pressed)
def reverse():
global reverse_pressed
reverse_pressed = not reverse_pressed
if reverse_pressed:
print("Move reverse:", reverse_pressed)
def left():
global left_pressed
left_pressed = not left_pressed
if left_pressed:
print("Move left:", left_pressed)
def right():
global right_pressed
right_pressed = not right_pressed
if right_pressed:
print("Move right:", right_pressed)
def cam_up():
camera_up()
def cam_down():
camera_down()
def cam_left():
camera_left()
def cam_right():
camera_right()
def on_forward_press():
forward()
forward_button.config(relief=SUNKEN)
def on_forward_release():
forward_button.config(relief=RAISED)
def on_reverse_press():
reverse()
reverse_button.config(relief=SUNKEN)
def on_reverse_release():
reverse_button.config(relief=RAISED)
def on_left_press():
left()
left_button.config(relief=SUNKEN)
def on_left_release():
left_button.config(relief=RAISED)
def on_right_press():
right()
right_button.config(relief=SUNKEN)
def on_right_release():
right_button.config(relief=RAISED)
root = Tk()
root.geometry("1020x700")
root.title("Live Streaming")
root.resizable(0, 0)
im = None
MF = Frame(root, bd=8, bg="lightblue", relief=GROOVE)
MF.place(x=0, y=0, height=50, width=1020)
menu_label = Label(MF, text="Live Streaming", font=("times new roman", 20, "bold"), bg="lightgray", fg="black", pady=0)
menu_label.pack(side=TOP, fill="x")
x = Label(root, image=im)
x.grid(row=1, column=0, padx=5, pady=50)
# Create buttons for control
forward_button = Button(root, text="Forward", font=("times new roman", 20, "bold"), command=on_forward_press)
forward_button.grid(row=2, column=1, padx=5, pady=5)
forward_button.bind("<ButtonPress>", lambda event: on_forward_press())
forward_button.bind("<ButtonRelease>", lambda event: on_forward_release())
reverse_button = Button(root, text="Reverse", font=("times new roman", 20, "bold"), command=on_reverse_press)
reverse_button.grid(row=2, column=2, padx=5, pady=5)
reverse_button.bind("<ButtonPress>", lambda event: on_reverse_press())
reverse_button.bind("<ButtonRelease>", lambda event: on_reverse_release())
left_button = Button(root, text="Left", font=("times new roman", 20, "bold"), command=on_left_press)
left_button.grid(row=3, column=1, padx=5, pady=5)
left_button.bind("<ButtonPress>", lambda event: on_left_press())
left_button.bind("<ButtonRelease>", lambda event: on_left_release())
right_button = Button(root, text="Right", font=("times new roman", 20, "bold"), command=on_right_press)
right_button.grid(row=3, column=2, padx=5, pady=5)
right_button.bind("<ButtonPress>", lambda event: on_right_press())
right_button.bind("<ButtonRelease>", lambda event: on_right_release())
camup_button = Button(root, text="Cam_up", font=("times new roman", 20, "bold"), command=cam_up)
camup_button.grid(row=2, column=3, padx=5, pady=5)
camdown_button = Button(root, text="Cam_down", font=("times new roman", 20, "bold"), command=cam_down)
camdown_button.grid(row=3, column=3, padx=5, pady=5)
camera_left_button = Button(root, text="Cam_left", font=("times new roman", 20, "bold"), command=cam_left)
camera_left_button.grid(row=2, column=4, padx=5, pady=5)
camera_right_button = Button(root, text="Cam_right", font=("times new roman", 20, "bold"), command=cam_right)
camera_right_button.grid(row=3, column=4, padx=5, pady=5)
live_detection()
root.mainloop()