-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
98 lines (75 loc) · 2.83 KB
/
app.py
File metadata and controls
98 lines (75 loc) · 2.83 KB
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
from flask import Flask, request, render_template, jsonify
import os
import tempfile
from main_pipeline import main
import base64
##########################
## Usage: python app.py ##
##########################
# --------------------------------------------------------------------------------- #
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
# --------------------------------------------------------------------------------- #
"""
Data from Web Page
"""
@app.route('/upload_file', methods=['POST'])
def upload_image():
data = request.get_json()
base64_str = data.get('image', None)
if not base64_str:
return jsonify({'error': 'No base64 image provided.'}), 400
try:
image_bytes = base64.b64decode(base64_str)
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmpfile:
tmpfile.write(image_bytes)
tmpfile_path = tmpfile.name
result_json = main(tmpfile_path)
if isinstance(result_json, str):
return jsonify({'error': result_json})
face_image_path = os.path.join("Outputs", "face_color.jpg")
face_base64 = ""
if os.path.exists(face_image_path):
with open(face_image_path, "rb") as f:
face_base64 = base64.b64encode(f.read()).decode("utf-8")
return jsonify({
"message": "Success",
"result": result_json,
"face_base64": face_base64
})
except Exception as e:
return jsonify({'error': str(e)}), 500
"""
Data from Postman
"""
# @app.route('/upload_file', methods=['POST'])
# def upload_image_file():
# if 'image' not in request.files:
# return jsonify({'error': 'No file provided.'}), 400
# file = request.files['image']
# try:
# image_bytes = file.read()
# base64_str = base64.b64encode(image_bytes).decode('utf-8')
# with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmpfile:
# tmpfile.write(base64.b64decode(base64_str))
# tmpfile_path = tmpfile.name
# result_json = main(tmpfile_path)
# if isinstance(result_json, str):
# return jsonify({'error': result_json})
# face_image_path = os.path.join("Outputs", "face_color.jpg")
# face_base64 = ""
# if os.path.exists(face_image_path):
# with open(face_image_path, "rb") as f:
# face_base64 = base64.b64encode(f.read()).decode("utf-8")
# return jsonify({
# "message": "Success",
# "result": result_json,
# "face_base64": face_base64
# })
# except Exception as e:
# return jsonify({'error': str(e)}), 500
# --------------------------------------------------------------------------------- #
if __name__ == '__main__':
app.run(debug=True)