def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--fn', default='assets/demo.jpg', type=str)
    parser.add_argument('--output', default='output', type=str)
    parser.add_argument('--inres', default='512,512', type=str)
    args, _ = parser.parse_known_args()
    args.inres = tuple(int(x) for x in args.inres.split(','))
    os.makedirs(args.output, exist_ok=True)
    kwargs = {
        'num_stacks': 2,
        'cnv_dim': 256,
        'weights': 'hpdet_coco',
        'inres': args.inres,
    }
    heads = {
        'hm': 1,  # 6
        'hm_hp': 17,  # 7
        'hp_offset': 2,  # 8
        'hps': 34,  # 9
        'reg': 2,  # 10
        'wh': 2,  # 11
    }
    model = HourglassNetwork(heads=heads, **kwargs)
    model = HpDetDecode(model)
    drawer = COCODrawer()
    fns = sorted(glob(args.fn))
    for fn in tqdm(fns):
        img = cv2.imread(fn)
        letterbox_transformer = LetterboxTransformer(args.inres[0],
                                                     args.inres[1])
        pimg = letterbox_transformer(img)
        pimg = normalize_image(pimg)
        pimg = np.expand_dims(pimg, 0)
        detections = model.predict(pimg)[0]
        for d in detections:
            score, cl = d[4], d[-1]
            if score < 0.3:
                break
            x1, y1, x2, y2 = d[:4]
            kps = d[5:-1]
            kps_x = kps[:17]
            kps_y = kps[17:]
            kps = letterbox_transformer.correct_coords(
                np.vstack([kps_x, kps_y])).T
            x1, y1, x2, y2 = letterbox_transformer.correct_box(x1, y1, x2, y2)
            img = drawer.draw_pose(img, kps)
            img = drawer.draw_box(img, x1, y1, x2, y2, cl)

        out_fn = os.path.join(args.output, 'hpdet.' + os.path.basename(fn))
        cv2.imwrite(out_fn, img)
        print("Image saved to: %s" % out_fn)
 def __init__(self, model, classes_path):
     self.weights = model
     self.classes_name = _read_lines(classes_path)
     if self.classes_name is None:
         self.classes_name = coco_names
     kwargs = {
         'num_stacks': 2,
         'cnv_dim': 256,
         'weights': self.weights,
     }
     heads = {
         'hm': 80,  # 3
         'reg': 2,  # 4
         'wh': 2  # 5
     }
     model = HourglassNetwork(heads=heads, **kwargs)
     self.model = CtDetDecode(model)
     self.letterbox_transformer = LetterboxTransformer(512, 512)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--fn', default='assets/demo.jpg', type=str)
    parser.add_argument('--output', default='output', type=str)
    parser.add_argument('--inres', default='512,512', type=str)
    args, _ = parser.parse_known_args()
    args.inres = tuple(int(x) for x in args.inres.split(','))
    os.makedirs(args.output, exist_ok=True)
    kwargs = {
        'num_stacks': 2,
        'cnv_dim': 256,
        'weights': 'ctdet_coco',
        'inres': args.inres,
    }
    heads = {
        'hm': 80,  # 3
        'reg': 2,  # 4
        'wh': 2  # 5
    }
    model = HourglassNetwork(heads=heads, **kwargs)
    model = CtDetDecode(model)
    drawer = COCODrawer()
    fns = sorted(glob(args.fn))
    for fn in tqdm(fns):
        img = cv2.imread(fn)
        letterbox_transformer = LetterboxTransformer(args.inres[0],
                                                     args.inres[1])
        pimg = letterbox_transformer(img)
        pimg = normalize_image(pimg)
        pimg = np.expand_dims(pimg, 0)
        detections = model.predict(pimg)[0]
        for d in detections:
            x1, y1, x2, y2, score, cl = d
            if score < 0.3:
                break
            x1, y1, x2, y2 = letterbox_transformer.correct_box(x1, y1, x2, y2)
            img = drawer.draw_box(img, x1, y1, x2, y2, cl)

        out_fn = os.path.join(args.output, 'ctdet.' + os.path.basename(fn))
        cv2.imwrite(out_fn, img)
        print("Image saved to: %s" % out_fn)
class CENTERNET(object):
    def __init__(self, model, classes_path):
        self.weights = model
        self.classes_name = _read_lines(classes_path)
        if self.classes_name is None:
            self.classes_name = coco_names
        kwargs = {
            'num_stacks': 2,
            'cnv_dim': 256,
            'weights': self.weights,
        }
        heads = {
            'hm': 80,  # 3
            'reg': 2,  # 4
            'wh': 2  # 5
        }
        model = HourglassNetwork(heads=heads, **kwargs)
        self.model = CtDetDecode(model)
        self.letterbox_transformer = LetterboxTransformer(512, 512)

    def detect_image(self, image):
        img = pil2cv(image)
        pimg = self.letterbox_transformer(img)
        pimg = normalize_image(pimg)
        pimg = np.expand_dims(pimg, 0)
        objects = []
        detections = self.model.predict(pimg)[0]
        for d in detections:
            left, top, right, bottom, score, cl = d
            if score < 0.3:
                break
            left, top, right, bottom = self.letterbox_transformer.correct_box(
                left, top, right, bottom)
            cl = int(cl)
            predicted_class = coco_names[cl]
            objects.append({
                "bbox": [left, top, right, bottom],
                "score": np.asscalar(score),
                "class_name": predicted_class,
                "class_id": cl
            })

        return {"objects": objects}

    def close_session(self):
        pass
Exemple #5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--fn', default='assets/demo.jpg', type=str)
    parser.add_argument('--output', default='output', type=str)
    parser.add_argument('--inres', default='512,512', type=str)
    parser.add_argument('--batch-size', default=32, type=int)
    parser.add_argument('--target-classes', default=[0], nargs='*', type=int)
    parser.add_argument('--under-score', default=0.3, type=float)
    args, _ = parser.parse_known_args()
    args.inres = tuple(int(x) for x in args.inres.split(','))
    os.makedirs(args.output, exist_ok=True)
    kwargs = {
        'num_stacks': 2,
        'cnv_dim': 256,
        'weights': 'ctdet_coco',
        'inres': args.inres,
    }
    heads = {
        'hm': 80,  # 3
        'reg': 2,  # 4
        'wh': 2  # 5
    }
    model = HourglassNetwork(heads=heads, **kwargs)
    model = CtDetDecode(model)
    drawer = COCODrawer()
    fns = sorted(glob(args.fn))
    fns_iter = iter(fns)
    pred_df = []

    batch_loop_count = math.ceil(len(fns) / args.batch_size)
    print('{}/batch_size({})={} batch loop'.format(len(fns), args.batch_size,
                                                   batch_loop_count))

    for j in range(0, batch_loop_count):
        pimgs = np.empty((0, args.inres[0], args.inres[1], 3))
        fnames = []
        last_batch = len(fns) % args.batch_size if (j == batch_loop_count -
                                                    1) else args.batch_size
        for i in range(0, last_batch):
            fname = next(fns_iter)
            img = cv2.imread(fname)
            letterbox_transformer = LetterboxTransformer(
                args.inres[0], args.inres[1])
            pimg = letterbox_transformer(img)
            #      cv2.imwrite(fname.replace('.jpg', '_lb.jpg'), pimg)
            pimg = normalize_image(pimg)
            pimg = np.expand_dims(pimg, 0)
            pimgs = np.append(pimgs, pimg, axis=0)
            fnames.append(fname)
        print(pimgs.shape)

        predicts = model.predict(pimgs)
        print(predicts.shape)

        for p, fn in zip(predicts, fnames):
            img = cv2.imread(fn)
            for d in p:
                x1, y1, x2, y2, score, cl = d
                x1, y1, x2, y2 = letterbox_transformer.correct_box(
                    x1, y1, x2, y2)
                if score < args.under_score:
                    break
                if 0 == len(args.target_classes) or cl in args.target_classes:
                    img = drawer.draw_box(img, x1, y1, x2, y2, score, cl)
                    pred_df.append(
                        [os.path.basename(fn), x1, y1, x2, y2, score, cl])

            out_fn = os.path.join(args.output, 'ctdet.' + os.path.basename(fn))
            cv2.imwrite(out_fn, img)
            print("Image saved to: %s" % out_fn)

    df = pd.DataFrame(pred_df,
                      columns=['fname', 'x1', 'y1', 'x2', 'y2', 'score', 'cl'])
    df['cl'] = df['cl'].astype(int)
    out_df = os.path.join(args.output, 'ctdet.csv')
    df.to_csv(out_df, index=False)
    print('output csv {} size: {}'.format(out_df, len(pred_df)))
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--output', default='output', type=str)
    parser.add_argument('--data', default='val2017', type=str)
    parser.add_argument('--annotations', default='annotations', type=str)
    parser.add_argument('--inres', default='512,512', type=str)
    parser.add_argument('--no-full-resolution', action='store_true')
    args, _ = parser.parse_known_args()
    args.inres = tuple(int(x) for x in args.inres.split(','))
    if not args.no_full_resolution:
        args.inres = (None, None)

    os.makedirs(args.output, exist_ok=True)
    kwargs = {
        'num_stacks': 2,
        'cnv_dim': 256,
        'weights': 'hpdet_coco',
        'inres': args.inres,
    }
    heads = {
        'hm': 1,  # 6
        'hm_hp': 17,  # 7
        'hp_offset': 2,  # 8
        'hps': 34,  # 9
        'reg': 2,  # 10
        'wh': 2,  # 11
    }
    out_fn_keypoints = os.path.join(
        args.output, args.data + '_keypoints_results_%s_%s.json' %
        (args.inres[0], args.inres[1]))
    model = HourglassNetwork(heads=heads, **kwargs)
    model = HpDetDecode(model)
    if args.no_full_resolution:
        letterbox_transformer = LetterboxTransformer(args.inres[0],
                                                     args.inres[1])
    else:
        letterbox_transformer = LetterboxTransformer(mode='testing',
                                                     max_stride=128)

    fns = sorted(glob(os.path.join(args.data, '*.jpg')))
    results = []
    for fn in tqdm(fns):
        img = cv2.imread(fn)
        image_id = int(os.path.splitext(os.path.basename(fn))[0])
        pimg = letterbox_transformer(img)
        pimg = normalize_image(pimg)
        pimg = np.expand_dims(pimg, 0)
        detections = model.predict(pimg)[0]
        for d in detections:
            score = d[4]
            x1, y1, x2, y2 = d[:4]
            x1, y1, x2, y2 = letterbox_transformer.correct_box(x1, y1, x2, y2)
            x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)

            kps = d[5:-1]
            kps_x = kps[:17]
            kps_y = kps[17:]
            kps = letterbox_transformer.correct_coords(
                np.vstack([kps_x, kps_y])).T
            # add z = 1
            kps = np.concatenate([kps, np.ones((17, 1), dtype='float32')], -1)
            kps = list(map(float, kps.flatten()))

            image_result = {
                'image_id': image_id,
                'category_id': 1,
                'score': float(score),
                'bbox': [x1, y1, (x2 - x1), (y2 - y1)],
                'keypoints': kps,
            }
            results.append(image_result)

    if not len(results):
        print("No predictions were generated.")
        return

    # write output
    with open(out_fn_keypoints, 'w') as f:
        json.dump(results, f, indent=2)
    print("Predictions saved to: %s" % out_fn_keypoints)
    # load results in COCO evaluation tool
    gt_fn = os.path.join(args.annotations,
                         'person_keypoints_%s.json' % args.data)
    print("Loading GT: %s" % gt_fn)
    coco_true = COCO(gt_fn)
    coco_pred = coco_true.loadRes(out_fn_keypoints)
    coco_eval = COCOeval(coco_true, coco_pred, 'keypoints')
    coco_eval.evaluate()
    coco_eval.accumulate()
    coco_eval.summarize()

    return coco_eval.stats
Exemple #7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--fn',
                        default='train_images/ID_001a12fb2.jpg',
                        type=str)
    parser.add_argument('--output', default='output', type=str)
    parser.add_argument('--inres', default='512,512', type=str)
    args, _ = parser.parse_known_args()
    args.inres = tuple(int(x) for x in args.inres.split(','))
    os.makedirs(args.output, exist_ok=True)
    kwargs = {
        'num_stacks': 2,
        'cnv_dim': 256,
        'weights': 'ctdet_coco',
        'inres': args.inres,
    }
    heads = {
        'hm': 80,  # 3
        'reg': 2,  # 4
        'wh': 2  # 5
    }
    model = HourglassNetwork(heads=heads, **kwargs)
    model = CtDetDecode(model)
    drawer = COCODrawer()
    fns = sorted(glob(args.fn))
    for fn in tqdm(fns):
        img = cv2.imread(fn)
        letterbox_transformer = LetterboxTransformer(args.inres[0],
                                                     args.inres[1])
        pimg = letterbox_transformer(img)
        pimg = normalize_image(pimg)
        pimg = np.expand_dims(pimg, 0)
        detections = model.predict(pimg)[0]

        train_matrix = train_2_matrix(
            os.path.splitext(os.path.basename(fn))[0])
        #train_matrix = train_2_matrix('ID_679502721')
        x_img = [0] * len(train_matrix[0])
        y_img = [0] * len(train_matrix[0])
        for car in range(len(train_matrix[0])):
            x_img[car] = get_img_coords(train_matrix[4][car],
                                        train_matrix[5][car],
                                        train_matrix[6][car])[0]
            y_img[car] = get_img_coords(train_matrix[4][car],
                                        train_matrix[5][car],
                                        train_matrix[6][car])[1]

        train_matrix = np.append(train_matrix, [x_img], axis=0)
        train_matrix = np.append(train_matrix, [y_img], axis=0)

        i = 1
        for d in detections:
            completeName = os.path.join(
                'output/',
                os.path.splitext(os.path.basename(fn))[0] + '_' + str(i) +
                '.txt')

            if os.path.exists(completeName):
                os.remove(completeName)

            x1, y1, x2, y2, score, cl = d
            if score < 0.3:
                break

            cl = int(cl)
            name = coco_names[cl].split()[-1]
            if name != 'car':
                continue

            x1, y1, x2, y2 = letterbox_transformer.correct_box(x1, y1, x2, y2)
            #img = drawer.draw_box(img, x1, y1, x2, y2, cl)

            x_center = (x2 - x1) / 2 + x1
            y_center = (y2 - y1) / 2 + y1
            car_index = 0
            faulty = True
            dist_x = math.inf
            dist_y = math.inf
            for car in range(len(train_matrix[0])):
                if abs(train_matrix[7][car] -
                       x_center) < 10000 / train_matrix[6][car] and abs(
                           train_matrix[8][car] -
                           y_center) < 10000 / train_matrix[6][car]:
                    faulty = False
                    if abs(train_matrix[7][car] - x_center) < dist_x and abs(
                            train_matrix[8][car] - y_center) < dist_y:
                        dist_x = abs(train_matrix[7][car] - x_center)
                        dist_y = abs(train_matrix[8][car] - y_center)
                        car_index = car
            if (faulty):
                continue

            f = open(completeName, "w+")

            croppedIm = img[int(y1):int(y2), int(x1):int(x2)]
            #out_fn = os.path.join(args.output,  str(i) + '_' + 'ctdet.' + os.path.basename(fn))
            out_fn = os.path.join(
                args.output,
                os.path.splitext(os.path.basename(fn))[0] + '_' + str(i) +
                '.jpg')
            cv2.imwrite(out_fn, croppedIm)

            f.write(str(x2 - x1) + '\n')
            f.write(str(y2 - y1) + '\n')
            f.write(str((x2 - x1) / (y2 - y1)) + '\n')
            f.write(str((x2 - x1) * (y2 - y1)) + '\n')
            f.write(str(x_center) + '\n')
            f.write(str(y_center) + '\n')
            f.write(
                str(
                    math.sqrt(
                        math.pow((y2 - y1) / 2 + y1 - len(img[0]) / 2, 2) +
                        math.pow((x2 - x1) / 2 + x1 - len(img), 2))) + '\n')
            f.write(
                str(
                    math.atan2((y2 - y1) / 2 + y1 - len(img[0]) / 2,
                               (x2 - x1) / 2 + x1 - len(img))) + '\n')

            if ((x2 - x1) / 2 + x1 < len(img)):
                f.write('1\n0')
            else:
                f.write('0\n1')

            f.write('\n\n')
            f.write(str(train_matrix[0][car_index]) + '\n')
            f.write(str(train_matrix[1][car_index]) + '\n')
            f.write(str(train_matrix[2][car_index]) + '\n')
            f.write(str(train_matrix[3][car_index]) + '\n')
            f.write(str(train_matrix[4][car_index]) + '\n')
            f.write(str(train_matrix[5][car_index]) + '\n')
            f.write(str(train_matrix[6][car_index]) + '\n')

            i += 1

        #out_fn = os.path.join(args.output, 'ctdet.' + os.path.basename(fn))
        out_fn = os.path.join(args.output, os.path.basename(fn))
        cv2.imwrite(out_fn, img)
        print("Image saved to: %s" % out_fn)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--output', default='output', type=str)
    parser.add_argument('--data', default='val2017', type=str)
    parser.add_argument('--annotations', default='annotations', type=str)
    parser.add_argument('--inres', default='512,512', type=str)
    parser.add_argument('--no-full-resolution', action='store_true')
    args, _ = parser.parse_known_args()
    args.inres = tuple(int(x) for x in args.inres.split(','))
    if not args.no_full_resolution:
        args.inres = (None, None)

    os.makedirs(args.output, exist_ok=True)
    kwargs = {
        'num_stacks': 2,
        'cnv_dim': 256,
        'weights': 'ctdet_coco',
        'inres': args.inres,
    }
    heads = {'hm': 80, 'reg': 2, 'wh': 2}
    out_fn_box = os.path.join(
        args.output, args.data + '_bbox_results_%s_%s.json' %
        (args.inres[0], args.inres[1]))
    model = HourglassNetwork(heads=heads, **kwargs)
    model = CtDetDecode(model)
    if args.no_full_resolution:
        letterbox_transformer = LetterboxTransformer(args.inres[0],
                                                     args.inres[1])
    else:
        letterbox_transformer = LetterboxTransformer(mode='testing',
                                                     max_stride=128)

    fns = sorted(glob(os.path.join(args.data, '*.jpg')))
    results = []
    for fn in tqdm(fns):
        img = cv2.imread(fn)
        image_id = int(os.path.splitext(os.path.basename(fn))[0])
        pimg = letterbox_transformer(img)
        pimg = normalize_image(pimg)
        pimg = np.expand_dims(pimg, 0)
        detections = model.predict(pimg)[0]
        for d in detections:
            x1, y1, x2, y2, score, cl = d
            # if score < 0.001:
            #   break
            x1, y1, x2, y2 = letterbox_transformer.correct_box(x1, y1, x2, y2)
            cl = int(cl)
            x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)
            image_result = {
                'image_id': image_id,
                'category_id': COCO_IDS[cl + 1],
                'score': float(score),
                'bbox': [x1, y1, (x2 - x1), (y2 - y1)],
            }
            results.append(image_result)

    if not len(results):
        print("No predictions were generated.")
        return

    # write output
    with open(out_fn_box, 'w') as f:
        json.dump(results, f, indent=2)
    print("Predictions saved to: %s" % out_fn_box)
    # load results in COCO evaluation tool
    gt_fn = os.path.join(args.annotations, 'instances_%s.json' % args.data)
    print("Loading GT: %s" % gt_fn)
    coco_true = COCO(gt_fn)
    coco_pred = coco_true.loadRes(out_fn_box)

    # run COCO evaluation
    coco_eval = COCOeval(coco_true, coco_pred, 'bbox')
    coco_eval.evaluate()
    coco_eval.accumulate()
    coco_eval.summarize()
    return coco_eval.stats
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('--video', default='webcam', type=str)
  parser.add_argument('--output', default='output', type=str)
  parser.add_argument('--inres', default='512,512', type=str)
  parser.add_argument('--outres', default='1080,1920', type=str)
  parser.add_argument('--max-frames', default=1000000, type=int)
  parser.add_argument('--fps', default=25.0 * 1.0, type=float)
  args, _ = parser.parse_known_args()
  args.inres = tuple(int(x) for x in args.inres.split(','))
  args.outres = tuple(int(x) for x in args.outres.split(','))
  os.makedirs(args.output, exist_ok=True)
  kwargs = {
    'num_stacks': 2,
    'cnv_dim': 256,
    'weights': 'ctdet_coco',
    'inres': args.inres,
  }
  heads = {
    'hm': 80,  # 3
    'reg': 2,  # 4
    'wh': 2  # 5
  }
  model = HourglassNetwork(heads=heads, **kwargs)
  model = CtDetDecode(model)
  drawer = COCODrawer()
  letterbox_transformer = LetterboxTransformer(args.inres[0], args.inres[1])
  cap = cv2.VideoCapture(0 if args.video == 'webcam' else args.video)
  out_fn = os.path.join(args.output, 'ctdet.' + os.path.basename(args.video)).replace('.mp4', '.avi')
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
  out = cv2.VideoWriter(out_fn, fourcc, args.fps, args.outres[::-1])
  k = 0
  tic = time.time()
  while cap.isOpened():
    if k > args.max_frames:
      print("Bye")
      break
    if k > 0 and k % 100 == 0:
      toc = time.time()
      duration = toc - tic
      print("[%05d]: %.3f seconds / 100 iterations" % (k, duration))
      tic = toc

    k += 1
    ret, img = cap.read()
    if not ret:
      print("Done")
      break
    pimg = letterbox_transformer(img)
    pimg = normalize_image(pimg)
    pimg = np.expand_dims(pimg, 0)
    detections = model.predict(pimg)[0]
    for d in detections:
      x1, y1, x2, y2, score, cl = d
      if score < 0.3:
        break
      x1, y1, x2, y2 = letterbox_transformer.correct_box(x1, y1, x2, y2)
      img = drawer.draw_box(img, x1, y1, x2, y2, cl)

    out.write(img)
  print("Video saved to: %s" % out_fn)