Python OpenCV LoadDatasetList,最后两个参数是什么?
我目前正在尝试使用 OpenCV 4.2.2 训练数据集,我搜索了网络,但只有 2 个参数的示例。OpenCV 4.2.2 loadDatasetList 需要 4 个参数,但存在一些缺点,我尽力通过以下方法克服了这些缺点。我首先尝试使用数组,但 loadDatasetList 抱怨数组不可迭代,然后我继续执行下面的代码,但没有成功。感谢您提供的任何帮助,感谢您的时间,希望每个人都平安健康。
先前传递数组时没有 iter() 的错误
PS E:\MTCNN> python kazemi-train.py No valid input file was given, please check the given filename. Traceback (most recent call last): File "kazemi-train.py", line 35, in status, images_train, landmarks_train = cv2.face.loadDatasetList(args.training_images,args.training_annotations, imageFiles, annotationFiles) TypeError: cannot unpack non-iterable bool object
当前错误是:
PS E:\MTCNN> python kazemi-train.py Traceback (most recent call last): File "kazemi-train.py", line 35, in status, images_train, landmarks_train = cv2.face.loadDatasetList(args.training_images,args.training_annotations, iter(imageFiles), iter(annotationFiles)) SystemError: returned NULL without setting an error
import os
import time
import cv2
import numpy as np
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Training of kazemi facial landmark algorithm.')
parser.add_argument('--face_cascade', type=str, help="Path to the cascade model file for the face detector",
default=os.path.join(os.path.dirname(os.path.realpath(__file__)),'models','haarcascade_frontalface_alt2.xml'))
parser.add_argument('--kazemi_model', type=str, help="Path to save the kazemi trained model file",
default=os.path.join(os.path.dirname(os.path.realpath(__file__)),'models','face_landmark_model.dat'))
parser.add_argument('--kazemi_config', type=str, help="Path to the config file for training",
default=os.path.join(os.path.dirname(os.path.realpath(__file__)),'models','config.xml'))
parser.add_argument('--training_images', type=str, help="Path of a text file contains the list of paths to all training images",
default=os.path.join(os.path.dirname(os.path.realpath(__file__)),'train','images_train.txt'))
parser.add_argument('--training_annotations', type=str, help="Path of a text file contains the list of paths to all training annotation files",
default=os.path.join(os.path.dirname(os.path.realpath(__file__)),'train','points_train.txt'))
parser.add_argument('--verbose', action='store_true')
args = parser.parse_args()
start = time.time()
facemark = cv2.face.createFacemarkKazemi()
if args.verbose:
print("Creating the facemark took {} seconds".format(time.time()-start))
start = time.time()
imageFiles = []
annotationFiles = []
for file in os.listdir("./AppendInfo"):
if file.endswith(".jpg"):
imageFiles.append(file)
if file.endswith(".txt"):
annotationFiles.append(file)
status, images_train, landmarks_train = cv2.face.loadDatasetList(args.training_images,args.training_annotations, iter(imageFiles), iter(annotationFiles))
assert(status == True)
if args.verbose:
print("Loading the dataset took {} seconds".format(time.time()-start))
scale = np.array([460.0, 460.0])
facemark.setParams(args.face_cascade,args.kazemi_model,args.kazemi_config,scale)
for i in range(len(images_train)):
start = time.time()
img = cv2.imread(images_train[i])
if args.verbose:
print("Loading the image took {} seconds".format(time.time()-start))
start = time.time()
status, facial_points = cv2.face.loadFacePoints(landmarks_train[i])
assert(status == True)
if args.verbose:
print("Loading the facepoints took {} seconds".format(time.time()-start))
start = time.time()
facemark.addTrainingSample(img,facial_points)
assert(status == True)
if args.verbose:
print("Adding the training sample took {} seconds".format(time.time()-start))
start = time.time()
facemark.training()
if args.verbose:
print("Training took {} seconds".format(time.time()-start))
如果我只使用 2 个参数,就会引发此错误
File "kazemi-train.py", line 37, in status, images_train, landmarks_train = cv2.face.loadDatasetList(args.training_images,args.training_annotations) TypeError: loadDatasetList() missing required argument 'images' (pos 3)
如果我尝试使用 3 个参数,就会引发此错误
Traceback (most recent call last): File "kazemi-train.py", line 37, in status, images_train, landmarks_train = cv2.face.loadDatasetList(args.training_images,args.training_annotations, iter(imagePaths)) TypeError: loadDatasetList() missing required argument 'annotations' (pos 4)
loadDatasetList 上的文档
您提供的图指的是
loadDatasetList()
的C++ API,其参数在很多情况下无法映射到Python API。一个原因是Python函数可以返回多个值,而C++不能。在C++ API中,第3和第4个参数用于存储函数的输出。它们分别存储从imageList中的文本文件读取后的图像路径和从annotationList中读取另一个文本文件后的注释路径。
回到您的问题,我在Python中找不到该函数的任何参考。我相信API在OpenCV 4中有所改变。经过多次尝试,我确定
cv2.face.loadDatasetList
只返回一个布尔值,而不是一个元组。所以这就是为什么你填写了四个参数,却遇到了第一个错误
TypeError: could not unpack non-iterable bool object
。
毫无疑问,
cv2.face.loadDatasetList
应该产生两个文件路径列表。因此,第一部分的代码看起来应如下所示:
images_train = []
landmarks_train = []
status = cv2.face.loadDatasetList(args.training_images, args.training_annotations, images_train, landmarks_train)
我期望
images_train
和
landmarks_train
应包含图像和地标注释的文件路径,但它并未按预期工作。
在理解整个程序后,我编写了一个新函数
my_loadDatasetList
来替换(损坏的)
cv2.face.loadDatasetList
。
def my_loadDatasetList(text_file_images, text_file_annotations):
status = False
image_paths, annotation_paths = [], []
with open(text_file_images, "r") as a_file:
for line in a_file:
line = line.strip()
if line != "":
image_paths.append(line)
with open(text_file_annotations, "r") as a_file:
for line in a_file:
line = line.strip()
if line != "":
annotation_paths.append(line)
status = len(image_paths) == len(annotation_paths)
return status, image_paths, annotation_paths
现在,您可以用
status, images_train, landmarks_train = my_loadDatasetList(args.training_images, args.training_annotations)
替换
status, images_train, landmarks_train = cv2.face.loadDatasetList(args.training_images,args.training_annotations, iter(imageFiles), iter(annotationFiles))
我已经测试过,
cv2.imread
可以加载
images_train
和
landmarks_train
,并且
cv2.face.loadFacePoints
分别使用来自
此处
的数据。
从文档中,我可以看到行
cv2.face.loadDatasetList
仅返回一个布尔值,其次从参数中删除
iter
。函数 loadDatasetList 接受列表作为第 3 和第 4 个参数。
因此,请在代码中进行以下更改:
来自:
status, images_train, landmarks_train = cv2.face.loadDatasetList(args.training_images,args.training_annotations, iter(imageFiles), iter(annotationFiles))
至:
status = cv2.face.loadDatasetList(args.training_images,args.training_annotations, imageFiles, annotationFiles)