Skip to main content
 首页 » 编程设计

python之Object_Detection_Demo : Google protobuf text_format. 合并 : a bytes-like object is required, 不是 'str'

2024年10月01日44Leo_wl

引用:object_detection_tutorial.ipynb

注意:我已根据安装说明成功安装所有内容 Installation并在 github 上对此进行了研究,但没有成功。

Label maps 将索引映射到类别名称,因此当我们的卷积网络预测 5 时,我们知道这对应于飞机。

label_map = label_map_util.load_labelmap(PATH_TO_LABELS) 

这引发了一个错误

TypeError: a bytes-like object is required, not 'str' 

在这个函数 label_map_util.load_labelmap 下钻后,下面是使用的函数 (load_labelmap)

from google.protobuf import text_format 
from object_detection.protos import string_int_label_map_pb2 
 
with tf.gfile.GFile(PATH_TO_LABELS, 'rb') as fid: 
    label_map_string = fid.read() 
    label_map = string_int_label_map_pb2.StringIntLabelMap() 
    print(type(label_map_string)) 
    print(type(label_map)) 
    try: 
      text_format.Merge(label_map_string, label_map) 
    except text_format.ParseError: 
      label_map.ParseFromString(label_map_string) 

我试图查看相同的错误。但是 label_map_string 已经是一个 Bytes 对象,正如您在输出中看到的那样。阅读时也尝试过“r”模式。

输出

<class 'bytes'> 
<class 'object_detection.protos.string_int_label_map_pb2.StringIntLabelMap'> 
--------------------------------------------------------------------------- 
TypeError                                 Traceback (most recent call last) 
<ipython-input-25-3fce64fd5c00> in <module>() 
      5     print(type(label_map)) 
      6     try: 
----> 7       text_format.Merge(label_map_string, label_map) 
      8     except text_format.ParseError: 
      9       label_map.ParseFromString(label_map_string) 
 
C:\Users\GUS9KOR\AppData\Local\Continuum\Anaconda3\envs\dlnd\lib\site-packages\protobuf-3.3.0-py3.5.egg\google\protobuf\text_format.py in Merge(text, message, allow_unknown_extension, allow_field_number, descriptor_pool) 
    475   """ 
    476   return MergeLines( 
--> 477       text.split('\n'), 
    478       message, 
    479       allow_unknown_extension, 
 
TypeError: a bytes-like object is required, not 'str' 

提前致谢。

请您参考如下方法:

如果发现从TensorFlow 1.0.0升级到TensorFlow 1.2.0解决了这个错误。我在 mac 上使用 protobuf==3.3.0。