Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to save: 'bytes' object has no attribute 'encode' #25

Closed
patrickcgray opened this issue Sep 22, 2017 · 25 comments
Closed

Unable to save: 'bytes' object has no attribute 'encode' #25

patrickcgray opened this issue Sep 22, 2017 · 25 comments
Assignees
Milestone

Comments

@patrickcgray
Copy link

I can't actually save a file on mac OS running python 3.6.2. Every time I try to save I get:

AttributeError: 'bytes' object has no attribute 'encode'

@jjdblast
Copy link

I also meet this problem, the error message exactly the same with patrick's

my environment :

Ubuntu 16.04, Anaconda 4, Python 2.7

@wkentaro wkentaro added the bug label Sep 26, 2017
@wkentaro wkentaro self-assigned this Sep 26, 2017
@landiaokafeiyan
Copy link

Hi,Thanks for sharing the code,I find a BUG that when I use the tool,if i sliding mouse,the tool will exit。what i have to do if i want to run the code and fix the bug?

@ibadami
Copy link

ibadami commented Oct 3, 2017

@wkentaro @jjdblast @patrickcgray
So I found two problems with the encoding bug.
in labelFile.py in def save(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None):
1.with open(filename, 'wb') as f: should be with open(filename, 'w') as f: because not all the data we would like to save in json is in binary.
2. imageData = b64encode(imageData.encode('utf-8')) should be imageData = b64encode(imageData).decode('ascii') this will convert the binary image to string.

As a final step one must correct the load function as well.
this will save the json file with ascii image data in base64.
This is suggested for python3, I am sure the change in python2 is similar.

@wkentaro
Copy link
Owner

wkentaro commented Oct 4, 2017

I tested in below environment, and they all works. Could you please specify the version again?

with labelme==2.6.2

  • python2.7 + pyqt5 + linux (work)
  • python2.7 + pyqt4 + linux (work)
  • python3.6 + pyqt5 + linux (work)
  • python3.6 + pyqt4 + linux (work)
  • python2.7 + pyqt5 + mac (not tested)
  • python2.7 + pyqt4 + mac (not tested)
  • python3.5 + pyqt5 + mac (work)
  • python3.5 + pyqt4 + mac (work)

@ibadami
Copy link

ibadami commented Oct 4, 2017

Could it be that this problem is platform specific? I am also running on mac OS and I faced the same problem.
Did you test it on mac?

@landiaokafeiyan
Copy link

@kentaro Wad i can install labelme on windows and ubuntu sucessfully. the problem is that when i use labeme to label the image, i will exit the GUI when i slide the mouse pulley.

@wkentaro
Copy link
Owner

wkentaro commented Oct 4, 2017

@landiaokafeiyan Your problem is different from the topic in this issue, right? If so, could you please open a new issue?

@landiaokafeiyan
Copy link

@ibadami i can install labelme on windows and ubuntu sucessfully. i have not test it on Mac

@landiaokafeiyan
Copy link

@wkentaro ok sure could you please fix the bug?

@wkentaro
Copy link
Owner

wkentaro commented Oct 4, 2017

@landiaokafeiyan I think so.

@landiaokafeiyan
Copy link

@wkentaro many thanks

@wkentaro
Copy link
Owner

wkentaro commented Oct 4, 2017

@ibadami I also tested on mac also, and it works. (fyi I use conda)

@ibadami
Copy link

ibadami commented Oct 4, 2017

I too used conda.
I am using pyqt5 and python 3.5 and you tested on them as well.
This is very surprising.

@ibadami
Copy link

ibadami commented Oct 4, 2017

What I do not understand is. When you read the image data using read function in app.py it is read as a binary data. Then how can you encode that binary data in utf-8 during saving. When I googled the error message, I found that this error occurs because the binary data cannot be further encoded.

@artstreltsov
Copy link

+1, same problem

@peyman-sabouri
Copy link

I used the following and solved the problem:

$ pip3 install SIP
$ pip3 install pyqt5
$ pip install labelme ( it did not worked with pip3)

@wangchenghan
Copy link

Thanks for sharing this code.
I also meet this problem on windows with python3.6 and pyqt5

@l02i08u
Copy link

l02i08u commented Nov 9, 2017

I tested in this way on windows, and it worked.
In labelFile.py
def save(...): with open(filename, 'wb') as f: if six.Py3: imagedata=b64encode(imageData.decode('utf-8') should be with open(filename, 'w') as f: if six.Py3: imagedata=str(b64encode(imageData), 'utf-8')

@wkentaro wkentaro added bug? and removed bug labels Nov 15, 2017
@hgs1217
Copy link

hgs1217 commented Dec 25, 2017

@l02i08u 's way works on my windows with Python 3.6. Thanks for his way.

@y-wan
Copy link

y-wan commented Dec 26, 2017

Thanks, @l02i08u 's way works for me as well while saving a file (Python 3.6 on Windows). However, when opening a json file saved in this way, the following code in labelFile.py should also be modified, otherwise similar encoding issues would occur:
(under def load(self, filename):)

  • with open(filename, 'rb') as f: should be with open(filename, 'r') as f:
  • imageData = b64decode(data['imageData']).decode('utf-8') should be imageData = b64decode(bytes(data['imageData'], 'utf-8'))

@MischaSchwendy
Copy link

Thank you @l02i08u + @y-wan ! These changes solved the problem for me, too! (also Python 3.6 on Windows)

@blakeliu
Copy link

blakeliu commented Jan 11, 2018

@y-wan
My os is windows 10 ,and anaconda 3 with python 3.5.4
def load(self, filename): try: with open(filename, 'r') as f: data = json.load(f) imagePath = data['imagePath'] if six.PY3: imageData = b64decode(bytes(data['imageData'], 'utf-8')) elif six.PY2: imageData = b64decode(data['imageData'])

def save(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None): try: with open(filename, 'w') as f: if six.PY3: imageData = str(b64encode(imageData), 'utf-8') #b64encode(imageData.encode('utf-8')) elif six.PY2: imageData = b64encode(imageData)
When I load json file:
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\labelme-2.6.4-py3.5.egg\labelme\app.py", line 780, in openFile
File "C:\Anaconda3\lib\site-packages\labelme-2.6.4-py3.5.egg\labelme\app.py", line 665, in loadFile
AttributeError: 'bytes' object has no attribute 'encode'

@runforever
Copy link

os: windows 10
python: 3.6.4
pyqt5

change labelFile.py file save method

with open(filename, 'w') as f: 
    if six.Py3:
       imagedata=str(b64encode(imageData), 'utf-8')

change labelFile.py file load method

with open(filename, 'r') as f:
    if six.PY3:
        imageData = b64decode(bytes(data['imageData'], 'utf-8'))

works

thanks @l02i08u @y-wan , you are life saver

@wkentaro wkentaro added bug and removed bug? labels Feb 6, 2018
@wkentaro
Copy link
Owner

wkentaro commented Feb 6, 2018

Sorry for late, but I confirmed this issue on Python3.
#61 should fix this.

@rafaelvalero
Copy link

you may try insted of encode, decode.

@wkentaro wkentaro added this to the 2.7.0 milestone May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests