from aiohttp import web
import base64
import io
import face_recognition

async def encode(request):
    request_data = await request.json()

    # Read base64 encoded image
    url = request_data['image'].split(',')[1]
    image = io.BytesIO(base64.b64decode(url))

    # Load image data
    np_array = face_recognition.load_image_file(image)

    # Find face locations
    locations = face_recognition.face_locations(np_array)

    # Create face encodings
    encodings = face_recognition.face_encodings(np_array, locations)

    results = []

    for i in range(len(locations)):
        top, right, bottom, left = locations[i]

        result = {
            'x': left,
            'y': top,
            'width': right - left,
            'height': bottom - top,
            'encodings': encodings[i].tolist()
        }

        results.append(result)

    return web.json_response(results)

def main():
    app = web.Application()
    app.router.add_post('/encode', encode)

    web.run_app(app, host='0.0.0.0', port='3000')

main()

[((14, 15, 14, 54), 'face_recognition.load_image_file', 'face_recognition.load_image_file', ({(14, 48, 14, 53): 'image'}, {}), '(image)', False, 'import face_recognition\n'), ((17, 16, 17, 57), 'face_recognition.face_locations', 'face_recognition.face_locations', ({(17, 48, 17, 56): 'np_array'}, {}), '(np_array)', False, 'import face_recognition\n'), ((20, 16, 20, 68), 'face_recognition.face_encodings', 'face_recognition.face_encodings', ({(20, 48, 20, 56): 'np_array', (20, 58, 20, 67): 'locations'}, {}), '(np_array, locations)', False, 'import face_recognition\n'), ((37, 11, 37, 37), 'aiohttp.web.json_response', 'web.json_response', ({(37, 29, 37, 36): 'results'}, {}), '(results)', False, 'from aiohttp import web\n'), ((40, 10, 40, 27), 'aiohttp.web.Application', 'web.Application', ({}, {}), '()', False, 'from aiohttp import web\n'), ((43, 4, 43, 49), 'aiohttp.web.run_app', 'web.run_app', (), '', False, 'from aiohttp import web\n'), ((11, 23, 11, 44), 'base64.b64decode', 'base64.b64decode', ({(11, 40, 11, 43): 'url'}, {}), '(url)', False, 'import base64\n')]