forked from adibyju/Human-following_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_control.py
290 lines (216 loc) · 7.85 KB
/
bot_control.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
import numpy as np
import cv2 as cv
import imutils
import time
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import argparse
import subprocess
import os
x2=1
y2=1
a=-1.0
FLAGS = []
count = 0
w=0.0
boxes = []
confidences = []
classids = []
idxs = ()
def draw_labels_and_boxes(img, boxes, confidences, classids, idxs, colors, labels):
# If there are any detections
if len(idxs) > 0:
for i in idxs.flatten():
# Get the bounding box coordinates
global w
x, y = boxes[i][0], boxes[i][1]
w, h = boxes[i][2], boxes[i][3]
# Get the unique color for this class
color = [int(c) for c in colors[classids[i]]]
# Draw the bounding box rectangle and label on the image
cv.rectangle(img, (x, y), (x+w, y+h), color, 2)
text = "{}: {:4f}".format(labels[classids[i]], confidences[i])
cv.putText(img, text, (x, y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
global x2,y2
x2 = x + int(w / 2)
y2 = y + int(h / 2)
cv.circle(img, (x2, y2), 4, (0, 255, 0), -1)
text = "x: " + str(x2) + ", y: " + str(y2)
cv.putText(img, text, (x2 - 10, y2 - 10),
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return img
def generate_boxes_confidences_classids(outs, height, width, tconf):
boxes = []
confidences = []
classids = []
for out in outs:
for detection in out:
#print (detection)
#a = input('GO!')
# Get the scores, classid, and the confidence of the prediction
scores = detection[5:]
classid = np.argmax(scores)
confidence = scores[classid]
# Consider only the predictions that are above a certain confidence level
if confidence > tconf:
# TODO Check detection
box = detection[0:4] * np.array([width, height, width, height])
centerX, centerY, bwidth, bheight = box.astype('int')
# Using the center x, y coordinates to derive the top
# and the left corner of the bounding box
x = int(centerX - (bwidth / 2))
y = int(centerY - (bheight / 2))
# Append to list
boxes.append([x, y, int(bwidth), int(bheight)])
confidences.append(float(confidence))
classids.append(classid)
return boxes, confidences, classids
def infer_image(net, layer_names, height, width, img, colors, labels, FLAGS,
boxes=None, confidences=None, classids=None, idxs=None, infer=True):
if infer:
# Contructing a blob from the input image
blob = cv.dnn.blobFromImage(img, 1 / 255.0, (416, 416),
swapRB=True, crop=False)
# Perform a forward pass of the YOLO object detector
net.setInput(blob)
# Getting the outputs from the output layers
start = time.time()
outs = net.forward(layer_names)
end = time.time()
if FLAGS.show_time:
print ("[INFO] YOLOv3 took {:6f} seconds".format(end - start))
# Generate the boxes, confidences, and classIDs
boxes, confidences, classids = generate_boxes_confidences_classids(outs, height, width, FLAGS.confidence)
# Apply Non-Maxima Suppression to suppress overlapping bounding boxes
idxs = cv.dnn.NMSBoxes(boxes, confidences, FLAGS.confidence, FLAGS.threshold)
if boxes is None or confidences is None or idxs is None or classids is None:
raise '[ERROR] Required variables are set to None before drawing boxes on images.'
# Draw labels and boxes on the image
img = draw_labels_and_boxes(img, boxes, confidences, classids, idxs, colors, labels)
return img, boxes, confidences, classids, idxs
def drive_robot(lin,ang):
pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
move = Twist()
move.linear.x = lin
move.angular.z = ang
pub.publish(move)
def subscriber():
rospy.Subscriber('/camera/rgb/image_raw',Image,entecallbackfunction)
rospy.spin()
cv.destroyAllWindows()
def entecallbackfunction(entemsg):
try:
print("Received image")
bridge = CvBridge()
cap1 = bridge.imgmsg_to_cv2(entemsg, desired_encoding='passthrough')
# grab the current frame
frame = cap1
frame = imutils.resize(frame, width=800)
global count
height, width = frame.shape[:2]
global x2,y2
y2 = -1
global w
w = 0.0
global boxes, confidences, classids, idxs
if count == 0:
frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
height, width, frame, colors, labels, FLAGS)
count += 1
else:
frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
count = (count + 1) % 6
cv.imshow('Frame', frame)
cv.waitKey(1)
#forward_dist = rospy.wait_for_message('/scan', LaserScan, timeout=None).ranges[0]
global a
if 0 < y2 < 60:
drive_robot(-0.8,0.0)
else:
if w > 77.0:
drive_robot(-0.8,0.0)
elif w > 70.0:
drive_robot(0.0,0.0)
elif w == 0.0:
drive_robot(0.0,a)
else:
if x2 > 420:
drive_robot(0.8,-0.4)
a=-1.0
elif x2 < 380:
drive_robot(0.8,0.4)
a=1.0
else:
drive_robot(0.8,0.0)
except CvBridgeError as e:
print(e)
if __name__=='__main__':
rospy.init_node("bot_control", anonymous=True)
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model-path',
type=str,
default='./yolov3-coco/',
help='The directory where the model weights and \
configuration files are.')
parser.add_argument('-w', '--weights',
type=str,
default='./yolov3-coco/yolov3.weights',
help='Path to the file which contains the weights \
for YOLOv3.')
parser.add_argument('-cfg', '--config',
type=str,
default='./yolov3-coco/yolov3.cfg',
help='Path to the configuration file for the YOLOv3 model.')
parser.add_argument('-i', '--image-path',
type=str,
help='The path to the image file')
parser.add_argument('-v', '--video-path',
type=str,
help='The path to the video file')
parser.add_argument('-vo', '--video-output-path',
type=str,
default='./output.avi',
help='The path of the output video file')
parser.add_argument('-l', '--labels',
type=str,
default='./yolov3-coco/coco-labels',
help='Path to the file having the \
labels in a new-line seperated way.')
parser.add_argument('-c', '--confidence',
type=float,
default=0.5,
help='The model will reject boundaries which has a \
probabiity less than the confidence value. \
default: 0.5')
parser.add_argument('-th', '--threshold',
type=float,
default=0.3,
help='The threshold to use when applying the \
Non-Max Suppresion')
parser.add_argument('--download-model',
type=bool,
default=False,
help='Set to True, if the model weights and configurations \
are not present on your local machine.')
parser.add_argument('-t', '--show-time',
type=bool,
default=False,
help='Show the time taken to infer each image.')
FLAGS, unparsed = parser.parse_known_args()
# Download the YOLOv3 models if needed
if FLAGS.download_model:
subprocess.call(['./yolov3-coco/get_model.sh'])
# Get the labels
labels = open(FLAGS.labels).read().strip().split('\n')
# Intializing colors to represent each label uniquely
colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')
# Load the weights and configutation to form the pretrained YOLOv3 model
net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)
# Get the output layer names of the model
layer_names = net.getLayerNames()
layer_names = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
subscriber()