-1

The following is my code of writing facial landmarks to files:

import face_alignment, os
from skimage import io

fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._3D, flip_input=False, device='cpu')
dir = os.listdir('/home/onur/Downloads/datasets/headsegmentation_final/Training/Images/')

for image in dir:
    try:
        input = io.imread('/home/onur/Downloads/datasets/headsegmentation_final/Training/Images/' + image)
        preds = fa.get_landmarks(input)
        if preds != None:
            print(preds)
            f = open('/home/onur/Downloads/datasets/headsegmentation_final/Training/Images/' + image[:-4] + '_landmark.txt', 'w')
            f.write(preds)
            f.write('\n')
    except:
        pass

The output of preds is this:

[array([[ 51.       , 122.       , -24.98454  ],
       [ 53.       , 145.       , -26.51761  ],
       [ 58.       , 164.       , -28.835543 ],
       [ 64.       , 183.       , -29.486448 ],
       [ 72.       , 198.       , -25.393326 ],   
...])]

But the returned files are empty. Where am I going wrong?

1

1 Answer 1

1

I would guess buffering is enabled, and you never close the file. As a result, nothing ends up getting written. Should do the writing part like this:

with open('/home/onur/Downloads/datasets/headsegmentation_final/Training/Images/' + image[:-4] + '_landmark.txt', 'w') as f:
    f.write(preds)
    f.write('\n')

Using the with context manager will automatically close the file when you exit the block, which should also flush anything in the buffer.

Sign up to request clarification or add additional context in comments.

2 Comments

It hasn't solved the issue..
Ah, you also need to specify how preds is to be written. The write method takes a string, and that's not the type of preds so an exception is thrown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.