Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write this pbtxt using python?

How can generate label_map.pbtxt using python? I need to generate label_map.pbtxt file automatically by sending object name.

item {
  id: 1
  name: 'object name'
}

I tried to create that using python functionality but when I start to train a model I get this error:

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

like image 544
FatiHe Avatar asked Aug 06 '26 08:08

FatiHe


2 Answers

from google.protobuf import text_format
with open("obj.pb.txt", "w") as fw:
    text_format.PrintMessage(obj, fw)

with open("obj.pb.txt", "r") as fr:
    text_format.Parse(fr.read(), obj)
like image 111
Magic Wang Avatar answered Aug 08 '26 11:08

Magic Wang


def label_map_v1(objname):
with open('/TEST-DS-TO-RECORD/annotations/label_map.pbtxt', 'a') as the_file:
    the_file.write('item\n')
    the_file.write('{\n')
    the_file.write('id :{}'.format(int(1)))
    the_file.write('\n')
    the_file.write("name :'{0}'".format(str(objname)))
    the_file.write('\n')
    the_file.write('}\n')

This can generate file that I need.

like image 31
FatiHe Avatar answered Aug 08 '26 12:08

FatiHe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!