-
Notifications
You must be signed in to change notification settings - Fork 2
/
robotControlWithCamera.py
170 lines (125 loc) · 3.54 KB
/
robotControlWithCamera.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
import cv2
from tkinter import *
from PIL import Image, ImageTk
import RPi.GPIO as pin
from motor import littleForward, littleBackward, littleLeft, littleRight
pin.setwarnings(False)
msg =''
#width, height = 800, 500 #setting widht and height
cap = cv2.VideoCapture(0)
# image = cv2.rotate(src, cv2.ROTATE_180)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 600)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 350)
root = Tk()
root.title("RPI Robot Control Panel")
#main label for showing the feed
imagel = Label(root)
imagel.pack()
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (30, 20)
# fontScale
fontScale = 0.7
# Blue color in BGR
color = (255, 255, 255)
# Line thickness of 2 px
thickness = 2
#load background and button images
for_img = 'image/f.gif'
left_img = 'image/l.gif'
right_img = 'image/r.gif'
back_img = 'image/b.gif'
quit_img = 'image/close.gif'
msg = ''
def forward():
"""forward motion"""
print("Going Forward.")
global msg
msg = 'Going Forward'
littleForward()
return
def backward():
"""backward motion"""
print("Going back! Watch OUT!!")
global msg
msg = 'Going BACKWARD'
littleBackward()
return
def left():
"""go left"""
print("Going left.")
global msg
msg = 'Going LEFT'
littleLeft()
return
def right():
"""go right"""
global msg
msg = 'Going RIGHT'
print("Going right.")
littleRight()
return
def msg_default():
global msg
msg = ''
def check_faces(f):
faces = face_cascade.detectMultiScale(f, scaleFactor = 1.5, minNeighbors = 5)
for(x, y, w, h) in faces:
print('Face found\n')
roi_f = f[y: y+h, x: x+w]
color = (255, 0, 0)
stroke = 2
end_cord_x = x+w
end_cord_y = y+h
cv2.rectangle(f, (x,y), (end_cord_x, end_cord_y), color, stroke)
return f
def get_frame():
print("chhobi lagbo vai.")
ret, frame = cap.read()
return frame
def update():
"""update frames."""
global msg
print("dak porse vai")
frame = get_frame()
cv2.putText(frame, msg, org, font, fontScale, color, thickness, cv2.LINE_AA)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
try:
cv2.image = check_faces(frame)
except:
pass
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
imagel.imgtk = imgtk
imagel.configure(image=imgtk)
msg_default()
imagel.after(15, update)
#read the image for tk
im_f = PhotoImage(file= for_img)
im_l = PhotoImage(file= left_img)
im_r = PhotoImage(file= right_img)
im_b = PhotoImage(file= back_img)
im_quit = PhotoImage(file=quit_img)
#buttons
for_but = Button(root,text="<< Left",repeatdelay=15,repeatinterval=10, command=forward)
for_but.config(image=im_f, fg='gray', border=0, borderwidth=0, bg='black')
for_but.place(x=540, y=250)
left_but = Button(root,repeatdelay=15,repeatinterval=10, command=left)
left_but.config(image=im_l,border=0,borderwidth=0, bg='black' )
left_but.place(x=500, y=300)
right_but = Button(root,repeatdelay=15,repeatinterval=10, command=right)
right_but.config(image=im_r,border=0,borderwidth=0, bg='black')
right_but.place(x=580, y=300)
back_but = Button(root, repeatdelay=15, repeatinterval=10, command=backward)
back_but.config(image=im_b, border=0,borderwidth=0, bg='black')
back_but.place(x=540, y = 350)
quit_but = Button(root, text='Quit', command=root.destroy)
quit_but.config(image = im_quit, bg='red')
quit_but.place(x=0, y=0)
msg = ''
msg_default()
update()
root.resizable(0, 0)
root.mainloop()
pin.cleanup()