我正在尝试遵循这个OpenCV练习http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html 但是在运行mergevec.py的步骤中遇到了困难(我使用Python版本而不是.cpp版本)。我使用的是Python3,而不是本文中提到的Python2.x。
此文件的源是https://github.com/wulfebw/mergevec/blob/master/mergevec.py
我得到的错误是
Traceback (most recent call last):
File "./tools/mergevec1.py", line 96, in <module>
merge_vec_files(vec_directory, output_filename)
File "./tools/mergevec1.py", line 45, in merge_vec_files
val = struct.unpack('<iihh', content[:12])
TypeError: a bytes-like object is required, not 'str'
我试着遵循python 3.5:TypeError:在写入文件时需要一个类似字节的对象,而不是'str',并使用open(f,'r',encoding='utf-8',errors='ignore')
。
我修改的代码如下:
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):
# 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], 'r', encoding='utf-8', errors='ignore') 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:
f = None
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, 'r', encoding='utf-8', errors='ignore') 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)
# 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, 'w', encoding='utf-8', errors='ignore') as vecfile:
content = ''.join(str(line) for line in vecfile.readlines())
data = content[12:]
outputfile.write(data)
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)
你知道我做错了什么吗?谢谢。
更新1
我这样做:
content = b''.join(str(line) for line in vecfile.readlines())
我基本上在前面加了“b”。但是,现在我得到了一个不同的错误:
回溯(最后一次调用):文件“/tools/mergevec1.py”,第97行,在merge\u-vec\u文件(vec\u目录,输出文件名)文件“/tools/mergevec1.py”,第44行,在merge\u-vec\u-files-content=b.”中。join(str(line)表示vecfile.readlines()中的行)类型错误:序列项0:应为类似对象的字节,str-find
当我改变它时,我能够解决我的问题:
for f in files:
with open(f, 'rb') as vecfile:
content = ''.join(str(line) for line in vecfile.readlines())
data = content[12:]
outputfile.write(data)
except Exception as e:
exception_response(e)
对于it:
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)
就像我改变它之前一样:
content = ''.join(str(line) for line in vecfile.readlines())
对于it:
content = b''.join((line) for line in vecfile.readlines())
因为它在等待一些str,现在它能够接收我们需要的二进制档案。
您保留错误是因为您正在使用代码
content = b''.join(str(line) for line in vecfile.readlines())
您必须使用:
content = b''.join((line) for line in vecfile.readlines())
这是没有“str”演员阵容的。
正如OP所解释的,文件包含二进制数据。为了处理二进制数据:
'rb'
作为open
调用中的模式
对于问题中提供的代码,用于读取图像的代码部分应为:
for f in files:
try:
with open(f, 'rb') as vecfile:
content = vecfile.read()
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)
我有windows 11 python 3.9,我运行一个py脚本来做一个启动器,我解决了一个问题,长重命名为int,但现在我不能确定发生了什么,错误是: TypeError:需要一个类似字节的对象,而不是str
我对Python完全陌生,正在尝试通过以下示例进行学习:对我来说,学习的最佳方式是查看示例,并尝试理解和使用我自己需要的代码。 我目前正试图通过一个项目学习python,该项目有树莓皮零W和adafruit fona 808突破板。 然而,示例代码是为Python2.7编写的,我想将其转换为适用于Python3的代码。 我收到这个打字错误。问题是,国际单项体育联合会应该如何运作。我曾尝试搜索谷歌并
我发现子字符串在压缩文件使用下面的python脚本。我得到"TypeError:一个字节样的对象是必需的,而不是'str'"。请任何一个人帮我解决这个问题。 和输入文件是 TruSeq2_SEAGATCGGAAGAGCTCGTATGCCGTCTTCTGCTTG 第二个文件是压缩文本文件。行“if abcd in line:”显示错误。
以下是尝试使用套接字修改用户提供的输入的代码: 当我执行它并提供输入时,会发生以下错误: 我能做些什么来解决这个问题?
我正在运行一个代码,但这一行出现错误: TypeError:需要一个类似字节的对象,而不是str 这里显示的是类型列表的各个部分,是 TypeError:需要一个类似字节的对象,而不是str