Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 合并.vec文件_Python 3.x_Opencv_Vector - Fatal编程技术网

Python 3.x 合并.vec文件

Python 3.x 合并.vec文件,python-3.x,opencv,vector,Python 3.x,Opencv,Vector,您好,我正在使用此代码: import sys import glob import struct import argparse import traceback def exception_response(e): exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) for

您好,我正在使用此代码:

import sys
import glob
import struct
import argparse
import traceback


def exception_response(e):
    exc_type, exc_value, exc_traceback = sys.exc_info()
    lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
    for line in lines:
        print(line)

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', dest='vec_directory')
    parser.add_argument('-o', dest='output_filename')
    args = parser.parse_args()
    return (args.vec_directory, args.output_filename)

def merge_vec_files(vec_directory, output_vec_file):
    """
    Iterates throught the .vec files in a directory and combines them.
    (1) Iterates through files getting a count of the total images in the .vec files
    (2) checks that the image sizes in all files are the same
    The format of a .vec file is:
    4 bytes denoting number of total images (int)
    4 bytes denoting size of images (int)
    2 bytes denoting min value (short)
    2 bytes denoting max value (short)
    ex:     6400 0000 4605 0000 0000 0000
        hex     6400 0000   4605 0000       0000        0000
                # images    size of h * w       min     max
        dec         100         1350            0       0
    :type vec_directory: string
    :param vec_directory: Name of the directory containing .vec files to be combined.
                Do not end with slash. Ex: '/Users/username/Documents/vec_files'
    :type output_vec_file: string
    :param output_vec_file: Name of aggregate .vec file for output.
        Ex: '/Users/username/Documents/aggregate_vec_file.vec'
    """

    # Check that the .vec directory does not end in '/' and if it does, remove it.
    if vec_directory.endswith('/'):
        vec_directory = vec_directory[:-1]
    # Get .vec files
    files = glob.glob('{0}/*.vec'.format(vec_directory))

    # Check to make sure there are .vec files in the directory
    if len(files) <= 0:
        print('Vec files to be mereged could not be found from directory: {0}'.format(vec_directory))
        sys.exit(1)
    # Check to make sure there are more than one .vec files
    if len(files) == 1:
        print('Only 1 vec file was found in directory: {0}. Cannot merge a single file.'.format(vec_directory))
        sys.exit(1)


    # Get the value for the first image size
    prev_image_size = 0
    try:
        with open(files[0], 'rb') as vecfile:
            content = b''.join((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 = b''.join((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)


    # Iterate through the .vec files, writing their data (not the header) to the output file
    # '<iihh' means 'little endian, int, int, short, short'
    header = struct.pack('<iihh', total_num_images, image_size, 0, 0)
    try:
        with open(output_vec_file, 'wb') as outputfile:
            outputfile.write(header)

            for f in files:
                with open(f, 'rb') as vecfile:
                    content = b''.join((line) for line in vecfile.readlines())
                    outputfile.write(bytearray(content[12:]))
    except Exception as e:
        exception_response(e)


if __name__ == '__main__':
    vec_directory, output_filename = get_args()
    if not vec_directory:
        sys.exit('mergvec requires a directory of vec files. Call mergevec.py with -v /your_vec_directory')
    if not output_filename:
        sys.exit('mergevec requires an output filename. Call mergevec.py with -o your_output_filename')

merge_vec_files(vec_directory, output_filename)

我的目标是将矢量文件合并在一起,创建一个矢量文件,作为opencv中haar级联的一部分。我刚刚更新了这篇文章,有很多代码,所以我现在用它作为填充文本,但是的,你能给我的任何帮助都是非常感谢的。谢谢

语法错误是因为忘了在def merge_vec_filesWow的末尾加上一个:我真不敢相信我居然没听清楚。非常感谢。现在,我收到了这个错误,不幸的是:文件“Merge_Vecs.py”,第22行^SyntaxError:parsing时出现意外的EOF看起来您还没有定义函数Merge_vec_files。我不是合并这类文件的专家。我只是想帮你解决一些语法错误。我试着用终端给它一个矢量文件的目录,然后是所需的输出文件夹。你知道我能做的另一种方法吗?这与OpenCV无关,你只是没有写入文件(或创建文件)的权限。要解决此问题,请参阅操作系统的文档。
 File "mergevec.py", line 95, in merge_vec_files
    with open(output_vec_file, 'wb') as outputfile:

PermissionError: [Errno 13] Permission denied: 'output_vec_file'