Changing gender of the image, add beard, make bald, add glasses etc

Recently few gender changing apps have gone viral on Facebook. They take user’s photo as an input and change the gender to opposite sex. Surprisingly these apps are highly accurate. All these app are using API exposed by the company faceapp.
Faceapp is a very popular mobile app which was doing the same image manipulations. Now Facebook apps are making using of their APIs to create similar Facebook apps. Here is the sample Python code which can be used to invoke their APIs.

#!/usr/bin/python2
import requests
import os
import sys
import random
import string
 
FILTERS = [
    "smile",
    "smile_2",
    "hot",
    "old",
    "young",
    "female",
    "male"
]
 
def upload_photo(path, filter_name, out_path):
    deviceID = ''.join(random.choice(string.letters) for i in range(8))
    headers = {'User-agent': "FaceApp/1.0.229 (Linux; Android 4.4)", 'X-FaceApp-DeviceID': deviceID}
    res = requests.post('https://node-01.faceapp.io/api/v2.3/photos', headers=headers, files={'file': open(path, "rb")})
    code = res.json().get('code')
    if not code:
        print 'Error getting code'
        sys.exit(1)
    res2 = requests.get('https://node-01.faceapp.io/api/v2.3/photos/%s/filters/%s?cropped=%s' % (code, filter_name, "1"), headers=headers)
    if 'x-faceapp-errorcode' in res2.headers:
        print "Error %s" % res2.headers['x-faceapp-errorcode']
        sys.exit(1)
    open(out_path, 'wb').write(res2.content)
 
def main():
    if len(sys.argv) != 4:
        print 'Usage: python faceapp.py image_path filter out_path'
        print 'Where filter is one of: %s' % (' '.join(FILTERS))
        sys.exit(1)
    prog, path, filter_name, out_path = sys.argv
 
    path = os.path.abspath(path)
    if not os.path.exists(path):
        print 'File %s not found' % path
        sys.exit(1)
    upload_photo(path, filter_name, out_path)
 
if __name__ == '__main__':
    main()

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *