我正在尝试按照Coding Robin的教程创建一个HAAR分类器:http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html。
我在需要合并所有.vec文件的部分。我正在尝试执行给定的python脚本,我收到以下错误:
Traceback (most recent call last):
File "mergevec.py", line 170, in <module>
merge_vec_files(vec_directory, output_filename)
File "mergevec.py", line 133, in merge_vec_files
val = struct.unpack('<iihh', content[:12])
struct.error: unpack requires a string argument of length 12
以下是python脚本中的代码:
# Get the value for the first image size
prev_image_size = 0
try:
with open(files[0], 'rb') as vecfile:
content = ''.join(str(line) for line in vecfile.readlines())
val = struct.unpack('<iihh', content[:12])
prev_image_size = val[1]
except IOError as e:
print('An IO error occured while processing the file: {0}'.format(f))
exception_response(e)
# Get the total number of images
total_num_images = 0
for f in files:
try:
with open(f, 'rb') as vecfile:
content = ''.join(str(line) for line in vecfile.readlines())
val = struct.unpack('<iihh', content[:12])
num_images = val[0]
image_size = val[1]
if image_size != prev_image_size:
err_msg = """The image sizes in the .vec files differ. These values must be the same. \n The image size of file {0}: {1}\n
The image size of previous files: {0}""".format(f, image_size, prev_image_size)
sys.exit(err_msg)
total_num_images += num_images
except IOError as e:
print('An IO error occured while processing the file: {0}'.format(f))
exception_response(e)
我尝试查找解决方案,但无法找到适合此特定问题的解决方案。任何帮助将不胜感激。
谢谢!
答案 0 :(得分:0)
我通过访问教程的github页面来解决这个问题。显然,我不得不删除任何长度为零的vec文件。
答案 1 :(得分:0)
你的问题是:
content[:12]
字符串不保证长度为12个字符;它可能更少。添加长度检查并单独处理,或try: except:
并为用户提供更准确的错误消息,例如“文件中的输入无效...”。