我正在为使用Python 3.5.3编写数据集编写小脚本。它检查文件夹中的现有文件,如果有那些已经被处理过的文件,它会找到下一个文件的最大索引。python max函数要求src2 TypeError:必需参数'src2'(pos 2)找不到
我被困在最大功能,因为某些原因需要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 (most recent call last): File "img_converter.py", line 45, in store_raw_images('pos/', 'pos') File "img_converter.py", line 24, in store_raw_images name_index = max(tr,[]) TypeError: src1 is not a numpy array, neither a scalar
但是,如果我把下面的代码片断我的功能外它的作品精美绝伦,没有错误:
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)))
有人可以帮助弄清楚为什么它显示出这么奇怪的行为? 请随时询问更多信息,并提前致谢。
感谢您的帮助!祝你好运 – Michael