繁体   English   中英

struct.error:解压缩需要长度为12的字符串参数

[英]struct.error: unpack requires a string argument of length 12

我正在尝试遵循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)

我尝试查看解决方案,但找不到适合此特定问题的解决方案。 任何帮助将不胜感激。

谢谢!

我通过转到本教程的github页面了解了这一点。 显然,我必须删除任何长度为零的vec文件。

您的问题是这样的:

content[:12]

该字符串不能保证为12个字符长; 可能会更少。 添加一个长度检查并单独进行处理,或者try: except:并为用户提供更清晰的错误消息,例如“文件中输入无效...”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM