2017-07-20 44 views
0

我正在爲使用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))) 

有人可以幫助弄清楚爲什麼它顯示出這麼奇怪的行爲? 請隨時詢問更多信息,並提前致謝。

回答

1

由於行from cv2.cv2 import *,您已覆蓋Python的內置函數max(),其實現方式不同。這完全是爲什麼不鼓勵使用import *;沒有辦法知道何時會出現這樣的問題。

+0

感謝您的幫助!祝你好運 – Michael

相關問題