【问题标题】:python max function asks for src2 TypeError: Required argument 'src2' (pos 2) not foundpython max 函数要求 src2 TypeError: 找不到所需的参数 'src2' (pos 2)
【发布时间】:2018-11-28 08:25:22
【问题描述】:

我正在使用 Python 3.5.3 编写用于数据集准备的小脚本。它检查文件夹中的现有文件,如果有已经处理过的文件,它会找到该文件的最大索引,从下一个开始。

我被困在 max 函数上,由于某些原因需要 src2 参数,但是它是没必要。

这是我的代码:

from cv2.cv2 import *
import numpy as np
import os

def store_raw_images(imgs_path, imgs_type):

imgs_format = '.jpg'

if any([img[0:3] == imgs_type for img in os.listdir(imgs_path)]):
    current_imgs = list(filter(lambda x: x[0:3] == imgs_type, os.listdir(imgs_path)))
    name_index = max(list(map(lambda x: int(x[4:-4]), current_imgs)))
    imgs = list(filter(lambda x: x[0:3] != imgs_type, os.listdir(imgs_path)))
else:
    name_index = 1
    imgs = os.listdir(imgs_path)

for img in imgs:
    try:
        # Grayscaling and resizing
        grayscaled = imread(imgs_path + img, IMREAD_GRAYSCALE)
        resized = resize(grayscaled, (60, 90)) if imgs_type == 'pos' else resize(grayscaled, (500, 600))
        imwrite(imgs_path + imgs_type + '-' + str(name_index) + imgs_format, resized)
        name_index += 1

        # Deleting origin image
        os.remove(imgs_path + img)

    except Exception as e:
        os.remove(imgs_path + img)


store_raw_images('pos/', 'pos')

我收到此错误:

Traceback(最近一次调用最后一次):文件“img_converter.py”,第 45 行, 在 store_raw_images('pos/', 'pos') 文件“img_converter.py”,第 24 行,在 store_raw_images name_index = max(tr,[]) TypeError: src1 不是 numpy 数组,也不是标量

但是,如果我将下一个代码片段放在我的函数之外,它绝对可以正常工作并且没有错误:

if any([img[0:3] == imgs_type for img in os.listdir(imgs_path)]):
        current_imgs = list(filter(lambda x: x[0:3] == imgs_type, os.listdir(imgs_path)))
        name_index = max(list(map(lambda x: int(x[4:-4]), current_imgs)))
        imgs = list(filter(lambda x: x[0:3] != imgs_type, os.listdir(imgs_path)))

有人可以帮忙弄清楚为什么它表现出如此奇怪的行为吗? 请随时询问更多信息,并提前感谢您。

【问题讨论】:

    标签: python python-3.x max


    【解决方案1】:

    由于from cv2.cv2 import * 行,您已经用不同的实现覆盖了Python 的内置max() 函数。这正是不鼓励使用import * 的原因;没有办法知道什么时候会出现这样的问题。

    【讨论】:

    • 感谢您的帮助!祝你好运
    【解决方案2】:
    max_ = max; # capture the pointer to the native python function 
    from cv2.cv2 import *
    

    调用 max_() 以使用本机 python max 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-26
      • 2019-05-10
      • 2017-05-26
      • 2019-06-20
      • 2021-04-09
      • 2019-09-03
      • 2011-09-22
      • 1970-01-01
      相关资源
      最近更新 更多