Beispiel #1
0
    def _init(self, args):
        # -------------------- init_args -------------------- #
        self.trt = args.trt
        self.dev = args.dev
        self.topic = args.topic
        self.show = args.show
        self.radar = args.radar
        self.flip = args.flip
        self.clip = (args.clip_h, args.clip_w)
        self.ctx = yolo_gluon.get_ctx(args.gpu)

        # -------------------- init_ros -------------------- #
        rospy.init_node("YOLO_ros_node", anonymous=True)
        self.bridge = CvBridge()
        self.img_pub = rospy.Publisher(self.yolo.pub_img, Image, queue_size=1)
        self.car_pub = rospy.Publisher(self.yolo.pub_box,
                                       Float32MultiArray,
                                       queue_size=1)
        self.depth_image = None
        self.image = None
        self.mat_car = Float32MultiArray()

        # -------------------- init_dev -------------------- #
        if self.dev == 'ros':
            rospy.Subscriber(DEPTH_TOPIC, Image, self._depth_callback)
            rospy.Subscriber(self.topic, Image, self._image_callback)
            print(global_variable.green)
            print('Image Topic: %s' % self.topic)
            print('Depth Topic: %s' % DEPTH_TOPIC)
            print(global_variable.reset_color)

        else:
            #pass
            threading.Thread(target=self._get_frame).start()

        # -------------------- init_radar -------------------- #
        if self.radar:
            self.radar_prob = yolo_cv.RadarProb(self.yolo.num_class,
                                                self.yolo.classes)

        # -------------------- init_video_saver -------------------- #
        if save_video_size is not None and len(save_video_size) == 2:
            self.save_video = True
            start_time = datetime.datetime.now().strftime("%m-%dx%H-%M")
            out_file = os.path.join('video', 'car_%s.avi' % start_time)

            fourcc = cv2.VideoWriter_fourcc(*'MJPG')  # (*'MJPG')#(*'MPEG')
            self.out = cv2.VideoWriter(out_file, fourcc, 30, (save_video_size))
        else:
            self.save_video = False
Beispiel #2
0
    def valid(self):
        print(global_variable.cyan)
        print('Valid')

        bs = 1  # batch size = 1
        h, w = self.size

        # init two matplotlib figures
        ax1 = yolo_cv.init_matplotlib_figure()
        ax2 = yolo_cv.init_matplotlib_figure()

        # init radar figure for vizualizing class distribution
        radar_prob = yolo_cv.RadarProb(self.num_class, self.classes)

        # init background, LP adder, car renderer
        BG_iter = yolo_gluon.load_background('val', bs, h, w)
        LP_generator = licence_plate_render.LPGenerator(h, w)
        car_renderer = RenderCar(h, w, self.classes, self.ctx[0], pre_load=False)

        for bg in BG_iter:
            # select background
            bg = bg.data[0].as_in_context(self.ctx[0])  # b*RGB*w*h
            # render images, type(imgs) = mxnet.ndarray
            imgs, labels = car_renderer.render(bg, 'valid', pascal_rate=0.5, render_rate=0.9)
            imgs, LP_labels = LP_generator.add(imgs, self.LP_r_max, add_rate=0.8)

            # return all_output[::-1], [LP_output]
            x1, x2, x3, LP_x = self.net.forward(is_train=False, data=imgs)
            outs = self.predict([x1, x2, x3])
            LP_outs = self.predict_LP([LP_x])

            # convert ndarray to np.array
            img = yolo_gluon.batch_ndimg_2_cv2img(imgs)[0]

            # draw licence plate border
            img, clipped_LP = LP_generator.project_rect_6d.add_edges(img, LP_outs[0, 1:])
            yolo_cv.matplotlib_show_img(ax2, clipped_LP)

            # draw car border
            img = yolo_cv.cv2_add_bbox(img, labels[0, 0].asnumpy(), 4, use_r=0)
            img = yolo_cv.cv2_add_bbox(img, outs[0], 5, use_r=0)
            yolo_cv.matplotlib_show_img(ax1, img)

            # vizualize class distribution
            radar_prob.plot(outs[0, 0], outs[0, -self.num_class:])
            raw_input('next')
Beispiel #3
0
    def valid(self):
        print(global_variable.cyan)
        print('Valid')

        bs = 1
        h, w = self.size
        ax1 = yolo_cv.init_matplotlib_figure()
        radar_prob = yolo_cv.RadarProb(self.num_class, self.classes)

        BG_iter = yolo_gluon.load_background('val', bs, h, w)
        car_renderer = RenderCar(h,
                                 w,
                                 self.classes,
                                 self.ctx[0],
                                 pre_load=False)

        for bg in BG_iter:
            # -------------------- get image -------------------- #
            bg = bg.data[0].as_in_context(self.ctx[0])  # b*RGB*w*h
            imgs, labels = car_renderer.render(bg,
                                               'valid',
                                               pascal_rate=0.5,
                                               render_rate=0.9)

            # -------------------- predict -------------------- #
            net_out = self.net.forward(is_train=False, data=imgs)
            # net_out = [x1, x2, x3], which shapes are
            # (1L, 640L, 3L, 30L), (1L, 160L, 3L, 30L), (1L, 40L, 3L, 30L)
            outs = self.predict(net_out)

            # -------------------- show -------------------- #
            img = yolo_gluon.batch_ndimg_2_cv2img(imgs)[0]

            img = yolo_cv.cv2_add_bbox(img, labels[0, 0].asnumpy(), 4, use_r=0)
            img = yolo_cv.cv2_add_bbox(img, outs[0], 5, use_r=0)
            yolo_cv.matplotlib_show_img(ax1, img)

            radar_prob.plot3d(outs[0, 0], outs[0, -self.num_class:])
            raw_input('next')
Beispiel #4
0
    def valid_Nima(self):
        '''
        compare with "Unsupervised Generation of a Viewpoint"
        Annotated Car Dataset from Videos
        -path: /media/nolan/SSD1/YOLO_backup/freiburg_static_cars_52_v1.1
          |-annotations (d)
            |-txt_path (d)
          |-all car folders (d)
          |-result_path (v)
            |-all car folders with box (v)
              |-img_path (v)
            |-annotations (v)
              |-save_txt_path (v)
              |-plot (p)
                |-all car azi compare (p)

        d: download from "Unsupervised Generation of a Viewpoint"
        v: generate from valid_Nima
        o: generate from valid_Nima_plot
        '''
        import cv2

        image_w = 960.
        image_h = 540.

        mx_resize = mxnet.image.ForceResizeAug(tuple(self.size[::-1]))
        radar_prob = yolo_cv.RadarProb(self.num_class, self.classes)

        x = np.arange(53)
        x = np.delete(x, [0, 6, 20, 23, 31, 36])
        for i in x:
            result_path = os.path.join(freiburg_path,
                                       "result_%s" % self.version)
            save_img_path = os.path.join(result_path, "car_%d" % i)
            if not os.path.exists(save_img_path):
                os.makedirs(save_img_path)

            save_txt_path = os.path.join(result_path, 'annotations')
            if not os.path.exists(save_txt_path):
                os.makedirs(save_txt_path)

            txt_path = os.path.join(freiburg_path, "annotations",
                                    "%d_annot.txt" % i)
            with open(txt_path, 'r') as f:
                all_lines = f.readlines()

            for line_i, line in enumerate(all_lines):
                img_name = line.split('\t')[0].split('.')[0] + '.png'
                img_path = os.path.join(freiburg_path, img_name)
                save_img = os.path.join(save_img_path, img_name.split('/')[-1])
                img = cv2.imread(img_path)

                # -------------------- Prediction -------------------- #
                nd_img = yolo_gluon.cv_img_2_ndarray(img,
                                                     self.ctx[0],
                                                     mxnet_resize=mx_resize)
                net_out = self.net.forward(is_train=False, data=nd_img)
                pred_car = self.predict(net_out[:3])
                pred_car[0, 5] = -1  # (1, 80)
                Cout = pred_car[0]

                left_ = (Cout[2] - 0.5 * Cout[4]) * image_w
                up_ = (Cout[1] - 0.5 * Cout[3]) * image_h
                right_ = (Cout[2] + 0.5 * Cout[4]) * image_w
                down_ = (Cout[1] + 0.5 * Cout[3]) * image_h

                vec_ang, vec_rad, prob = radar_prob.cls2ang(
                    Cout[0], Cout[-self.num_class:])

                # -------------------- Ground Truth -------------------- #
                box_para = np.fromstring(line.split('\t')[1],
                                         dtype='float32',
                                         sep=' ')
                left, up, right, down = box_para
                h = (down - up) / image_h
                w = (right - left) / image_w
                y = (down + up) / 2 / image_h
                x = (right + left) / 2 / image_w
                boxA = [0, y, x, h, w, 0]

                azi_label = int(line.split('\t')[2]) - 90
                azi_label = azi_label - 360 if azi_label > 180 else azi_label

                inter = (min(right, right_) -
                         max(left, left_)) * (min(down, down_) - max(up, up_))
                a1 = (right - left) * (down - up)
                a2 = (right_ - left_) * (down_ - up_)
                iou = (inter) / (a1 + a2 - inter)

                save_txt = os.path.join(save_txt_path, '%d_annot' % i)
                with open(save_txt, 'a') as f:
                    f.write('%s %f %f %f %f\n' %
                            (img_name, iou, azi_label, vec_ang * 180 / math.pi,
                             vec_rad))

                yolo_cv.cv2_add_bbox(img, Cout, 3, use_r=False)
                yolo_cv.cv2_add_bbox(img, boxA, 4, use_r=False)

                cv2.imshow('img', img)
                cv2.waitKey(1)
                cv2.imwrite(save_img, img)