Exemplo n.º 1
0
    def processImage(self, img, scale):
        print("llega imagen ", img.width, scale)
        scale = 0.7
        self.src = np.frombuffer(img.image,
                                 np.uint8).reshape(img.height, img.width,
                                                   img.depth)
        image = cv2.resize(self.src, None, fx=scale, fy=scale)
        #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        processed_image_cpu = transforms.image_transform(image.copy())
        processed_image = processed_image_cpu.contiguous().to(
            non_blocking=True)
        unsqueezed = torch.unsqueeze(processed_image, 0).to(self.args.device)
        fields = self.processor.fields(unsqueezed)[0]
        keypoint_sets, _ = self.processor.keypoint_sets(fields)
        #print("keyPoints", keypoint_sets)

        # # save in ice structure
        people = []
        for p in keypoint_sets:
            joints = {}
            person = Person()
            for pos, joint in enumerate(p):
                keypoint = KeyPoint()
                keypoint.x = joint[0] / scale
                keypoint.y = joint[1] / scale
                keypoint.score = joint[2]
                joints[COCO_IDS[pos]] = keypoint
            person.id = 0
            person.joints = joints
            people.append(person)
        return people
Exemplo n.º 2
0
def webcam(args):

    # add args.device
    args.device = torch.device('cpu')
    if torch.cuda.is_available():
        args.device = torch.device('cuda')

    # load models
    args.camera = True
    pifpaf = PifPaf(args)
    monoloco = MonoLoco(model_path=args.model, device=args.device)

    # Start recording
    cam = cv2.VideoCapture(0)
    visualizer_monoloco = None

    while True:
        start = time.time()
        ret, frame = cam.read()
        image = cv2.resize(frame, None, fx=args.scale, fy=args.scale)
        height, width, _ = image.shape
        print('resized image size: {}'.format(image.shape))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        processed_image_cpu = transforms.image_transform(image.copy())
        processed_image = processed_image_cpu.contiguous().to(
            args.device, non_blocking=True)
        fields = pifpaf.fields(torch.unsqueeze(processed_image, 0))[0]
        _, _, pifpaf_out = pifpaf.forward(image, processed_image_cpu, fields)

        if not ret:
            break
        key = cv2.waitKey(1)

        if key % 256 == 27:
            # ESC pressed
            print("Escape hit, closing...")
            break
        pil_image = Image.fromarray(image)
        intrinsic_size = [xx * 1.3 for xx in pil_image.size]
        kk, dict_gt = factory_for_gt(
            intrinsic_size)  # better intrinsics for mac camera
        if visualizer_monoloco is None:
            visualizer_monoloco = VisualizerMonoloco(kk, args)(pil_image)
            visualizer_monoloco.send(None)

        if pifpaf_out:
            boxes, keypoints = preprocess_pifpaf(pifpaf_out, (width, height))
            outputs, varss = monoloco.forward(keypoints, kk)
            dic_out = monoloco.post_process(outputs, varss, boxes, keypoints,
                                            kk, dict_gt)
            visualizer_monoloco.send((pil_image, dic_out))
            end = time.time()
        print("run-time: {:.2f} ms".format((end - start) * 1000))

    cam.release()

    cv2.destroyAllWindows()
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    nets.cli(parser)
    decoder.cli(parser, force_complete_pose=False, instance_threshold=0.1, seed_threshold=0.5)
    parser.add_argument('--no-colored-connections',
                        dest='colored_connections', default=True, action='store_false',
                        help='do not use colored connections to draw poses')
    parser.add_argument('--disable_cuda', action='store_true', default=None,
                        help='disable CUDA')
    args = parser.parse_args()

    # add args.device
    args.device = torch.device('cpu')
    if not args.disable_cuda and torch.cuda.is_available():
        print('************************ using gpu *****************************')
        args.device = torch.device('cuda')

    # load model
    model, _ = nets.factory_from_args(args)
    model = model.to(args.device)
    processor = decoder.factory_from_args(args, model)

    # own coco val json
    f = open('/home/chenjia/pedestrian_det/chenjiaPifPaf/splitjson/test_5000.json')
    js = ujson.load(f)
    img_paths = js['images']   # len==5000, img的相对路径
    img_path_root = '/data/nfs_share/public/zxyang/human_data/detection_data_cpu'
    out_root = '/home/chenjia/pedestrian_det/openpifpaf/show_eval'

    # random check pred result
    for i in range(50):
        print('*************** run the ', i + 1, 'image ******************')
        ind = np.random.randint(0, 4999)
        img_path = os.path.join(img_path_root, img_paths[ind]['file_name'])
        img = cv2.imread(img_path)
        img = cv2.resize(img, (683, 384))

        image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        processed_image_cpu = transforms.image_transform(image.copy())   # normalize

        processed_image = processed_image_cpu.contiguous().to(args.device, non_blocking=True)  # transpose 2,0,1
        fields = processor.fields(torch.unsqueeze(processed_image, 0))[0]
        keypoint_sets, _ = processor.keypoint_sets(fields)

        # plot pred result
        plot_points(image, keypoint_sets, COCO_PERSON_SKELETON)
        cv2.imwrite(os.path.join(out_root, str(ind) + '.jpg'), image)
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    nets.cli(parser)
    decoder.cli(parser, force_complete_pose=False, instance_threshold=0.05)
    parser.add_argument('--no-colored-connections',
                        dest='colored_connections',
                        default=True,
                        action='store_false',
                        help='do not use colored connections to draw poses')
    parser.add_argument('--disable-cuda',
                        action='store_true',
                        help='disable CUDA')
    parser.add_argument(
        '--source',
        default=0,
        help='OpenCV source url. Integer for webcams. Or ipwebcam streams.')
    parser.add_argument('--scale',
                        default=0.1,
                        type=float,
                        help='input image scale factor')
    args = parser.parse_args()

    # check whether source should be an int
    if len(args.source) == 1:
        args.source = int(args.source)

    # add args.device
    args.device = torch.device('cpu')
    if not args.disable_cuda and torch.cuda.is_available():
        args.device = torch.device('cuda')

    # load model
    model, _ = nets.factory(args)
    model = model.to(args.device)
    processors = decoder.factory(args, model)

    last_loop = time.time()
    capture = cv2.VideoCapture(args.source)

    visualizers = None
    while True:
        ret, image_original = capture.read()
        print(ret)
        image = cv2.resize(image_original, None, fx=args.scale, fy=args.scale)
        print('resized image size: {}'.format(image.shape))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        if visualizers is None:
            visualizers = [Visualizer(p, args)(image) for p in processors]
            for v in visualizers:
                v.send(None)

        start = time.time()
        processed_image_cpu = transforms.image_transform(image.copy())
        processed_image = processed_image_cpu.contiguous().to(
            args.device, non_blocking=True)
        print('preprocessing time', time.time() - start)

        fields = processors[0].fields(torch.unsqueeze(processed_image, 0))[0]
        for v in visualizers:
            v.send((image, fields))

        print('loop time = {:.3}s, FPS = {:.3}'.format(
            time.time() - last_loop, 1.0 / (time.time() - last_loop)))
        last_loop = time.time()
Exemplo n.º 5
0
def main():
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    nets.cli(parser)
    decoder.cli(parser, force_complete_pose=False, instance_threshold=0.05)
    parser.add_argument('--no-colored-connections',
                        dest='colored_connections', default=True, action='store_false',
                        help='do not use colored connections to draw poses')
    parser.add_argument('--disable-cuda', action='store_true',
                        help='disable CUDA')
    parser.add_argument('--source', default=0,
                        help='OpenCV source url. Integer for webcams. Or ipwebcam streams.')
    parser.add_argument('--scale', default=0.1, type=float,
                        help='input image scale factor')
    args = parser.parse_args()

    # check whether source should be an int
    if len(args.source) == 1:
        args.source = int(args.source)

    # add args.device
    args.device = torch.device('cpu')
    if not args.disable_cuda and torch.cuda.is_available():
        args.device = torch.device('cuda')

    # load model
    model, _ = nets.factory(args)
    model = model.to(args.device)
    processors = decoder.factory(args, model)
    

    print(args)

    last_loop = time.time()
    capture = cv2.VideoCapture(args.source)

    visualizers = None
    while True:
        ret , image_original = capture.read()
        print(ret)
        image = cv2.resize(image_original, None, fx=args.scale, fy=args.scale)
        print('resized image size: {}'.format(image.shape))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        if visualizers is None:
            visualizers = [Visualizer(p, args)(image) for p in processors]
            for v in visualizers:
                v.send(None)

        start = time.time()
        processed_image_cpu = transforms.image_transform(image.copy())
        processed_image = processed_image_cpu.contiguous().to(args.device, non_blocking=True)
        print('preprocessing time', time.time() - start)

        fields = processors[0].fields(torch.unsqueeze(processed_image, 0))[0]
        for v in visualizers:
            v.send((image, fields))

        print('loop time = {:.3}s, FPS = {:.3}'.format(
            time.time() - last_loop, 1.0 / (time.time() - last_loop)))
        last_loop = time.time()