示例#1
0
def main():

    trained_model = cfg.trained_model
    thresh = 0.5
    image_dir = '/home/cory/cedl/vid/videos/vid04'

    net = Darknet19()
    net_utils.load_net(trained_model, net)
    net.eval()
    net.cuda()
    print('load model successfully')
    print(net)

    image_extensions = ['.jpg', '.JPG', '.png', '.PNG']
    image_abs_paths = sorted([
        os.path.join(image_dir, name) for name in os.listdir(image_dir)
        if name[-4:] in image_extensions
    ])

    t_det = Timer()
    t_total = Timer()

    for i, image_path in enumerate(image_abs_paths):
        t_total.tic()
        image, im_data = preprocess(image_path)
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        t_det.tic()
        bbox_pred, iou_pred, prob_pred = net.forward(im_data)
        det_time = t_det.toc()
        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)

        im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds,
                                            cfg)

        if im2show.shape[0] > 1100:
            im2show = cv2.resize(im2show, (int(
                1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))
        cv2.imshow('test', im2show)

        total_time = t_total.toc()
        format_str = 'frame: %d, (detection: %.1f fps, %.1f ms) (total: %.1f fps, %.1f ms)'
        print(format_str % (i, 1. / det_time, det_time * 1000, 1. / total_time,
                            total_time * 1000))

        t_det.clear()
        t_total.clear()

        key = cv2.waitKey(1)
        if key == ord('q'):
            break
示例#2
0
    def process(self):
        while True:
            image, im_data = preprocess(self.camera)
            im_data = net_utils.np_to_variable(im_data,
                                               is_cuda=True,
                                               volatile=True).permute(
                                                   0, 3, 1, 2)
            bbox_pred, iou_pred, prob_pred = self.net(im_data)

            bbox_pred = bbox_pred.data.cpu().numpy()
            iou_pred = iou_pred.data.cpu().numpy()
            prob_pred = prob_pred.data.cpu().numpy()

            bboxes, scores, cls_inds = yolo_utils.postprocess(
                bbox_pred, iou_pred, prob_pred, image.shape, cfg, self.thresh)
            out = np.ones((1, 2)).astype('float32')

            for x in range(len(bboxes)):
                if cls_inds[x] == 14:
                    topleft = (bboxes[x][0], bboxes[x][1])
                    bottomright = (bboxes[x][2], bboxes[x][3])
                    conf = scores[x]
                    detect = True

                    diff = self._difference(topleft, bottomright)
                    area = self._area(topleft, bottomright)

                    out[0][0] = diff
                    out[0][0] /= self.center[0]
                    out[0][1] = area
                    out[0][1] /= self.max_area

                    return out
示例#3
0
def test_net(net, dataloader, max_per_image=300, thresh=0.5, vis=False):
    num_images = len(dataloader.dataset)

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)] for _ in range(1)]

    det_file = os.path.join(output_dir, 'detections.pkl')
    size_index = args.image_size_index

    for i, batch in enumerate(dataloader):
        orig_image = np.array(tr.to_pil_image(batch['image'][0]))
        image = batch['image']
        truth_boxes = batch['bounding_boxes'][0]
        image = Variable(image)
        if torch.cuda.is_available():
            image = image.cuda()

        bbox_pred, iou_pred, prob_pred = net(image)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, orig_image.shape, cfg, thresh, 0)

        orig_image = Image.fromarray(orig_image)
        draw = ImageDraw.Draw(orig_image)
        for box in truth_boxes:
            draw.rectangle([(box[0], box[1]), (box[2], box[3])],
                           outline='blue')
        orig_image.save('outputs/' + str(random.randint(0, 10000)) + '.jpg')
示例#4
0
def test_net(net, dataset, conn, max_per_image=300, thresh=0.5, vis=False):
    loader = GenLoader(dataset)
    _t = {'im_detect': Timer(), 'misc': Timer()}
    for inputs, originals, meta in loader:
        im_data = Variable(inputs.cuda(), volatile=True)

        _t['im_detect'].tic()
        bbox_pred, iou_pred, prob_pred = net(im_data)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        for i in range(len(bbox_pred)):
            bboxes, scores, cls_inds = yolo_utils.postprocess(
                np.expand_dims(bbox_pred[i],
                               0), np.expand_dims(iou_pred[i], 0),
                np.expand_dims(prob_pred[i], 0), originals[i].shape, cfg,
                thresh)

            if len(bboxes) > 0:
                orig = originals[i].copy()

                for box in bboxes:
                    cv2.rectangle(orig, tuple(box[:2]), tuple(box[2:]),
                                  (0, 0, 255))

                cv2.imwrite('./test.jpg', orig)
                pdb.set_trace()
示例#5
0
    def getCarinfofromPic(self, content, method='nparray'):
        image, im_data = self.preprocess(content, method='nparray')
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        bbox_pred, iou_pred, prob_pred = self.net(im_data)

        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        bboxes, scores, cls_inds = yolo_utils.postprocess(bbox_pred,
                                                          iou_pred,
                                                          prob_pred,
                                                          image.shape,
                                                          cfg,
                                                          self.thresh,
                                                          size_index=0)

        roi = []
        for i in range(len(bboxes)):
            roiimage = image[bboxes[i][1]:bboxes[i][3],
                             bboxes[i][0]:bboxes[i][2]]
            roi.append(roiimage)

        return bboxes, scores, cls_inds, image, roi
示例#6
0
def test_net(net, imdb, max_per_image=300, thresh=0.5, vis=False):
    #ci chu xiu gaile
    num_images = imdb.num_images
    #    num_images = 2
    ipdb.set_trace()
    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    #    all_boxes = [[[] for _ in range(num_images)]
    #                 for _ in range(imdb.num_classes)]

    # timers
    #    _t = {'im_detect': Timer(), 'misc': Timer()}
    #    det_file = os.path.join(output_dir, 'detections.pkl')
    # =============================================================================
    size_index = 0
    # =============================================================================
    # =============================================================================
    #    change of me
    #     size_index = args.image_size_index
    # =============================================================================

    for i in range(num_images):

        batch = imdb.next_batch(size_index=size_index)

        ori_im = batch['origin_im'][0]
        im = batch['images']
        gt_boxes = batch['gt_boxes']
        gt_classes = batch['gt_classes']
        dontcare = batch['dontcare']

        im_data = net_utils.np_to_variable(im, is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)

        #        _t['im_detect'].tic()
        bbox_pred, iou_pred, prob_pred = net(im_data, gt_boxes, gt_classes,
                                             dontcare, size_index)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, ori_im.shape, cfg, thresh,
            size_index)

        #        loss = net.loss
        bbox_loss = net.bbox_loss.data.cpu().numpy()[0]
        iou_loss = net.iou_loss.data.cpu().numpy()[0]
        cls_loss = net.cls_loss.data.cpu().numpy()[0]
        print('bbox_loss', bbox_loss)
        print('iou_loss', iou_loss)
        print('cls_loss', cls_loss)
示例#7
0
def sample(net, country, conn, max_samples=20, batch_size=16, thresh=0.5):
    gen = RandomSampler(conn, country, transform)
    upload_count = 0
    img_boxes = []
    cur_filename = None
    cur_whole_img = None
    cur_img_geom = None
    cur = conn.cursor()

    while upload_count < max_samples:
        inputs, originals, meta = zip(*[next(gen) for _ in range(batch_size)])
        inputs = Variable(torch.stack(inputs, 0).cuda(), volatile=True)

        bbox_pred, iou_pred, prob_pred = net(inputs)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        for i in range(len(bbox_pred)):
            row_off, col_off, filename, whole_img, img_geom = meta[i]
            if filename != cur_filename:
                print('Finished processing file')
                res = process_file(cur_filename, img_boxes, cur_whole_img,
                                   country, cur_img_geom)
                if res and cur_filename:
                    cur.execute(
                        "UPDATE buildings.images SET done=true WHERE project=%s AND filename=%s",
                        (country, cur_filename))
                    conn.commit()
                img_boxes = []
                cur_filename = filename
                cur_whole_img = whole_img
                cur_img_geom = img_geom

            bboxes, scores, cls_inds = yolo_utils.postprocess(
                np.expand_dims(bbox_pred[i],
                               0), np.expand_dims(iou_pred[i], 0),
                np.expand_dims(prob_pred[i], 0), originals[i].shape, cfg,
                thresh)
            if len(bboxes) > 3:

                img_data = originals[i].copy()
                for box in bboxes:
                    cv2.rectangle(img_data, tuple(box[:2]), tuple(box[2:]),
                                  (0, 0, 255))

                cv2.imwrite('test.jpg', img_data)

                bboxes[:, (0, 2)] += col_off
                bboxes[:, (1, 3)] += row_off
                img_boxes.append(
                    np.concatenate([bboxes, np.expand_dims(scores, 1)],
                                   axis=1))
示例#8
0
def test_net(net, test_set, data_dir, batch_size = 16, thresh=0.5):
    dataset = img_iter(test_set, data_dir)
    false_positives, false_negatives, true_positives, num_samples = 0,0,0,0
    for batch in range(0, len(test_set), batch_size):
        inputs, originals, targets = zip(*[next(dataset) for _ in range(batch_size)])
        inputs = Variable(torch.stack(inputs, 0).cuda(), volatile=True)

        bbox_pred, iou_pred, prob_pred = net(inputs)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        ridx = np.random.randint(len(bbox_pred))

        for i in range(len(bbox_pred)):
            bboxes, scores, cls_inds = yolo_utils.postprocess(
                np.expand_dims(bbox_pred[i], 0), 
                np.expand_dims(iou_pred[i], 0),
                np.expand_dims(prob_pred[i], 0),
                [300,300,3],
                cfg, 
                thresh
            )

            if ridx == i:
                orig = originals[i].copy()
                for box in bboxes:
                    cv2.rectangle(orig, tuple(box[:2]), tuple(box[2:]), (0,0,255))
                cv2.imwrite('samples/sample_%d.jpg' % batch, orig)

            fp, fn, tp, jaccard = get_metrics(targets[i], bboxes)
            false_positives += fp
            false_negatives += fn
            true_positives += tp
            num_samples += len(targets[i])
        print('False positivies: %d, False negatives: %d, True positivies: %d, Precision: %f, Recall: %f' % 
            (false_positives, false_negatives, true_positives, float(true_positives)/(true_positives + false_positives), float(true_positives)/(true_positives + false_negatives)))
    return false_positives, false_negatives, true_positives, num_samples
示例#9
0
def main():

    shutil.rmtree('output', ignore_errors=True)
    shutil.copytree('output_template', 'output')
    shutil.rmtree('kitti_det_output', ignore_errors=True)
    os.makedirs('kitti_det_output')

    trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_new_2/kitti_new_2_100.h5'
    thresh = 0.5
    use_kitti = True
    image_dir = '/home/cory/KITTI_Dataset/data_object_image_2/training/image_2'

    net = Darknet19()
    net_utils.load_net(trained_model, net)
    net.eval()
    net.cuda()
    print('load model successfully')

    # print(net)

    def str_index(filename):
        if use_kitti:
            return filename
        begin_pos = filename.rfind('_') + 1
        end_pos = filename.rfind('.')
        str_v = filename[begin_pos:end_pos]
        return int(str_v)

    image_extensions = ['.jpg', '.JPG', '.png', '.PNG']
    img_files = open(
        '/home/cory/yolo2-pytorch/train_data/kitti/kitti_val_images.txt')
    image_abs_paths = img_files.readlines()
    image_abs_paths = [f.strip() for f in image_abs_paths]
    '''image_abs_paths = sorted([os.path.join(image_dir, name)
                              for name in os.listdir(image_dir)
                              if name[-4:] in image_extensions],
                             key=str_index)'''

    key_frame_path = ''
    detection_period = 5
    use_flow = False

    kitti_filename = 'yolo_flow_kitti_det.txt'
    try:
        os.remove(kitti_filename)
    except OSError:
        pass

    t_det = Timer()
    t_total = Timer()

    for i, image_path in enumerate(image_abs_paths):
        t_total.tic()
        image, im_data = preprocess(image_path)
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)

        layer_of_flow = 'conv4'
        t_det.tic()
        bbox_pred, iou_pred, prob_pred = net.forward(im_data)

        det_time = t_det.toc()

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)
        det_obj = detection_objects(bboxes, scores, cls_inds)
        save_as_kitti_format(i, det_obj, kitti_filename, src_label='kitti')

        vis_enable = False
        if vis_enable:
            im2show = yolo_utils.draw_detection(image, bboxes, scores,
                                                cls_inds, cfg)

            cv2.imshow('detection', im2show)
            cv2.imwrite('output/detection/{:04d}.jpg'.format(i), im2show)

        total_time = t_total.toc()
        format_str = 'frame: %d, (detection: %.1f fps, %.1f ms) (total: %.1f fps, %.1f ms)'
        print(format_str % (i, 1. / det_time, det_time * 1000, 1. / total_time,
                            total_time * 1000))

        t_det.clear()
        t_total.clear()

        if vis_enable:
            key = cv2.waitKey(0)
            if key == ord('q'):
                break
示例#10
0
def test_net(net, imdb, max_per_image=300, thresh=0.5, vis=False):
    num_images = imdb.num_images

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)]
                 for _ in range(imdb.num_classes)]

    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}
    det_file = os.path.join(output_dir, 'detections.pkl')
    size_index = args.image_size_index

    for i in range(num_images):

        batch = imdb.next_batch(size_index=size_index)
        ori_im = batch['origin_im'][0]
        im_data = net_utils.np_to_variable(batch['images'], is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)

        _t['im_detect'].tic()
        bbox_pred, iou_pred, prob_pred = net(im_data)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        bboxes, scores, cls_inds = yolo_utils.postprocess(bbox_pred,
                                                          iou_pred,
                                                          prob_pred,
                                                          ori_im.shape,
                                                          cfg,
                                                          thresh,
                                                          size_index
                                                          )
        detect_time = _t['im_detect'].toc()

        _t['misc'].tic()

        for j in range(imdb.num_classes):
            inds = np.where(cls_inds == j)[0]
            if len(inds) == 0:
                all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
                continue
            c_bboxes = bboxes[inds]
            c_scores = scores[inds]
            c_dets = np.hstack((c_bboxes,
                                c_scores[:, np.newaxis])).astype(np.float32,
                                                                 copy=False)
            all_boxes[j][i] = c_dets

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1]
                                      for j in range(imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in range(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        nms_time = _t['misc'].toc()

        if i % 20 == 0:
            print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'.format(i + 1, num_images, detect_time, nms_time))  # noqa
            _t['im_detect'].clear()
            _t['misc'].clear()

        if vis:
            im2show = yolo_utils.draw_detection(ori_im,
                                                bboxes,
                                                scores,
                                                cls_inds,
                                                cfg,
                                                thr=0.1)
            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show,
                                     (int(1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))  # noqa
            cv2.imshow('test', im2show)
            cv2.waitKey(0)

    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
    imdb.evaluate_detections(all_boxes, output_dir)
示例#11
0
    bbox_pred, iou_pred, prob_pred = model(im_data)

    det_time = t_det.toc()

    # to numpy
    bbox_pred = bbox_pred.data.cpu().numpy()

    iou_pred = iou_pred.data.cpu().numpy()

    prob_pred = prob_pred.data.cpu().numpy()

    bboxes, scores, cls_inds = yolo_utils.postprocess(
        bbox_pred,
        iou_pred,
        prob_pred,
        image.shape,
        cfg,
        cfg.thresh,
        cfg.iou_thresh,
        size_index=cfg.size_index)

    im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds, cfg,
                                        cfg.thresh)
    total_time = t_total.toc()

    if im2show.shape[0] > 1100:
        im2show = cv2.resize(
            im2show,
            (int(1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))

    im2show = im2show[..., ::-1]
示例#12
0
def main():

    shutil.rmtree('output', ignore_errors=True)
    shutil.copytree('output_template', 'output')

    # trained_model = cfg.trained_model
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_new_2/kitti_new_2_60.h5'
    trained_model = '/home/cory/yolo2-pytorch/models/training/voc0712_obj_scale/voc0712_obj_scale_1.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_det_new_2/kitti_det_new_2_40.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_det_new_2/kitti_det_new_2_10.h5'
    thresh = 0.5
    use_kitti = True
    image_dir = '/home/cory/KITTI_Dataset/data_tracking_image_2/training/image_02/0013'
    # car = 1 5
    # pedestrian = 13 17

    net = Darknet19()
    net_utils.load_net(trained_model, net)
    net.eval()
    net.cuda()
    print('load model successfully')
    # print(net)

    def str_index(filename):
        if use_kitti:
            return filename
        begin_pos = filename.rfind('_') + 1
        end_pos = filename.rfind('.')
        str_v = filename[begin_pos: end_pos]
        return int(str_v)

    image_extensions = ['.jpg', '.JPG', '.png', '.PNG']
    image_abs_paths = sorted([os.path.join(image_dir, name)
                              for name in os.listdir(image_dir)
                              if name[-4:] in image_extensions],
                             key=str_index)

    key_frame_path = ''
    detection_period = 5
    use_flow = False

    kitti_filename = 'yolo_flow_kitti_det.txt'
    try:
        os.remove(kitti_filename)
    except OSError:
        pass

    t_det = Timer()
    t_total = Timer()

    for i, image_path in enumerate(image_abs_paths):
        t_total.tic()
        image, im_data = preprocess(image_path)
        im_data = net_utils.np_to_variable(im_data, is_cuda=True, volatile=True).permute(0, 3, 1, 2)

        layer_of_flow = 'conv4'
        # key frame
        if i % detection_period == 0 and use_flow:
            key_frame_path = image_path
            # conv5 feature map
            feature = net.get_feature_map(im_data=im_data, layer=layer_of_flow)
            feature = feature.data.cpu().numpy()
            feature_map_all = plot_feature_map(feature, resize_ratio=1)
            # cv2.imshow('feature_map', feature_map_all)
            cv2.imwrite('output/feature_map/{:04d}.jpg'.format(i), feature_map_all * 255)

        t_det.tic()
        if use_flow:
            t1 = time.time()
            conv5_shifted_gpu = detect_by_flow(i, feature, image, image_path, key_frame_path)
            t2 = time.time()
            print('detect_by_flow', t2 - t1)
            bbox_pred, iou_pred, prob_pred = net.feed_feature(Variable(conv5_shifted_gpu), layer=layer_of_flow)

        else:
            bbox_pred, iou_pred, prob_pred = net.forward(im_data)

        det_time = t_det.toc()

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)
        det_obj = detection_objects(bboxes, scores, cls_inds)
        save_as_kitti_format(i, det_obj, kitti_filename, src_label='kitti')

        im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds, cfg)

        cv2.imshow('detection', im2show)
        cv2.imwrite('output/detection/{:04d}.jpg'.format(i), im2show)

        total_time = t_total.toc()
        format_str = 'frame: %d, (detection: %.1f fps, %.1f ms) (total: %.1f fps, %.1f ms)'
        print(format_str % (
            i, 1. / det_time, det_time * 1000, 1. / total_time, total_time * 1000))

        t_det.clear()
        t_total.clear()

        key = cv2.waitKey(1)
        if key == ord('q'):
            break
示例#13
0
def test_net_img_only(net, img_list, max_per_image=300, thresh=0.5, vis=False):
    num_images = len(img_list)

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)]
                 for _ in range(cfg.num_classes)]

    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}
    det_file = os.path.join(output_dir, 'detections.pkl')
    size_index = args.image_size_index
    inp_size = cfg.multi_scale_inp_size
    if not os.path.exists("result"):
        os.mkdir("result")
    dt = dataTransform.dataTransform()
    for i in range(num_images):
        img_name = img_list[i]
        im, _, __, ___, ori_im = test_only_transform(img_name, inp_size,
                                                     size_index)
        im = np.reshape(im,
                        newshape=(-1, im.shape[0], im.shape[1], im.shape[2]))
        im_data = net_utils.np_to_variable(im, is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        with torch.set_grad_enabled(False):
            bbox_pred, iou_pred, prob_pred = net(im_data)
        '''
        bbox->(batch,h*w,prior 4)
        iou ->(batch,h*w,prior,1)
        prob_pred-->(batch,h*w,prior,20)
        '''
        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()
        '''
        这里后处理的是:
        return bbox_pred, scores, cls_inds
        '''
        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, ori_im.shape, cfg, thresh,
            size_index)
        detect_time = _t['im_detect'].toc()

        _t['misc'].tic()
        '''
        以下的操作是
        对我们预测的值进行处理,这里需要注意的是,对于
        这些问题,我们在最后头保留它的概率
        并对最后的概率获取
        '''
        for j in range(imdb.num_classes):
            inds = np.where(cls_inds == j)[0]
            if len(inds) == 0:
                all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
                continue
            c_bboxes = bboxes[inds]
            c_scores = scores[inds]
            c_dets = np.hstack(
                (c_bboxes, c_scores[:, np.newaxis])).astype(np.float32,
                                                            copy=False)
            all_boxes[j][i] = c_dets

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack(
                [all_boxes[j][i][:, -1] for j in range(imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in range(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        #save detect_result to xml
        dt.writeXml(img_name, "./result", ori_im, cfg.label_names,
                    cls_inds.tolist(), bboxes.tolist())
        if vis:
            im2show = yolo_utils.draw_detection(ori_im,
                                                bboxes,
                                                scores,
                                                cls_inds,
                                                cfg,
                                                thr=0.5)
            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show,
                                     (int(1000. * float(im2show.shape[1]) /
                                          im2show.shape[0]), 1000))  # noqa
            cv2.imshow('test', im2show)
            cv2.waitKey(0)
示例#14
0
    def test(
        self,
        path_in='demo',
        path_out='./interface/tmp/wx_d',
        cut_path='./result/cut/',
        retrival_params=['自动', None, None, '全选', '全选', '全选', '全选', '全选',
                         '全选']):
        #Read Infomation
        #params---->sex,prince1,prince2,dress,top,bottom,version,style,age_value
        #print "Test"
        t_det = Timer()
        t_total = Timer()
        # im_fnames = ['person.jpg']
        im_fnames = sorted(
            (fname for fname in os.listdir(path_in)
             if os.path.splitext(fname)[-1] == '.jpg'))  # shuffle data
        # shutil.rmtree(cut_path)
        # os.mkdir(cut_path)
        for i in im_fnames:
            im_fname = os.path.join(path_in, i)
            #im_fnames = (os.path.join(path_in, fname) for fname in im_fnames)
            #print im_fname
            self.people_sex = None
            self.people_age = 20
            if '自动' in retrival_params[0] or '自动' in retrival_params[-1]:
                try:
                    #self.img_uploader.run(im_fname)
                    self.result = self.face.run(im_fname)
                    #print self.result

                    if len(self.result) == 1:
                        if self.result[0][u'faceAttributes'][
                                u'gender'] == u'female':
                            self.people_sex = '2'
                        else:
                            self.people_sex = '1'
                    self.people_age = self.result[0][u'faceAttributes'][u'age']
                    #print "people_sex:"
                    #print self.people_sex
                except:
                    print "can't found face identify"
                    pass

            print 'sex:'
            print self.people_sex
            #pool = Pool(processes=1)
            #print "HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

            #for i, (image, im_data, im_name) in enumerate(pool.imap(preprocess, im_fnames, chunksize=1)):
            (image, im_data, im_name) = preprocess(im_fname)
            t_total.tic()
            im_data = net_utils.np_to_variable(im_data,
                                               is_cuda=True,
                                               volatile=True).permute(
                                                   0, 3, 1, 2)
            im_name = os.path.split(im_name)[-1]
            # print im_name
            t_det.tic()
            #print "oooooooooooooooo1"
            bbox_pred, iou_pred, prob_pred = self.net(im_data)
            det_time = t_det.toc()
            # to numpy
            bbox_pred = bbox_pred.data.cpu().numpy()
            iou_pred = iou_pred.data.cpu().numpy()
            prob_pred = prob_pred.data.cpu().numpy()

            # print bbox_pred.shape, iou_pred.shape, prob_pred.shape
            #print "oooooooooooooooo2"
            self.bboxes, self.scores, self.cls_inds = yolo_utils.postprocess(
                bbox_pred, iou_pred, prob_pred, image.shape, cfg, self.thresh)
            #print image.shape
            #print "bboxes"
            #print bboxes
            #print "HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
            cls_res = []
            cls_score = []
            cls_res_top5 = []
            cls_score_top5 = []
            flag = 0
            for i, bbox in enumerate(self.bboxes):
                #print "bbox",bbox
                print "start===================================================%d" % i
                cut_image = image[bbox[1]:bbox[3], bbox[0]:bbox[2]]
                res_num, res_score = cloth_num(cut_image, self.model_res)
                cls_res.append(self.num_classes[res_num[0]])
                cls_re = []
                for n in range(10):
                    cls_re.append(self.num_classes[res_num[n]])
                    #print "++++++++++++++++++++++++++++++"+str(n)
                    #print Cnum[0][n],res_num[0]
                    #print self.num_classes[Cnum[0][n]]
                #print "-------------"
                #print res_score
                #If the adjacent two types of differentiation is too small,it is considered that the category that the category can not be determined
                if res_score[
                        0] < 12 and res_score[1] * 1.0 / res_score[0] > 0.9:
                    #print res_score[1]*1.0/res_score[0]
                    cls_score.append(0)
                else:
                    #If two overlapping frames of the same category exceed 0.4,only the box with the largest weight is saved
                    for j, obox in enumerate(self.bboxes[:i]):
                        # (bbox[0], bbox[1]), (bbox[2], bbox[3])
                        if obox[2] > bbox[0] and obox[3] > bbox[1] and obox[
                                0] < bbox[2] and obox[1] < bbox[3]:
                            print "------------------------xiangjiao------------------------------"
                            box_inter = (max(obox[0],
                                             bbox[0]), max(obox[1], bbox[1]),
                                         min(obox[2],
                                             bbox[2]), min(obox[3], bbox[3]))
                            area1 = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
                            area2 = (obox[2] - obox[0]) * (obox[3] - obox[1])
                            area_min = min((area1, area2))
                            area_inter = (box_inter[2] - box_inter[0]) * (
                                box_inter[3] - box_inter[1])
                            #print area_min,area_inter
                            if area_inter * 1.0 / area_min >= 0.5:
                                print "====================YEEEEEES++++++++++++++++"
                                #if cls_res[i]==cls_res[j]:
                                if res_score[0] > cls_score[j]:
                                    if cls_score[j] != 0:
                                        cls_score[j] = 1
                                        cls_score_top5[j][0] = 1
                                else:
                                    res_score[0] = 1
                                    break
                    cls_score.append(res_score[0])
                cls_res_top5.append(cls_re)
                #print cls_score
                cls_score_top5.append(res_score)

                #print res_num
                #cls_score.append(res_score)
                #print "resnet"
                #print cls_res,cls_score

                #cut_for_intel = cv2.resize(cut_image, (800, 500))
                #print "===================================================%d"%i
                #cut_for_intel = cv2.resize(cut_image, (int(1000. * float(cut_image.shape[1]) / cut_image.shape[0]), 1000))
                #cv2.imwrite(os.path.join(cut_path,"%d_%s"%(i,im_name)),cut_for_intel)
            self.cls_res = cls_res
            self.cls_score = cls_score
            self.cls_res_top5 = cls_res_top5
            self.cls_score_top5 = cls_score_top5
            #im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds, cfg)
            #print "cls_inds:"
            #print type(self.cls_inds),self.cls_inds
            # print "cls_res:"
            #
            # cls_res=np.array(cls_res)
            #print type(cls_res),cls_res
            # image 原始图像 bboxes 识别框坐标 scores识别置信度 cls_res 第一个分类类别 cls_score 第一个分类置信度 f_info 识别文本信息 f_pic识别的类别信息 cls_res_top5前十个分类类别
            # cls_score_top5前十个分类置信度,f_pic_like识别的相似类别信息,f_pic_rec根据类别推荐的信息,f_pic_rec_like根据相似类别推荐的信息,cls_inds识别的大类别
            f_info = open(InfoDir, 'w')
            f_pic = open(InfoPicPath, 'w')
            f_pic_like = open(InfoPicLikePath, 'w')
            f_pic_rec = open(InfoPicPathRec, 'w')
            f_pic_rec_like = open(InfoPicLikePathRec, 'w')
            im2show = self.draw_detection_rec(image,
                                              self.bboxes,
                                              self.cls_inds,
                                              self.scores,
                                              self.cls_res_top5,
                                              self.cls_score_top5,
                                              f_info,
                                              f_pic,
                                              f_pic_like,
                                              f_pic_rec,
                                              f_pic_rec_like,
                                              sex_dec=self.people_sex,
                                              age_dec=self.people_age,
                                              retrival_params=retrival_params)
            f_info.close()
            f_pic.close()
            f_pic_like.close()
            f_pic_rec.close()
            f_pic_rec_like.close()
            #for i,j in enumerate(cls_res):
            #    info = "识别概率:%0.2f\t准确度:%0.2f\t类别:%s\n" % ((i[0][1]), i[0][0], score_class)

            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show, (int(
                    1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))
            # cv2.imshow('test', im2show)
            #cv2.imwrite("./result/test/{}".format(im_name), im2show)
            cv2.imwrite(os.path.join(path_out, im_name), im2show)
            total_time = t_total.toc()
            # wait_time = max(int(60 - total_time * 1000), 1)
            # cv2.waitKey(0)

            #format_str = 'frame: %d, (detection: %.1f Hz, %.1f ms) (total: %.1f Hz, %.1f ms)'
            #print(format_str % (i, 1. / det_time, det_time * 1000, 1. / total_time, total_time * 1000))

        t_total.clear()
        t_det.clear()
示例#15
0
    def cloth_test(self,
                   path_in='./interface/intel_pic/pair.jpg',
                   cut_out='./interface/cut_pic/'):
        #服装识别
        (image, im_data, im_name) = preprocess(path_in)
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        im_name = os.path.split(im_name)[-1]
        bbox_pred, iou_pred, prob_pred = self.net(im_data)
        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape
        # print "oooooooooooooooo2"
        #self.bboxes, self.scores, self.cls_inds = yolo_utils.postprocess(bbox_pred, iou_pred, prob_pred, image.shape, cfg, self.thresh)
        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, image.shape, cfg, self.thresh)
        #服装分类
        cls_res = []
        cls_score = []
        cls_res_top5 = []
        cls_score_top5 = []
        cut_dec = []
        if os.path.isdir(cut_out):
            shutil.rmtree(cut_out)
        os.mkdir(cut_out)
        for i, bbox in enumerate(bboxes):

            # print "bbox",bbox
            #print "start===================================================%d" % i
            cut_image = image[bbox[1]:bbox[3], bbox[0]:bbox[2]]
            res_num, res_score = cloth_num(cut_image, self.model_res)
            cls_res.append(self.num_classes[res_num[0]])
            cls_re = []
            for n in range(10):
                cls_re.append(self.num_classes[res_num[n]])
                # print "++++++++++++++++++++++++++++++"+str(n)
                # print Cnum[0][n],res_num[0]
                # print self.num_classes[Cnum[0][n]]
            # print "-------------"
            # print res_score
            # If the adjacent two types of differentiation is too small,it is considered that the category that the category can not be determined
            if res_score[0] < 12 and res_score[1] * 1.0 / res_score[0] > 0.9:
                # print res_score[1]*1.0/res_score[0]
                cls_score.append(0)
            else:
                # If two overlapping frames of the same category exceed 0.4,only the box with the largest weight is saved
                for j, obox in enumerate(bboxes[:i]):
                    # (bbox[0], bbox[1]), (bbox[2], bbox[3])
                    if obox[2] > bbox[0] and obox[3] > bbox[1] and obox[
                            0] < bbox[2] and obox[1] < bbox[3]:
                        print "------------------------xiangjiao------------------------------"
                        box_inter = (max(obox[0],
                                         bbox[0]), max(obox[1], bbox[1]),
                                     min(obox[2],
                                         bbox[2]), min(obox[3], bbox[3]))
                        area1 = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
                        area2 = (obox[2] - obox[0]) * (obox[3] - obox[1])
                        area_min = min((area1, area2))
                        area_inter = (box_inter[2] - box_inter[0]) * (
                            box_inter[3] - box_inter[1])
                        # print area_min,area_inter
                        if area_inter * 1.0 / area_min >= 0.4:
                            print "====================YEEEEEES++++++++++++++++"
                            if cls_res[i] == cls_res[j]:
                                if res_score[0] > cls_score[j]:
                                    if cls_score[j] != 0:
                                        cls_score[j] = 1
                                        cls_score_top5[j][0] = 1
                                else:
                                    res_score[0] = 1
                                    break
                cls_score.append(res_score[0])
            cls_res_top5.append(cls_re)
            #print cls_score
            cls_score_top5.append(res_score)

            # print res_num
            # cls_score.append(res_score)
            # print "resnet"
            # print cls_res,cls_score

            # # cut_for_intel = cv2.resize(cut_image, (800, 500))
            print "===================================================%d" % i
            cut_for_intel = cv2.resize(
                cut_image,
                (int(1000. * float(cut_image.shape[1]) / cut_image.shape[0]),
                 1000))  #存储识别到的服装
            cut_dec.append(cut_for_intel)
            cv2.imwrite(os.path.join(cut_out, "%d_%s" % (i, im_name)),
                        cut_for_intel)
        #print cls_inds
        #print cls_res_top5
        return cls_inds, cls_res_top5, cls_score_top5, cut_dec
示例#16
0
def home():
    data = request.body.read()
    body = json.loads(data)
    im_path = body['dir_path']
    #im_path = 'demo'
    im_fnames = sorted((fname for fname in os.listdir(im_path)\
                        if os.path.splitext(fname)[-1] == '.jpg'))
    im_fnames = (os.path.join(im_path, fname) for fname in im_fnames)

    min_record_tmp_list = [0] * len(det_class)

    for i, (image, im_data,
            fname) in enumerate(pool.imap(preprocess, im_fnames, chunksize=1)):
        print(fname)
        t_total.tic()
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        t_det.tic()
        bbox_pred, iou_pred, prob_pred = net(im_data)
        det_time = t_det.toc()
        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)

        im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds,
                                            cfg)

        ## create list that used to write to database
        path_list = fname.split("/")
        filename = path_list.pop()
        time_folder = im_path

        # wirte im2show to out dir
        im_out_path = os.path.join(time_folder, "out")
        check_path_create(im_out_path)
        cv2.imwrite(os.path.join(im_out_path, filename), im2show)

        tmp_list = ['0'] * len(det_class)
        for i in cls_inds:
            try:
                tmp_list[det_class.index(cfg.label_names[i])] = '1'
                min_record_tmp_list[det_class.index(cfg.label_names[i])] += 1
            except:
                pass

        tmp_list.insert(0, time_folder)
        tmp_list.insert(0, filename)
        conn.execute(
            """insert into images_det (name, time_folder, %s)\
                        values (%s)""" %
            (",".join(det_class), ",".join(['?'] * len(tmp_list))), tmp_list)
        conn.commit()
        total_time = t_total.toc()

        if i % 1 == 0:
            format_str = 'frame: %d, (detection: %.1f Hz, %.1f ms) (total: %.1f Hz, %.1f ms)'
            print(format_str % (i, 1. / det_time, det_time * 1000,
                                1. / total_time, total_time * 1000))

            t_total.clear()
            t_det.clear()

    tmp_list = [im_path]
    min_record_tmp_list = [str(i) for i in min_record_tmp_list]
    tmp_list.extend(min_record_tmp_list)
    conn.execute(
        """insert into minute_det (time_folder, %s)
                    values (%s)""" %
        (",".join(det_class), ",".join(['?'] * len(tmp_list))), tmp_list)
    conn.commit()
示例#17
0
def test_net(net, imdb, max_per_image=300, thresh=0.5, vis=False):
# =============================================================================
#     chang here for ryan
# =============================================================================
    num_images = imdb.num_images
    print('num-images',num_images)
#    num_images = 3

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)]
                 for _ in range(imdb.num_classes)]

    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}
    det_file = os.path.join(output_dir, 'detections.pkl')
# =============================================================================
#     change size_index = 0
# =============================================================================
#    size_index = args.image_size_index
    size_index = 0

    for i in range(num_images):

        batch = imdb.next_batch(size_index=size_index)
#        print('next_batch')
        ori_im = batch['origin_im'][0]
        
        im_data = net_utils.np_to_variable(batch['images'], is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        
#        print('im_data')

        _t['im_detect'].tic()
        bbox_pred, iou_pred, prob_pred = net(im_data)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        bboxes, scores, cls_inds = yolo_utils.postprocess(bbox_pred,
                                                          iou_pred,
                                                          prob_pred,
                                                          ori_im.shape,
                                                          cfg,
                                                          thresh,
                                                          size_index
                                                          )
        detect_time = _t['im_detect'].toc()

        _t['misc'].tic()

        for j in range(imdb.num_classes):
            inds = np.where(cls_inds == j)[0]
            if len(inds) == 0:
                all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
                continue
            c_bboxes = bboxes[inds]
            c_scores = scores[inds]
            c_dets = np.hstack((c_bboxes,
                                c_scores[:, np.newaxis])).astype(np.float32,
                                                                 copy=False)
            all_boxes[j][i] = c_dets

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1]
                                      for j in range(imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in range(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        nms_time = _t['misc'].toc()

        if i % 20 == 0:
            print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'.format(i + 1, num_images, detect_time, nms_time))  # noqa
            _t['im_detect'].clear()
            _t['misc'].clear()

        if vis:
            im2show = yolo_utils.draw_detection(ori_im,
                                                bboxes,
                                                scores,
                                                cls_inds,
                                                cfg,
                                                thr=0.1)
            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show,
                                     (int(1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))  # noqa
            cv2.imshow('test', im2show)
            cv2.waitKey(0)

    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
    imdb.evaluate_detections(all_boxes, output_dir)
示例#18
0
    def test_ontime(
        self,
        path_in='demo',
        path_out='./interface/tmp/wx_d',
        dec_flag=0,
        cv_im=[],
        da_flag=1,
        retrival_params=['自动', None, None, '全选', '全选', '全选', '全选', '全选',
                         '全选']):
        #Read Infomation

        #print "Test"
        t_det = Timer()
        t_total = Timer()
        # if dec_flag==1:
        #     shutil.rmtree("./result/cut/")
        #     os.mkdir("./result/cut/")
        # im_fnames = ['person.jpg']
        im_fnames = sorted(
            (fname for fname in os.listdir(path_in)
             if os.path.splitext(fname)[-1] == '.jpg'))  # shuffle data
        im_fname = os.path.join(path_in, im_fnames[0])

        if cv_im == []:
            (image, im_data, im_name) = preprocess(im_fname)
            exit()
        else:
            #print "OOOOOOOOOOOOOOOOOOOOOOJBK"
            image = cv_im
            im_name = im_fname
            im_data = get_ImData(cv_im)

        t_total.tic()
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        im_name = os.path.split(im_name)[-1]
        # print im_name
        t_det.tic()

        if dec_flag:
            self.people_sex = None
            self.people_age = 20
            #if retrival_params[0] == '自动' or retrival_params[-1] == '自动':
            if '自动' in retrival_params[0] or '自动' in retrival_params[-1]:
                try:
                    #self.img_uploader.run(im_fname)
                    self.result = self.face.run(im_fname)
                    #print self.result

                    if len(self.result) == 1:
                        if self.result[0][u'faceAttributes'][
                                u'gender'] == u'female':
                            self.people_sex = '2'
                        else:
                            self.people_sex = '1'
                    #print "people_sex:"
                    #print self.people_sex
                except:
                    print "can't contact face identify"
                    pass
            #print 'sex:'
            #print self.people_sex
            #print "ooooooooooooooooooooooooooooooooooooooooooo"
            #print "oooooooooooooooo1"
            #print "oook"
            bbox_pred, iou_pred, prob_pred = self.net(im_data)
            det_time = t_det.toc()
            det_time = t_det.toc()
            # to numpy
            bbox_pred = bbox_pred.data.cpu().numpy()
            iou_pred = iou_pred.data.cpu().numpy()
            prob_pred = prob_pred.data.cpu().numpy()

            # print bbox_pred.shape, iou_pred.shape, prob_pred.shape
            #print "oooooooooooooooo2"
            self.bboxes, self.scores, self.cls_inds = yolo_utils.postprocess(
                bbox_pred, iou_pred, prob_pred, image.shape, cfg, self.thresh)
            #print image.shape
            #print "bboxes"
            #print bboxes
            #print "HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
            cls_res = []
            cls_score = []
            cls_res_top5 = []
            cls_score_top5 = []
            flag = 0
            for i, bbox in enumerate(self.bboxes):
                #print "bbox",bbox
                cut_image = image[bbox[1]:bbox[3], bbox[0]:bbox[2]]
                res_num, res_score = cloth_num(cut_image, self.model_res)
                cls_res.append(self.num_classes[res_num[0]])
                cls_re = []
                for n in range(10):
                    cls_re.append(self.num_classes[res_num[n]])
                    #print "++++++++++++++++++++++++++++++"+str(n)
                    #print Cnum[0][n],res_num[0]
                    #print self.num_classes[Cnum[0][n]]
                cls_res_top5.append(cls_re)
                cls_score_top5.append(res_score)
                #print "-------------"
                #print res_score
                #If the adjacent two types of differentiation is too small,it is considered that the category that the category can not be determined
                if res_score[
                        0] < 12 and res_score[1] * 1.0 / res_score[0] > 0.9:
                    #print res_score[1]*1.0/res_score[0]
                    cls_score.append(0)
                else:
                    #If two overlapping frames of the same category exceed 0.4,only the box with the largest weight is saved
                    for j, obox in enumerate(self.bboxes[:i]):
                        # (bbox[0], bbox[1]), (bbox[2], bbox[3])
                        if obox[2] > bbox[0] and obox[3] > bbox[1] and obox[
                                0] < bbox[2] and obox[1] < bbox[3]:
                            print "------------------------xiangjiao------------------------------"
                            box_inter = (max(obox[0],
                                             bbox[0]), max(obox[1], bbox[1]),
                                         min(obox[2],
                                             bbox[2]), min(obox[3], bbox[3]))
                            area1 = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
                            area2 = (obox[2] - obox[0]) * (obox[3] - obox[1])
                            area_min = min((area1, area2))
                            area_inter = (box_inter[2] - box_inter[0]) * (
                                box_inter[3] - box_inter[1])
                            #print area_min,area_inter
                            if area_inter * 1.0 / area_min >= 0.5:
                                print "====================YEEEEEES++++++++++++++++"
                                #if cls_res[i]==cls_res[j]:
                                if res_score[0] > cls_score[j]:
                                    if cls_score[j] != 0:
                                        cls_score[j] = 1
                                else:
                                    res_score[0] = 1
                                    break
                    cls_score.append(res_score[0])

                #print res_num
                #cls_score.append(res_score)
                #print "resnet"
                #print cls_res,cls_score

                #cut_for_intel = cv2.resize(cut_image, (800, 500))
                #cut_for_intel = cv2.resize(cut_image,(int(1000. * float(cut_image.shape[1]) / cut_image.shape[0]), 1000))
                #cv2.imwrite("./result/cut/%d_%s"%(i,im_name),cut_for_intel)
            self.cls_res = cls_res
            self.cls_score = cls_score
            self.cls_res_top5 = cls_res_top5
            self.cls_score_top5 = cls_score_top5
            #print "bbboxes"
            #print self.bboxes
            # im2show = yolo_utils.draw_detection_rec(image, self.bboxes, self.scores, self.cls_res,self.cls_score,f_info,f_pic,self.cls_res_top5,self.cls_score_top5,f_pic_like,f_pic_rec,f_pic_rec_like,self.cls_inds)
        else:
            # 用于显示
            #print "bbboxes"
            #print self.bboxes
            #im2show = yolo_utils.draw_detection_rec(image, self.bboxes, self.scores, self.cls_res,self.cls_score,f_info,f_pic,self.cls_res_top5,self.cls_score_top5,f_pic_like,f_pic_rec,f_pic_rec_like,self.cls_inds)
            f_info = open(InfoDir, 'w')
            f_pic = open(InfoPicPath, 'w')
            f_pic_like = open(InfoPicLikePath, 'w')
            f_pic_rec = open(InfoPicPathRec, 'w')
            f_pic_rec_like = open(InfoPicLikePathRec, 'w')
            im2show = self.draw_detection_rec(image,
                                              self.bboxes,
                                              self.cls_inds,
                                              self.scores,
                                              self.cls_res_top5,
                                              self.cls_score_top5,
                                              f_info,
                                              f_pic,
                                              f_pic_like,
                                              f_pic_rec,
                                              f_pic_rec_like,
                                              flag=1,
                                              sex_dec=self.people_sex,
                                              age_dec=self.people_age,
                                              retrival_params=retrival_params)
            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show, (int(
                    1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))
            cv2.imwrite(os.path.join(path_out, im_name), im2show)
            f_info.close()
            f_pic.close()
            f_pic_like.close()
            f_pic_rec.close()
            f_pic_rec_like.close()

        t_total.clear()
        t_det.clear()
示例#19
0
for i, (image, im_data) in enumerate(pool.imap(
        preprocess, im_fnames, chunksize=1)):
    t_total.tic()
    im_data = net_utils.np_to_variable(
        im_data, is_cuda=True, volatile=True).permute(0, 3, 1, 2)
    t_det.tic()
    bbox_pred, iou_pred, prob_pred = net(im_data)
    det_time = t_det.toc()
    # to numpy
    bbox_pred = bbox_pred.data.cpu().numpy()
    iou_pred = iou_pred.data.cpu().numpy()
    prob_pred = prob_pred.data.cpu().numpy()

    # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

    bboxes, scores, cls_inds = yolo_utils.postprocess(
        bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)

    im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds, cfg)

    if im2show.shape[0] > 1100:
        im2show = cv2.resize(im2show,
                             (int(1000. *
                                  float(im2show.shape[1]) / im2show.shape[0]),
                              1000))
    cv2.imshow('test', im2show)

    total_time = t_total.toc()
    # wait_time = max(int(60 - total_time * 1000), 1)
    cv2.waitKey(0)

    if i % 1 == 0:
示例#20
0
def test_net(net, imdb, max_per_image=300, thresh=0.5, vis=False):

    num_images = imdb.num_images

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)]
                 for _ in range(imdb.num_classes)]

    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}
    det_file = os.path.join(output_dir, 'detections.pkl')
    size_index = args.image_size_index
    #helper: 0:320, 1:352, 2:384, 3:416, 4:448, 5:480, 6:512, 7:544, 8:576'
    #here val_img sometimes is 5123
    for i in range(num_images):

        batch = imdb.next_batch(size_index=size_index)
        ori_im = batch['origin_im'][0]
        im_data = net_utils.np_to_variable(batch['images'],
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)

        _t['im_detect'].tic()
        with torch.set_grad_enabled(False):
            bbox_pred, iou_pred, prob_pred = net(im_data)
        '''
        bbox->(batch,h*w,prior 4)
        iou ->(batch,h*w,prior,1)
        prob_pred-->(batch,h*w,prior,20)
        '''
        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()
        '''
        这里后处理的是:
        return bbox_pred, scores, cls_inds
        '''
        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, ori_im.shape, cfg, thresh,
            size_index)
        detect_time = _t['im_detect'].toc()

        _t['misc'].tic()
        '''
        以下的操作是
        对我们预测的值进行处理,这里需要注意的是,对于
        这些问题,我们在最后头保留它的概率
        并对最后的概率获取
        '''
        for j in range(imdb.num_classes):
            inds = np.where(cls_inds == j)[0]
            if len(inds) == 0:
                all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
                continue
            c_bboxes = bboxes[inds]
            c_scores = scores[inds]
            c_dets = np.hstack(
                (c_bboxes, c_scores[:, np.newaxis])).astype(np.float32,
                                                            copy=False)
            all_boxes[j][i] = c_dets

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack(
                [all_boxes[j][i][:, -1] for j in range(imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in range(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        nms_time = _t['misc'].toc()

        if i % 20 == 0:
            print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'.format(
                i + 1, num_images, detect_time, nms_time))  # noqa
            _t['im_detect'].clear()
            _t['misc'].clear()

        if vis:
            im2show = yolo_utils.draw_detection(ori_im,
                                                bboxes,
                                                scores,
                                                cls_inds,
                                                cfg,
                                                thr=0.1)
            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show,
                                     (int(1000. * float(im2show.shape[1]) /
                                          im2show.shape[0]), 1000))  # noqa
            cv2.imshow('test', im2show)
            cv2.waitKey(0)
    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
示例#21
0
def test_net(net, imdb, max_per_image=300, thresh=0.5, vis=False, num_classes=8):
    num_images = imdb.size

    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)
    all_boxes = [[[] for _ in range(num_images)]
                 for _ in range(num_classes)]

    dt_loader = DataLoader(kitti, batch_size=1, shuffle=False)
    # timers
    _t = {'im_detect': Timer(), 'misc': Timer()}
    det_file = os.path.join(output_dir, 'detections.pkl')
    size_index = args.image_size_index
    i = 0
    for im_data, gt_boxes, gt_classes, dontcare, ori_imgs in dt_loader.get_batch():

        # ori_im = (np.rollaxis(im_data[0].data.numpy(), 0, 3) * 255).astype(np.uint8)
        ori_im = ori_imgs[0]

        _t['im_detect'].tic()
        bbox_pred, iou_pred, prob_pred = net(im_data.cuda())

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # bboxes, scores, cls_inds = yolo_utils.postprocess(bbox_pred, iou_pred, prob_pred, (ori_im.shape[1], ori_im.shape[0]), cfg, thresh, size_index)
        bboxes, scores, cls_inds = yolo_utils.postprocess(bbox_pred, iou_pred, prob_pred, ori_im.shape, cfg, thresh, size_index)
        detect_time = _t['im_detect'].toc()

        _t['misc'].tic()

        for j in range(num_classes):
            inds = np.where(cls_inds == j)[0]
            if len(inds) == 0:
                all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
                continue
            c_bboxes = bboxes[inds]
            c_scores = scores[inds]
            c_dets = np.hstack((c_bboxes,
                                c_scores[:, np.newaxis])).astype(np.float32,
                                                                 copy=False)
            all_boxes[j][i] = c_dets

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1]
                                      for j in range(num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in range(1, num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        nms_time = _t['misc'].toc()

        if i % 20 == 0:
            print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'.format(i + 1, num_images, detect_time, nms_time))  # noqa
            _t['im_detect'].clear()
            _t['misc'].clear()

        if vis:
            im2show = yolo_utils.draw_detection(ori_im, bboxes, scores, cls_inds, cfg, thr=0.1)
            # im2show = yolo_utils.draw_detection(ori_im, np.round(gt_boxes[0]).astype(int), np.array([1]*gt_boxes[0].shape[0]), gt_classes[0], cfg, thr=0.1)

            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show, (int(1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))  # noqa
            print im2show.shape
            cv2.imshow('test', im2show)
            cv2.waitKey(0)
        i += 1

    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
    kitti.evaluate_detections(all_boxes, output_dir)
示例#22
0
for i, (image, im_data) in enumerate(pool.imap(
        preprocess, im_fnames, chunksize=1)):
    t_total.tic()
    im_data = net_utils.np_to_variable(
        im_data, is_cuda=True, volatile=True).permute(0, 3, 1, 2)
    t_det.tic()
    bbox_pred, iou_pred, prob_pred = net(im_data)
    det_time = t_det.toc()
    # to numpy
    bbox_pred = bbox_pred.data.cpu().numpy()
    iou_pred = iou_pred.data.cpu().numpy()
    prob_pred = prob_pred.data.cpu().numpy()

    # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

    bboxes, scores, cls_inds = yolo_utils.postprocess(
        bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)

    im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds, cfg)

    if im2show.shape[0] > 1100:
        im2show = cv2.resize(im2show,
                             (int(1000. *
                                  float(im2show.shape[1]) / im2show.shape[0]),
                              1000))
    cv2.imshow('test', im2show)

    total_time = t_total.toc()
    # wait_time = max(int(60 - total_time * 1000), 1)
    cv2.waitKey(0)

    if i % 1 == 0:
示例#23
0
    def extractObjects(self, video_path):
        import os
        import cv2
        import torch
        import numpy as np
        from torch.multiprocessing import Pool

        from darknet import Darknet19
        import utils.yolo as yolo_utils
        import utils.network as net_utils
        from utils.timer import Timer
        import cfgs.config as cfg

        def preprocess(fname):
            # return fname
            image = cv2.imread(fname)
            im_data = np.expand_dims(
                yolo_utils.preprocess_test((image, None, cfg.inp_size))[0], 0)
            return image, im_data

        # hyper-parameters
        # npz_fname = 'models/yolo-voc.weights.npz'
        # h5_fname = 'models/yolo-voc.weights.h5'
        trained_model = cfg.trained_model
        # trained_model = os.path.join(cfg.train_output_dir, 'darknet19_voc07trainval_exp3_158.h5')
        thresh = 0.5
        im_path = video_path
        # ---

        net = Darknet19()
        net_utils.load_net(trained_model, net)
        # net.load_from_npz(npz_fname)
        # net_utils.save_net(h5_fname, net)
        net.cuda()
        net.eval()
        print('load model succ...')

        t_det = Timer()
        t_total = Timer()
        # im_fnames = ['person.jpg']
        im_fnames = sorted([
            fname for fname in sorted(os.listdir(im_path))
            if os.path.splitext(fname)[-1] == '.jpg'
        ])
        im_fnames = (os.path.join(im_path, fname) for fname in im_fnames)
        objectDetect = []
        for i, (image) in enumerate(im_fnames):
            t_total.tic()
            im_data = preprocess(image)
            image = im_data[0]
            im_data = im_data[1]
            im_data = net_utils.np_to_variable(im_data,
                                               is_cuda=True,
                                               volatile=True).permute(
                                                   0, 3, 1, 2)
            t_det.tic()
            bbox_pred, iou_pred, prob_pred = net(im_data)
            det_time = t_det.toc()
            # to numpy
            bbox_pred = bbox_pred.data.cpu().numpy()
            iou_pred = iou_pred.data.cpu().numpy()
            prob_pred = prob_pred.data.cpu().numpy()

            # print bbox_pred.shape, iou_pred.shape, prob_pred.shape
            bboxes, scores, cls_inds = yolo_utils.postprocess(
                bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)
            objectDetect.append(','.join(
                set([cfg.label_names[i] for i in cls_inds])))
        return objectDetect