-
Notifications
You must be signed in to change notification settings - Fork 4
/
obj_det_YoloV4.py
173 lines (144 loc) · 5.96 KB
/
obj_det_YoloV4.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
import time
import rospy
import importlib
baxter=importlib.import_module("baxter-python3.baxter")
import cv2
import numpy as np
import argparse
PI = 3.141592
WIDTH = 960
HEIGHT = 600
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model', type=str, default='yolov4', help='Model desired')
args = parser.parse_args()
model_list = ["yolov4","yolov4-new", "yolov4x-mish"]
print("[INFO] loading model...")
if args.model == model_list[0]:
#Load net
modelConfig = "./models/yolov4.cfg"
modelWeigths = "./models/yolov4.weights"
net = cv2.dnn.readNetFromDarknet(modelConfig, modelWeigths)
print("Net Loaded: {}".format(args.model))
with open('./models/coco.names', 'r') as f:
classes = f.read().splitlines()
print("Classes: {}".format(len(classes)))
#suggested
conf_threshold = 0.1
nms_threshold = 0.6 #lower=stronger
elif args.model == model_list[1]:
#Load net
modelConfig = "./models/yolov4_new.cfg"
modelWeigths = "./models/yolov4_new.weights"
net = cv2.dnn.readNetFromDarknet(modelConfig, modelWeigths)
print("Net Loaded: {}".format(args.model))
with open('./models/coco.names', 'r') as f:
classes = f.read().splitlines()
print("Classes: {}".format(len(classes)))
#suggested
conf_threshold = 0.35
nms_threshold = 0.05 #lower=stronger
elif args.model == model_list[2]:
#Load net
modelConfig = "./models/yolov4x-mish.cfg"
modelWeigths = "./models/yolov4x-mish.weights"
net = cv2.dnn.readNetFromDarknet(modelConfig, modelWeigths)
print("Net Loaded: {}".format(args.model))
with open('./models/coco.names', 'r') as f:
classes = f.read().splitlines()
print("Classes: {}".format(len(classes)))
#suggested
conf_threshold = 0.3
nms_threshold = 0.01 #lower=stronger
else:
print("[Error] Model passed not present, choose between: {}".format(model_list))
exit()
np.random.seed(42) #to generate the same colors
colors = np.random.randint(0, 255, size=(len(classes), 3), dtype='uint8')
print("Colors generated: "+str(colors.shape[0]))
# function to get the output layer names
# in the architecture
def get_output_layers(net):
layer_names = net.getLayerNames()
output_layers = []
for i in net.getUnconnectedOutLayers():
i = i[0] #DEPENDING ON YOUR OPENCV VERSION delete this line and similars in the code (i it's not an array anymore)
output_layers.append(layer_names[i-1])
return output_layers
# function to draw bounding box on the detected object with class name
def draw_bounding_box(img_yolo, class_id, confidence, x, y, x_plus_w, y_plus_h):
label = str(classes[class_id])
# Preparing colour for current bounding box
color = [int(j) for j in colors[class_id]]
cv2.rectangle(img_yolo, (x,y), (x_plus_w,y_plus_h), color, 2)
text_box_current = '{}: {:.2f}'.format(label, confidence)
if y<5:(x,y)=(x+15, y+30) #label position not out of the image
cv2.putText(img_yolo, text_box_current, (x-6,y-6), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,0), 2)
cv2.putText(img_yolo, text_box_current, (x-5,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
print("[INFO] starting robot...")
rospy.init_node("testing")
rospy.sleep(2.0)
robot = baxter.BaxterRobot(rate=100, arm="left")
rospy.sleep(2.0)
robot._set_camera(camera_name="left_hand_camera", state=True, width=WIDTH, height=HEIGHT, fps=30)
robot.set_robot_state(True)
print("[INFO] moving in position...")
print(robot.move_to_neutral())
print(robot.move_to_zero())
print(robot.move_to_joint_position({"left_s0": -PI/4}, timeout=10))
data = np.array(list(robot._cam_image.data), dtype=np.uint8)
middle_point = np.array([WIDTH/2, HEIGHT/2])
print("[INFO] getting image stream and passing to DNN...")
while not rospy.is_shutdown():
img = np.array(list(robot._cam_image.data), dtype=np.uint8)
img = img.reshape(int(HEIGHT), int(WIDTH), 4)
img = img[:, :, :3].copy()
#Passing image to DNN
#gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# create input blob
blob = cv2.dnn.blobFromImage(img, 1/255, (416,416), (0,0,0), True, crop=False)
# set input blob for the network
net.setInput(blob)
# run inference through the network
# and gather predictions from output layers
start = time.time()
outs = net.forward(get_output_layers(net))
print('Prediction took {:.5f} seconds'.format(time.time() - start))
# initialization
class_ids = []
confidences = []
boxes = []
# for each detetion from each output layer
# get the confidence, class id, bounding box params
# and ignore weak detections (confidence < conf_threshold)
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
center_x = int(detection[0] * WIDTH)
center_y = int(detection[1] * HEIGHT)
w = int(detection[2] * WIDTH)
h = int(detection[3] * HEIGHT)
x = center_x - w / 2
y = center_y - h / 2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
#apply non-max suppression
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
print("Detections: "+str(indices.shape[0])) if len(indices)!=0 else print("No Detections")
# go through the detections remaining
# after nms and draw bounding box
for i in indices:
i = i[0] #DEPENDING ON YOUR OPENCV VERSION delete this line and similars in the code (i it's not an array anymore)
box = boxes[i]
x = box[0]
y = box[1]
w = box[2]
h = box[3]
draw_bounding_box(img, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))
robot._set_display_data(cv2.resize(img, (1024,600)))
robot.rate.sleep()
print(robot.move_to_neutral())
robot.set_robot_state(False)