我目前正在尝试使用OpenCV 4.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的文档
您提供的图形是指^ {< CD1>}的C++ API,在很多情况下,其参数通常不能映射到Python API的参数。一个原因是Python函数可以返回多个值,而C++不能。在C++ API中,提供第三和第四参数来存储函数的输出。它们分别存储从imageList中的文本文件读取后的图像路径,以及通过在annotationList中读取另一个文本文件来存储注释的路径
回到你的问题,我在Python中找不到该函数的任何参考。我相信API在OpenCV 4中已经改变了。经过多次试验,我确信
cv2.face.loadDatasetList
只返回一个布尔值,而不是一个元组。这就是为什么您会遇到第一个错误TypeError: cannot unpack non-iterable bool object
,即使您填写了四个参数毫无疑问,
cv2.face.loadDatasetList
应该生成两个文件路径列表。因此,第一部分的代码应该如下所示:我希望
images_train
和landmarks_train
应该包含图像和地标注释的文件路径,但它没有按预期工作在理解了整个程序之后,我编写了一个新函数
my_loadDatasetList
来替换(断开的)cv2.face.loadDatasetList
您现在可以替换
借
我已经测试了
images_train
和landmarks_train
可以分别通过cv2.imread
和cv2.face.loadFacePoints
使用来自here的数据进行加载从文档中,我可以看到行
cv2.face.loadDatasetList
只返回一个布尔值,然后从参数中删除iter
。函数loadDatasetList接受一个列表作为第三个和第四个参数因此,请在代码中进行以下更改:
发件人:
致:
相关问题 更多 >
编程相关推荐