def detect(): """ Performs object detection and publishes coordinates. """ #global image and depth frames from ZED camera global image global points_list points_list = [[0, 0, 0]] * 921600 image = np.zeros((560, 1000, 3), np.uint8) # Initialize detector send_message(Color.GREEN, "[INFO] Initializing TinyYOLOv3 detector.") det = Detector( "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3.cfg", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3_68000.weights", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/obj.names") (H, W) = (None, None) # Load model send_message(Color.GREEN, "[INFO] Loading network model.") net = det.load_model() # Initilialize Video Stream send_message(Color.GREEN, "[INFO] Starting video stream.") counter = 0 dets = 0 nondets = 0 detect = True fps = FPS().start() boxes, confidences, indices, cls_ids, colors, ids, distances = [], [], [], [], [], [], [] ret = True while not rospy.is_shutdown(): # Grab next frame #ret, frame = video.read() frame = image color = "" diststring = "" ##AQUI SE MODIFICA EL VIDEO #frame = add_brightness(frame) #frame = add_darkness(frame) if cv2.waitKey(1) & 0xFF == ord('q'): send_message(Color.RED, "[DONE] Quitting program.") break print(frame.shape) frame = imutils.resize(frame, width=1000) (H, W) = frame.shape[:2] if det.get_w() is None or det.get_h() is None: det.set_h(H) det.set_w(W) # Perform detection detect = True dets += 1 # Get bounding boxes, condifences, indices and class IDs boxes, confidences, indices, cls_ids = det.get_detections(net, frame) # Publish detections det_str = "Det: {}, BBoxes {}, Colors {}, Distance {}".format( dets, boxes, colors, distances) send_message(Color.BLUE, det_str) # If there were any previous detections, draw them colors = [] distances = [] obj_list = ObjDetectedList() len_list = 0 for ix in indices: i = ix[0] box = boxes[i] x, y, w, h = box x, y, w, h = int(x), int(y), int(w), int(h) if detect == True: color = calculate_color(frame, x, y, h, w) p1 = int((x + w / 2) * 1.28) p2 = int((y + h / 2) * 1.28) dist = points_list[(p1 + p2 * 1280)][0] if (dist < .30): diststring = "OUT OF RANGE" else: diststring = str(dist) + " m" color = str(color.color) colors.append(color) distances.append(dist) obj = ObjDetected() print(p1, p2) obj.x = x obj.y = y obj.h = h obj.w = w obj.X = points_list[(p1 + p2 * 1280)][0] obj.Y = points_list[(p1 + p2 * 1280)][1] obj.color = color obj.clase = 'bouy' if cls_ids[i] == 0 else 'marker' len_list += 1 obj_list.objects.append(obj) det.draw_prediction(frame, cls_ids[i], confidences[i], color, diststring, x, y, x + w, y + h) fps.update() obj_list.len = len_list detector_pub.publish(obj_list) cv2.line(frame, (500, 560), (500, 0), (255, 0, 0)) fps.stop() info = [ ("Detects: ", dets), ("No detects: ", nondets), ("FPS", "{:.2F}".format(fps.fps())), ] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, det.get_h() - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # Show current frame #cv2.imshow("depth", depth_frame) cv2.imshow("Frame", frame) #while cv2.waitKey(0) and 0xFF == ord('q'): rate.sleep()
def detect(self): """ Performs object detection and publishes coordinates. """ # Initialize detector self.send_message(Color.GREEN, "[INFO] Initializing TinyYOLOv3 detector.") det = Detector("/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3.cfg", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/tiny3_68000.weights", "/home/nvidia/catkin_ws/src/boat/scripts/vantec-config/obj.names") (H, W) = (None, None) # Load model self.send_message(Color.GREEN, "[INFO] Loading network model.") net = det.load_model() print(net) # Initilialize Video Stream self.send_message(Color.GREEN, "[INFO] Starting video stream.") counter = 0 dets = 0 nondets = 0 detect = True fps = FPS().start() boxes, confidences, indices, cls_ids, colors, ids, distances = [], [], [], [], [], [], [] zed_cam_size = 1280 ret = True while not rospy.is_shutdown(): # Grab next frame #ret, frame = video.read() frame = self.image #cap = cv2.VideoCapture("/home/nvidia/opencv_install/pajarito/bird.jpg") #hasFrame, frame = cap.read() color = "" diststring = "" ##AQUI SE MODIFICA EL VIDEO #frame = add_brightness(frame) #frame = add_darkness(frame) if cv2.waitKey(1) & 0xFF == ord ('q'): self.send_message(Color.RED, "[DONE] Quitting program.") break frame = imutils.resize(frame, width=1000) (H, W) = frame.shape[:2] if det.get_w() is None or det.get_h() is None: det.set_h(H) det.set_w(W) # Perform detection detect = True dets += 1 # Get bounding boxes, condifences, indices and class IDs boxes, confidences, indices, cls_ids = det.get_detections(net, frame) # Publish detections # If there were any previous detections, draw them colors = [] distances = [] obj_list = ObjDetectedList() len_list = 0 for ix in indices: i = ix[0] box = boxes[i] x, y, w, h = box x, y, w, h = int(x), int(y), int(w), int(h) if detect == True: color = self.calculate_color(frame,x,y,h,w) depth = self.img_depth[y:y+h,x:x+w] d_list = [] for j in depth: if str(j[1]) != 'nan' and str(j[1]) != 'inf': d_list.append(j[1]) if len(d_list) != 0: dist = np.mean(d_list) else: dist = 'nan' if (dist < .30): diststring = "OUT OF RANGE" else: diststring = str(dist) + " m" color = str(color.color) colors.append(color) distances.append(dist) if str(dist) != 'nan': obj = ObjDetected() #print(p1,p2) obj.x = x obj.y = y obj.h = h obj.w = w obj.X = dist obj.Y = 0 obj.color = color obj.clase = 'bouy' if cls_ids[i] == 0 else 'marker' len_list += 1 obj_list.objects.append(obj) det.draw_prediction(frame, cls_ids[i], confidences[i], color,diststring, x, y, x+w, y+h) det_str = "Det: {}, BBoxes {}, Colors {}, Distance {}".format(dets, boxes, colors, distances) self.send_message(Color.BLUE, det_str) fps.update() obj_list.len = len_list self.detector_pub.publish(obj_list) cv2.line(frame, (500,560), (500,0), (255,0,0)) fps.stop() info = [ ("Detects: ", dets), ("No detects: ", nondets), ("FPS", "{:.2F}".format(fps.fps())), ] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, det.get_h() - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # Show current frame #cv2.imshow("Frame", frame) #print(self.depth) #cv2.waitKey(3) rate.sleep()
def detect(config, weights, classes, video): """ Performs object detection and publishes a data structure that contains, instance coordinates, instance class and sets a counter that will be used by the tracker. """ # Initialize detector send_message(Color.GREEN, "[INFO] Initializing TinyYOLOv3 detector.", "info") det = Detector(config, weights, classes) (H, W) = (None, None) # Initialize object tracks tracks = Tracks() # Initilialize Video Stream send_message(Color.GREEN, "[INFO] Starting video stream.", "info") if video == "0": video = cv2.VideoCapture(0) else: video = cv2.VideoCapture(args.video) counter, N = 0, 12 detect = True fps = FPS().start() while not rospy.is_shutdown() or video.isOpened(): # Grab next frame ret, frame = video.read() if not ret: send_message(Color.RED, "[DONE] Finished processing.", "info") cv2.waitKey(2000) break elif cv2.waitKey(1) & 0xFF == ord('q'): send_message(Color.RED, "[DONE] Quitting program.", "info") break frame = imutils.resize(frame, width=1000) (H, W) = frame.shape[:2] if det.get_width() is None or det.get_height() is None: det.set_height(H) det.set_width(W) # Modify frame brightness if necessary # frame = change_brightness(frame, 0.8) # Decrease # frame = change_brightness(frame, 1.5) # Increase # Every N frames perform detection if detect: # boxes, indices, cls_ids = [], [], [] boxes, indices, cls_ids = det.get_detections(det.net, frame) print(len(boxes)) objects = [] # Create objects and update tracks for i in range(len(cls_ids)): x, y, w, h = boxes[i] # Create object with attributes: class, color, bounding box obj = Object(cls_ids[i], get_color(frame, x, y, w, h), boxes[i], cv2.TrackerKCF_create()) obj.tracker.init(frame, (x, y, w, h)) objects.append(obj) tracks.update(objects) detect = False # While counter < N, update through KCF else: if counter == N: counter = 0 detect = True for o in tracks.objects: obj = tracks.get_object(o) obj.print_object() (succ, bbox) = obj.update_tracker(frame) if not succ: obj.reduce_lives() if obj.get_lives() == 0: tracks.delete_object(o) else: obj.update_object_bbox(bbox) obj.reset_lives() tracks.set_object(obj, o) # This is for debugging purposes. Just to check that bounding boxes are updating. if DEBUG: for o in tracks.objects: obj = tracks.get_object(o) x, y, w, h = obj.get_object_bbox() det.draw_prediction(frame, obj.get_class(), obj.get_color(), obj.get_id(), int(x), int(y), int(x + w), int(y + h)) # Publish detections counter += 1 det_str = "Detections {}: {}".format(counter, tracks.objects) send_message(Color.BLUE, det_str) fps.update() fps.stop() info = [("FPS", "{:.2F}".format(fps.fps())), ("OUT", "class, color, id")] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, det.get_height() - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # Show current frame cv2.imshow("Frame", frame) rate.sleep()