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

AttributeError: 'str' object has no attribute 'write' #6

Open
tangaggie opened this issue Dec 22, 2018 · 4 comments
Open

AttributeError: 'str' object has no attribute 'write' #6

tangaggie opened this issue Dec 22, 2018 · 4 comments

Comments

@tangaggie
Copy link

Hi,
I modified some codes in 'write' of binvox_rw.py to resolve the error - 'AttributeError: 'str' object has no attribute 'write' , when executing the command - model.write('dilated.binvox').
Hope it helps.

def write(voxel_model, fp):
""" Write binary binvox format.

Note that when saving a model in sparse (coordinate) format, it is first
converted to dense format.

Doesn't check if the model is 'sane'.

"""
if voxel_model.data.ndim==2:
    # TODO avoid conversion to dense
    dense_voxel_data = sparse_to_dense(voxel_model.data, voxel_model.dims)
else:
    dense_voxel_data = voxel_model.data
print('fp : ', fp)
with open(fp, 'w') as fw:
    fw.write('#binvox 1\n')
    fw.write('dim '+' '.join(map(str, voxel_model.dims))+'\n')
    fw.write('translate '+' '.join(map(str, voxel_model.translate))+'\n')
    fw.write('scale '+str(voxel_model.scale)+'\n')
    fw.write('data\n')
    if not voxel_model.axis_order in ('xzy', 'xyz'):
       raise ValueError('Unsupported voxel model axis order')

    if voxel_model.axis_order=='xzy':
       voxels_flat = dense_voxel_data.flatten()
    elif voxel_model.axis_order=='xyz':
       voxels_flat = np.transpose(dense_voxel_data, (0, 2, 1)).flatten()

# keep a sort of state machine for writing run length encoding
    state = voxels_flat[0]
    ctr = 0
    for c in voxels_flat:
       if c==state:
           ctr += 1
        # if ctr hits max, dump
           if ctr==255:
              fw.write(chr(state))
              fw.write(chr(ctr))
              ctr = 0
       else:
          # if switch state, dump
           fw.write(chr(state))
           fw.write(chr(ctr))
           state = c
           ctr = 1
# flush out remainders
       if ctr > 0:
         fw.write(chr(state))
         fw.write(chr(ctr))
@JohnNesbit
Copy link

thanks so much! this helped me a lot!

@tianyilt
Copy link

after using the new write function,I encounter another problem.

i try to run the demo

import scipy.ndimage
scipy.ndimage.binary_dilation(model.data.copy(), output=model.data)
model.write('dilated.binvox')

the error is that

UnicodeEncodeError: 'gbk' codec can't encode character '\x80' in position 0: illegal multibyte sequence
File "binvox_rw.py", line 289, in write
fw.write(chr(ctr))

i try to fix it by changing "with open(fp, 'w') as fw:" into “with open(fp, 'w', encoding) as fw:”
but still failed because i can't read the saved binvox file by

with open('dilated.binvox', 'rb') as f:
model = binvox_rw.read_as_3d_array(f)

the error is that

Traceback (most recent call last):
File "", line 2, in
File "binvox_rw.py", line 147, in read_as_3d_array
data = data.reshape(dims)
ValueError: cannot reshape array of size 2717486 into shape (32,32,32)

@zougougou
Copy link

@tianyilt 请问你是否解决了这个问题,能告诉我一下解决方法吗

@tianyilt
Copy link

tianyilt commented Oct 7, 2021

binvox_rw io in decor-gan plays well and you can refer to this repository. @zougougou

@tianyilt 请问你是否解决了这个问题,能告诉我一下解决方法吗

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

4 participants