def Apply(self):
        upsample_size = [
            int(self.image.shape[0] / 8 * PoseThinpose.resize_out_ratio),
            int(self.image.shape[1] / 8 * PoseThinpose.resize_out_ratio)
        ]

        request = predict_pb2.PredictRequest()
        request.model_spec.name = 'pose_thinpose'
        request.model_spec.signature_name = 'predict_images'
        request.inputs['tensor_image'].CopyFrom(
            tf.contrib.util.make_tensor_proto(self.image,
                                              shape=[1, 232, 217, 3],
                                              dtype=np.float32))
        request.inputs['upsample_size'].CopyFrom(
            tf.contrib.util.make_tensor_proto(upsample_size,
                                              shape=[2],
                                              dtype=np.int32))

        result = self.istub.Predict(request, 10.0)  # 10 secs timeout

        tensor_heatMat_up = tensor_util.MakeNdarray(
            result.outputs['tensor_heatMat_up'])
        tensor_pafMat_up = tensor_util.MakeNdarray(
            result.outputs['tensor_pafMat_up'])
        tensor_peaks = tensor_util.MakeNdarray(result.outputs['tensor_peaks'])

        peaks = tensor_peaks[0]
        heatMat = tensor_heatMat_up[0]
        pafMat = tensor_pafMat_up[0]

        self.humans = PoseEstimator.estimate_paf(peaks, heatMat, pafMat)
Exemplo n.º 2
0
def main(_):
    channel = grpc.insecure_channel(FLAGS.server)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'tf_openpose'
    request.model_spec.signature_name = 'predict_images'

    image_path = '/home/yitao/Documents/fun-project/tensorflow-related/tf-pose-estimation/images/p1.jpg'
    resize_out_ratio = 4.0

    runNum = 15
    humans = None
    durationSum = 0.0
    runCount = 0

    for i in range(runNum):
        start = time.time()

        image = common.read_imgfile(image_path, None, None)
        upsample_size = [
            int(image.shape[0] / 8 * resize_out_ratio),
            int(image.shape[1] / 8 * resize_out_ratio)
        ]

        # print(upsample_size)

        request.inputs['tensor_image'].CopyFrom(
            tf.contrib.util.make_tensor_proto(image,
                                              shape=[1, 232, 217, 3],
                                              dtype=np.float32))
        request.inputs['upsample_size'].CopyFrom(
            tf.contrib.util.make_tensor_proto(upsample_size,
                                              shape=[2],
                                              dtype=np.int32))

        result = stub.Predict(request, 10.0)  # 10 secs timeout
        tensor_heatMat_up = tensor_util.MakeNdarray(
            result.outputs['tensor_heatMat_up'])
        tensor_pafMat_up = tensor_util.MakeNdarray(
            result.outputs['tensor_pafMat_up'])
        tensor_peaks = tensor_util.MakeNdarray(result.outputs['tensor_peaks'])

        peaks = tensor_peaks[0]
        heatMat = tensor_heatMat_up[0]
        pafMat = tensor_pafMat_up[0]

        humans = PoseEstimator.estimate_paf(peaks, heatMat, pafMat)

        end = time.time()
        duration = (end - start)
        print("it takes %s sec" % str(duration))

        if (i > 10):
            runCount += 1
            durationSum += duration

    print("On average, it takes %f sec over %d runs." %
          (durationSum / runCount, runCount))
    print(humans)
Exemplo n.º 3
0
    def inference(self, npimg):
        def add_batch_dim(np_im):
            return np_im[None] if np_im.ndim == 3 else np_im

        output = self.forward_fn(
            add_batch_dim(self._get_scaled_img(npimg, None)[0][0]))

        peaks, heatMat_up, pafMat_up = self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_output: output,
                self.upsample_size: self.UPSAMPLE_SIZE
            })

        return PoseEstimator.estimate_paf(peaks[0], heatMat_up[0],
                                          pafMat_up[0])
Exemplo n.º 4
0
    def Apply(self, batched_data_dict, batch_size, istub):
        if (batch_size != len(batched_data_dict["upsample_size"])):
            print("[Error] Apply() batch size not matched...")
            return None
        else:
            batched_result_dict = dict()

            request = predict_pb2.PredictRequest()
            request.model_spec.name = 'pose_openpose'
            request.model_spec.signature_name = 'predict_images'
            request.inputs['tensor_image'].CopyFrom(
                tf.contrib.util.make_tensor_proto(
                    batched_data_dict["client_input"],
                    shape=batched_data_dict["client_input"].shape))
            request.inputs['upsample_size'].CopyFrom(
                tf.contrib.util.make_tensor_proto(
                    batched_data_dict["upsample_size"][0],
                    shape=[2],
                    dtype=np.int32))

            result = istub.Predict(request, 10.0)  # 10 secs timeout

            tensor_heatMat_up = tensor_util.MakeNdarray(
                result.outputs['tensor_heatMat_up'])
            tensor_pafMat_up = tensor_util.MakeNdarray(
                result.outputs['tensor_pafMat_up'])
            tensor_peaks = tensor_util.MakeNdarray(
                result.outputs['tensor_peaks'])

            # print(tensor_heatMat_up.shape)

            humans_array = []
            for i in range(batch_size):
                peaks = tensor_peaks[i]
                heatMat = tensor_heatMat_up[i]
                pafMat = tensor_pafMat_up[i]

                humans = PoseEstimator.estimate_paf(peaks, heatMat, pafMat)
                humans_array.append(humans)

            batched_result_dict["humans"] = humans_array

            return batched_result_dict
    logger.debug('cam read+')
    cam = cv2.VideoCapture(args.camera)
    ret_val, image = cam.read()
    logger.info('cam image=%dx%d' % (image.shape[1], image.shape[0]))

    while True:
        ret_val, image = cam.read()

        im = cv2.resize(image, (432, 368), interpolation=cv2.INTER_AREA)
        im = im.reshape((1, 368, 432, 3))
        im = im.astype(np.float32, copy=False)

        logger.debug('image process+')
        peaks, heatMat_up, pafMat_up = run_test(im, tflite_model_file)
        humans = PoseEstimator.estimate_paf(peaks[0], heatMat_up[0],
                                            pafMat_up[0])

        logger.debug('postprocess+')
        image = draw_humans(image, humans, imgcopy=False)

        logger.debug('show+')
        cv2.putText(image, "FPS: %f" % (1.0 / (time.time() - fps_time)),
                    (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        cv2.imshow('tf-pose-estimation result', image)
        fps_time = time.time()
        if cv2.waitKey(1) & 0xFF == ord('a'):
            break
        logger.debug('finished+')

    cv2.destroyAllWindows()
Exemplo n.º 6
0
    def inference(self, npimg, resize_to_default=True, upsample_size=1.0):
        if npimg is None:
            raise Exception(
                'The image is not valid. Please check your image exists.')

        if resize_to_default:
            upsample_size = [
                int(self.target_size[1] / 8 * upsample_size),
                int(self.target_size[0] / 8 * upsample_size)
            ]
        else:
            upsample_size = [
                int(npimg.shape[0] / 8 * upsample_size),
                int(npimg.shape[1] / 8 * upsample_size)
            ]

        #if self.tensor_image.dtype == tf.quint8:
        #    # quantize input image
        #    npimg = TfPoseEstimator._quantize_img(npimg)
        #    pass

        logger.debug('inference+ original shape=%dx%d' %
                     (npimg.shape[1], npimg.shape[0]))
        img = npimg
        if resize_to_default:
            img = self._get_scaled_img(npimg, None)[0][0]
        logger.debug('img shape : {}'.format(np.shape(img)))
        #output = self.persistent_sess.run(
        #    [self.tensor_output], feed_dict={
        #        self.tensor_image: [img]
        #    }
        #)

        output = self.engine.infer(np.expand_dims(img, axis=1))
        logger.info('generated output {}'.format(np.shape(output)))
        output = output[0].reshape([57, 20, 34, 1]).transpose([3, 1, 2, 0])

        logger.info('generated output {}'.format(np.shape(output)))
        peaks, heatMat_up, pafMat_up = self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_output: output,
                self.upsample_size: upsample_size
            })
        logger.info('generated peaks')
        peaks = peaks[0]
        self.heatMat = heatMat_up[0]
        self.pafMat = pafMat_up[0]

        for i in range(19):
            arr_ = np.squeeze(heatMat_up[0][:, :, i])
            logger.debug('MAX : {} MIN : {}, MEDIAN : {}'.format(
                np.max(arr_), np.min(arr_), np.median(arr_)))
            cv2.imwrite('/home/nvidia/Downloads/heatmap_{}.jpg'.format(i),
                        arr_)

        logger.debug('inference- heatMat=%dx%d pafMat=%dx%d' %
                     (self.heatMat.shape[1], self.heatMat.shape[0],
                      self.pafMat.shape[1], self.pafMat.shape[0]))

        t = time.time()
        humans = PoseEstimator.estimate_paf(peaks, self.heatMat, self.pafMat)
        logger.debug('estimate time=%.5f' % (time.time() - t))
        return humans