Initial commit of project
This commit is contained in:
67
main.py
Normal file
67
main.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import os
|
||||
|
||||
from datetime import datetime
|
||||
from flask import Flask, jsonify, request
|
||||
import config
|
||||
from werkzeug.utils import secure_filename
|
||||
from imageProcessing import extract_metadata
|
||||
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
|
||||
|
||||
app = Flask("P! Vision Metadata Engine")
|
||||
app.config["UPLOAD_FOLDER"] = config.DATA_FOLDER
|
||||
router = '/metadata/' + config.ENV
|
||||
|
||||
|
||||
def allowed_file(file_name):
|
||||
return '.' in file_name and \
|
||||
file_name.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
@app.route(router + '/hello', methods=['GET'])
|
||||
def hello():
|
||||
return "Hello from P! Metadata Engine"
|
||||
|
||||
|
||||
@app.route(router + '/calculate-image-metadata',
|
||||
methods=['GET', 'POST'])
|
||||
def calculate_image_metadata():
|
||||
try:
|
||||
if request.method == 'POST':
|
||||
print()
|
||||
print("request.form:\t", request.form)
|
||||
print("request.files:\t", request.files)
|
||||
request_id = request.form.get("request_id", 100)
|
||||
print("request_id:\t", request_id)
|
||||
if "image_file" not in request.files:
|
||||
msg = "No file part: 'image_file' in POST request."
|
||||
print(msg)
|
||||
return msg
|
||||
file = request.files["image_file"]
|
||||
if file.filename == '':
|
||||
msg = "File name is empty."
|
||||
print(msg)
|
||||
return msg
|
||||
if file and allowed_file(file.filename):
|
||||
print("Uploaded file:\t", file.filename)
|
||||
image_file_name = str(datetime.now()).replace(':', '') + \
|
||||
f'_{request_id}_' + secure_filename(file.filename)
|
||||
image_file_path = os.path.join(
|
||||
app.config["UPLOAD_FOLDER"], image_file_name)
|
||||
file.save(image_file_path)
|
||||
print(f"Image file is saved at {image_file_path}")
|
||||
|
||||
modelResponse =extract_metadata(image_file_path)
|
||||
print("Response :\t", modelResponse)
|
||||
return jsonify({"request_id": request_id, "metadata": modelResponse})
|
||||
else:
|
||||
msg = "POST the image file. Don't GET it."
|
||||
print(msg)
|
||||
return msg
|
||||
except Exception as e:
|
||||
msg = str(type(e).__name__) + ': ' + str(e)
|
||||
print(msg)
|
||||
return msg
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=False, host="0.0.0.0", port=4455)
|
||||
Reference in New Issue
Block a user