File size: 4,170 Bytes
61242aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import sys
sys.path.append('.')

import os
import numpy as np
import base64
import json
import io

from PIL import Image, ExifTags
from flask import Flask, request, jsonify
from idlivesdk import getHWID
from idlivesdk import setLicenseKey
from idlivesdk import initSDK
from idlivesdk import processImage

licenseKeyPath = "license.txt"
license = os.environ.get("LICENSE_KEY")

if license is None:
    try:
        with open(licenseKeyPath, 'r') as file:
            license = file.read().strip()
    except IOError as exc:
        print("failed to open license.txt: ", exc.errno)
    print("License Key: ", license)

hwid = getHWID()
print("HWID: ", hwid.decode('utf-8'))

ret = setLicenseKey(license.encode('utf-8'))
print("Set License: ", ret)

ret = initSDK("model".encode('utf-8'))
print("Init: ", ret)

app = Flask(__name__) 

def apply_exif_rotation(image):
    try:
        exif = image._getexif()
        if exif is not None:
            for orientation in ExifTags.TAGS.keys():
                if ExifTags.TAGS[orientation] == 'Orientation':
                    break

            # Get the orientation value
            orientation = exif.get(orientation, None)

            # Apply the appropriate rotation based on the orientation
            if orientation == 3:
                image = image.rotate(180, expand=True)
            elif orientation == 6:
                image = image.rotate(270, expand=True)
            elif orientation == 8:
                image = image.rotate(90, expand=True)

    except AttributeError:
        print("No EXIF data found")

    return image


@app.route('/process_image', methods=['POST'])
def process_image():
    file = request.files['image']

    try:
        image = apply_exif_rotation(Image.open(file)).convert('RGB')        
    except:
        result = "Failed to open file"
        response = jsonify({"resultCode": "Error", "result": result})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response

    image_np = np.asarray(image)
    result = processImage(image_np, image_np.shape[1], image_np.shape[0])

    if result is None:
        result = "Failed to process image"
        response = jsonify({"resultCode": "Error", "result": result})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response
    else:
        result_dict = json.loads(result.decode('utf-8'))
        response = jsonify({"resultCode": "Ok", "result": result_dict})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response

@app.route('/process_image_base64', methods=['POST'])
def process_image_base64():
    try:
        content = request.get_json()
        base64_image = content['base64']

        image_data = base64.b64decode(base64_image)
        image = apply_exif_rotation(Image.open(io.BytesIO(image_data))).convert("RGB")
    except:
        result = "Failed to parse base64"
        response = jsonify({"resultCode": "Error", "result": result})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response

    image_np = np.asarray(image)
    result = processImage(image_np, image_np.shape[1], image_np.shape[0])

    if result is None:
        result = "Failed to process image"
        response = jsonify({"resultCode": "Error", "result": result})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response
    else:
        result_dict = json.loads(result.decode('utf-8'))
        response = jsonify({"resultCode": "Ok", "result": result_dict})

        response.status_code = 200
        response.headers["Content-Type"] = "application/json; charset=utf-8"
        return response

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 9000))
    app.run(host='0.0.0.0', port=port)