import paddle.inference as paddle_infer
import numpy as np
import paddle.vision.transforms as T
class Lane_model_infer:
    # def __init__(self, model_dir="./lane_model/7_12_6"):
    def __init__(self, model_dir="./lane_model/6_9"):
        # 初始化 paddle 推理
        self.model_dir = model_dir
        self.config = paddle_infer.Config(model_dir + "/model.pdmodel", model_dir + "/model.pdiparams")
        self.config.disable_glog_info()
        self.config.enable_use_gpu(200, 0)
        # self.config.enable_memory_optim(True)
        # self.config.switch_ir_optim(True)
        # self.config.switch_use_feed_fetch_ops(False)
        # self.config.delete_pass("conv_elementwise_add_act_fuse_pass")
        # self.config.delete_pass("conv_elementwise_add_fuse_pass")
        self.predictor = paddle_infer.create_predictor(self.config)
        self.input_names = self.predictor.get_input_names()
        self.input_handle = self.predictor.get_input_handle(self.input_names[0])
        self.output_names = self.predictor.get_output_names()
        self.output_handle = self.predictor.get_output_handle(self.output_names[0])
        self.normalize_transform = T.Normalize(mean=[127.5], std=[127.5])
        # print(self.config.summary())
    def infer(self,src) -> np.ndarray:
        # 7_12_3 [60:240,:]
        # crop_src = src[60:240,:]
        # image = self.normalize_transform(crop_src)
        image = self.normalize_transform(src)
        image = image.transpose(2, 0, 1)
        image = np.expand_dims(image, axis=0)
        self.input_handle.copy_from_cpu(image)
        self.predictor.run()
        results = self.output_handle.copy_to_cpu()[0]
        return results

# if __name__ == "__main__":
#     predictor = Lane_model_infer()
#     import time
#     while True:
#         time.sleep(1)
#         print('123')