Skip to content
Snippets Groups Projects
Commit 4cba53b7 authored by Herman Andersen Dyrkorn's avatar Herman Andersen Dyrkorn
Browse files

salamandermatching now works for juvenile, will soon make it work for male and female

parent b06aa7b7
No related branches found
No related tags found
1 merge request!18Resolve "refactoring matchsalamander and register location endpoint"
from flask import request, jsonify, send_file, send_from_directory from flask import request, jsonify, send_file
from flask_restful import Resource from flask_restful import Resource
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
import os.path import os.path
import shutil
from api import db from api import db
from api.models.dbmodels import Location from api.models.dbmodels import Location
from api.models.dbmodels import Salamander, SalamanderGrowth
APPROVED_SEX = ["male", "female", "juvenile"] APPROVED_SEX = ["male", "female", "juvenile"]
APPROVED_SPECIES = ["smooth_newt", "northern_crested_newt"] APPROVED_SPECIES = ["smooth_newt", "northern_crested_newt"]
...@@ -12,26 +14,37 @@ APPROVED_SPECIES = ["smooth_newt", "northern_crested_newt"] ...@@ -12,26 +14,37 @@ APPROVED_SPECIES = ["smooth_newt", "northern_crested_newt"]
# matchSalamander endpoint # matchSalamander endpoint
class MatchSalamander(Resource): class MatchSalamander(Resource):
@staticmethod @staticmethod
# @jwt_required @jwt_required
def post(): def post():
data = request.form data = request.form
image = request.files['image'] user_id = get_jwt_identity()
locations = db.session.query(Location.name).all() locations = db.session.query(Location.name).all()
approved_locations = [] approved_locations = []
for location in locations: for location in locations:
approved_locations.append(location.name) approved_locations.append(location.name)
if image.filename != '' and "sex" in data and "location" in data and "species" in data: if "sex" in data and "location" in data and "species" in data and "confirmed" in data:
if data['sex'] in APPROVED_SEX and data['species'] in APPROVED_SPECIES and data['location'] in approved_locations: if data['sex'] in APPROVED_SEX and data['species'] in APPROVED_SPECIES and data['location'].lower() in approved_locations:
original_image_path = get_path(data, "original/") if data['confirmed']:
processed_image_path = get_path(data, "processed/") path_to_image_for_match = "./temp_images/" + str(user_id) + "_str.jpeg"
if directory_not_empty(original_image_path): path_to_original_image = "./temp_images/" + str(user_id) + ".jpeg"
path_to_images = "images/" + data['location'].lower() + "/" + data['species'] + "/" + data['sex'] + "/"
if data['sex'] != "juvenile":
hi = "g"
# start processing with AI # start processing with AI
# load preprocessed salamanders from processed_image_path # load preprocessed salamanders from processed_image_path
# brute force matching # brute force matching
# if match: # if match:
image.save(original_image_path + image.filename) else:
return send_file(image, as_attachment=True, attachment_filename=image.filename, mimetype='image/jpeg') location_id = db.session.query(Location.id).filter_by(name=data['location'].lower()).first()
new_salamander = Salamander(sex=data['sex'], species=data['species'], location_id=location_id[0], uid=user_id)
db.session.add(new_salamander)
db.session.commit()
salamander_id = db.session.query(Salamander.id).filter_by(uid=user_id).order_by(Salamander.id.desc()).first()
path_for_new_salamander = path_to_images + str(salamander_id[0]) + "/"
os.makedirs(path_for_new_salamander)
move_and_rename_original(path_for_new_salamander, path_to_original_image)
return jsonify({"message": "new juvenile in database", "id": salamander_id[0]})
# if miss: # if miss:
# create new salamander in DB # create new salamander in DB
# save original image # save original image
...@@ -39,12 +52,7 @@ class MatchSalamander(Resource): ...@@ -39,12 +52,7 @@ class MatchSalamander(Resource):
# output = new salamander with ID something, no match # output = new salamander with ID something, no match
else: else:
os.makedirs(original_image_path) return jsonify({"message": "invalid sex or location"})
os.makedirs(processed_image_path)
# start processing with AI
# create a new salamander in DB
image.save(original_image_path + image.filename)
return send_file("/Users/hermandyrkorn/Desktop/Bachelor/salamander-api/images/smooth_newt/aldhghudeyh/female/original/IMG_1201.jpeg", mimetype='jpeg', attachment_filename=image.filename, as_attachment=True)
else: else:
return jsonify({"message": "invalid sex or location"}) return jsonify({"message": "invalid sex or location"})
else: else:
...@@ -57,8 +65,9 @@ class MatchSalamander(Resource): ...@@ -57,8 +65,9 @@ class MatchSalamander(Resource):
return send_file("../temp_images/" + str(user_id) + ".jpeg", attachment_filename=str(user_id) + ".jpeg") return send_file("../temp_images/" + str(user_id) + ".jpeg", attachment_filename=str(user_id) + ".jpeg")
def get_path(data, end_directory): def move_and_rename_original(path_for_new_salamander, path_to_original_image):
return "images/" + data["species"] + "/" + data["location"] + "/" + data["sex"] + "/" + end_directory number_of_files = len(os.listdir(path_for_new_salamander))
shutil.move(path_to_original_image, path_for_new_salamander + str(number_of_files) + ".jpeg")
def directory_not_empty(directory_path): def directory_not_empty(directory_path):
...@@ -70,3 +79,4 @@ def directory_not_empty(directory_path): ...@@ -70,3 +79,4 @@ def directory_not_empty(directory_path):
return True return True
else: else:
return False return False
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment