Skip to content
Snippets Groups Projects

Resolve "matchSalamander endpoint"

Merged Herman Andersen Dyrkorn requested to merge 1-matchsalamander-endpoint into master
+ 37
4
from flask import Flask, request
from flask_restful import Api, Resource
import os.path
app = Flask(__name__)
api = Api(app)
@@ -9,13 +10,45 @@ class MatchSalamander(Resource):
@staticmethod
def post():
data = request.form
uploaded_file = request.files['image']
if uploaded_file.filename != '':
uploaded_file.save("images/"+uploaded_file.filename)
return {"species:": data["species"], "sex": data["sex"], "location": data["location"]}
image = request.files['image']
if image.filename != '' and "sex" in data and "location" in data and "species" in data:
original_image_path = get_path(data, "original/")
processed_image_path = get_path(data, "processed/")
if directory_not_empty(original_image_path):
image.save(original_image_path + image.filename)
# start processing with AI
# load preprocessed salamanders from processed_image_path
output = {"message": "match or miss"}
else:
os.makedirs(original_image_path)
os.makedirs(processed_image_path)
image.save(original_image_path + image.filename)
output = {"message": "no salamanders to match against"}
else:
output = {"message": "errors in form data"}
return output
api.add_resource(MatchSalamander, "/matchSalamander")
def get_path(data, end_directory):
return "images/" + data["species"] + "/" + data["sex"] + "/" + data["location"] + "/" + end_directory
def directory_not_empty(directory_path):
if os.path.exists(directory_path) and not os.path.isfile(directory_path):
# Checking if the directory is empty or not
if not os.listdir(directory_path):
return False
else:
return True
else:
return False
if __name__ == "__main__":
app.run(debug=True)
Loading