python使用importlib进行动态导入py文件

python动态导入py文件

import importlib

def dynamic_import(module):
    return importlib.import_module(module)

实例

import importlib
import cv2

def dynamic_import(module):
    return importlib.import_module(module)


class OpenCVAlgo:
    def __init__(self, **kwargs):
        self.uuid = kwargs['uuid']
        self.openCVPath = kwargs['openCVPath']
        self.algo = dynamic_import(self.openCVPath)


    def predict(self,cvImg):
        res = self.algo.func(cvImg)
        return res




if __name__ == '__main__':
    test = dynamic_import(r"testCV")

    parms = {
        "uuid": 1,
        "openCVPath": r"testCV"}
    # # # 创建算法实例
    OpenCV_alg = OpenCVAlgo(**parms)
    img = cv2.imread(r"D:\dataset\DogCat-seg\images\train\1.jpg")
    res = OpenCV_alg.predict(img)
    # showImg(res)
import cv2

def func(img):
    print(img.shape)
    img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    print(img.shape)
    return img

if __name__ == '__main__':
    img = cv2.imread(r"D:\test.jpg")
    func(img)

你可能感兴趣的:(python,开发语言,opencv)