File size: 2,793 Bytes
c7a22ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f71fc7
c7a22ad
9f71fc7
 
 
 
 
 
 
c7a22ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
sys.path.append('.')

import os
import numpy as np
import base64
import json

from PIL import Image, ExifTags
from flask import Flask, request, jsonify
from opyalpr import getHWID
from opyalpr import setLicenseKey
from opyalpr import initSDK
from opyalpr 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

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