【发布时间】: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