代码之家  ›  专栏  ›  技术社区  ›  mad

如何将参数为参数的函数传递给另一个字符串为函数名的python函数?

  •  1
  • mad  · 技术社区  · 7 年前

    我在main.py中有以下源代码

    Me.Py

    import data_utils as du
    
    X_train1, Y_train1, groundtruth_train1= du.loaddata(train_file, "adjust_gamma", 0.8)
    X_train2, Y_train2, groundtruth_train2= du.loaddata(train_file, "adjust_gamma", 1.2)
    X_train3, Y_train3, groundtruth_train3= du.loaddata(train_file, "scale_image", 0.5)
    X_train4, Y_train4, groundtruth_train4= du.loaddata(train_file, "scale_image", 0.8)
    X_train5, Y_train5, groundtruth_train5= du.loaddata(train_file, "scale_image", 1.5)
    X_train6, Y_train6, groundtruth_train6= du.loaddata(train_file, "scale_image", 2.0)
    X_train7, Y_train7, groundtruth_train7= du.loaddata(train_file, "compress_jpeg", 70)
    X_train8, Y_train8, groundtruth_train8= du.loaddata(train_file, "compress_jpeg", 90)
    

    main.py将读取几个图像,应用图像转换,将它们划分为块(这些是我的x_训练输出),并获取图像标签(y_训练和groundtrust_训练)。图像转换由带参数的字符串定义(例如“Adjust_Gamma”等)。

    现在,load_data.py是基于这个答案构建的。 about how to pass functions with arguments to another function

    DATAUTULYS.PY

    def loaddata(file_images, function_name, parameter):
    
    x = []
    y = []
    
    with open(file_images) as f:
        images_names = f.readlines()
        images_names = [a.strip() for a in images_names]
    
        j=0
    
        for line in images_names:
    
            j=j+1
    
            line='image_folder/' + line
            img = cv2.imread(img_path)
            img=perform(eval(function_name)(img,parameter)) 
            ...
    

    执行的函数将接收函数名(如在main.py中看到的字符串)及其参数(numpy数组中的图像和参数)。执行的功能详细如下:

    def perform(fun, *args):
    fun(*args)
    
    def adjust_gamma(image, gamma):
       invGamma = 1.0 / gamma
       table = np.array([((i / 255.0) ** invGamma) * 255
          for i in np.arange(0, 256)]).astype("uint8")
       return cv2.LUT(image, table)
    
    def compress_jpeg(image, compression_factor):
       encode_param=[int(cv2.IMWRITE_JPEG_QUALITY), compression_factor]
       result, encimg=cv2.imencode('.jpg', image, encode_param)
       decimg=cv2.imdecode(encimg, 1)
       return decimg
    
    def scale_image(image, scaling_factor):
       resized_image=cv2.resize(image, (scaling_factor, scaling_factor))
       return resized_image 
    

    我尝试使用eval函数,以便传递的字符串可以被视为函数名( This answer inspired me to do that )但是,当我运行代码时,会得到以下错误:

    文件 “My.Py”, 第32行,做实验

    x train1,y train1,groundtrust train1=du.loaddata(train file,“adjust gamma”,0.8)

    文件 “数据实用程序.py”, 第29行,在LoadData中

    img=执行(eval(函数名)(img,参数))文件“data_utils.py”,

    第171行,执行中 fun(*args)typeerror:“numpy.ndarray”对象不可调用

    那么,我该如何解决我的问题呢?如何使用函数名作为字符串将函数作为参数传递给另一个函数?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mehrdad Pedramfar    7 年前

    替换 eval(function_name)(img,parameter) 用:

    globals()[function_name](img,parameter)
    

    请注意,您想要的功能应该在我的答案中的同一个模块中,如果不是,请阅读 this link This 关于 globals locals 在python中找到解决问题的最佳方法。

    此外,您还可以使用 getattr ,像这样:

    getattr(module, func)(*args, **kwargs)