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

Merge branch '18-combine-matchsalamander-with-ai' into 'master'

Resolve "combine matchsalamander with ai"

Closes #18

See merge request !21
parents 097e18c6 a26fb37b
No related branches found
No related tags found
1 merge request!21Resolve "combine matchsalamander with ai"
import numpy as np
from cv2 import cv2
from constants import width, height, step_size
from algorithm.main_src.constants import width, height, step_size
kp = [cv2.KeyPoint(x, y, step_size) for y in range(0, width , step_size) for x in range(0, height, step_size)]
sift = cv2.SIFT_create()
......
from cv2 import cv2
import os
from SalamanderImage import SalamanderImage
from algorithm.main_src.SalamanderImage import SalamanderImage
#import skeletonization
#import straightening
import dsift
import segmentation
import algorithm.main_src.dsift as dsift
import algorithm.main_src.segmentation as segmentation
def create_salamander_image(filename: str):
print("Processing image " + filename)
......
import os
from cv2 import cv2
from Salamander import Salamander
from SalamanderImage import SalamanderImage
# from SalamanderImage import SalamanderImage
# from database import salamanders, load
from imageprocessing import create_salamander_image
from algorithm.main_src.imageprocessing import create_salamander_image
min_good_match = 15
match_dist = 0.75
......@@ -60,15 +59,15 @@ def match_single_image(input_image, match_image) -> bool:
def match_file_structure(input_image, match_directory) -> int:
for folder in os.listdir(match_directory):
current_dir = match_directory + "\\" + folder
current_dir = match_directory + folder
for filename in os.listdir(current_dir):
if filename.endswith("_str.png"):
match_image = current_dir + "\\" + filename
match_image = current_dir + "/" + filename
res = match_single_image(input_image, match_image)
if res:
return int(folder)
return -1
print(match_file_structure("..\\..\\img_analyze\\img81_str.png", "..\\..\\Male"))
# print(match_file_structure("..\\..\\img_analyze\\img81_str.png", "..\\..\\Male"))
# res = match_single_image("..\\..\\img_analyze\\img40Edit_str.png", "..\\..\\img_analyze\\img43_str.png")
......@@ -6,10 +6,10 @@ import math
from tensorflow.python.keras.layers import Minimum
import predict_salamander_abdomen as psa
import algorithm.train_src.predict_salamander_abdomen as psa
from threading import Semaphore
DEEPLABCUT_CONFIG_PATH = "./dlc_model/config.yaml"
DEEPLABCUT_CONFIG_PATH = "./algorithm/train_src/dlc_model/config.yaml"
########################################
# CONSTANTS
########################################
......@@ -17,7 +17,7 @@ DEEPLABCUT_CONFIG_PATH = "./dlc_model/config.yaml"
# What GPU to use (normally 0 if you only have 1 gpu):
GPUID = 0
MAX_DEEPLABCUT_IMAGE_SIZE = 1280
LEAST_DEEPLABCUT_SCORE = 0.02
LEAST_DEEPLABCUT_SCORE = 0.00
# Only one thread is allowed to use tf and the gpu at once:
_ACCESS_TF_AND_GPU_SEMA = Semaphore(1)
......@@ -29,7 +29,7 @@ STRAIGTHENED_IMAGE_ASPECT_RATIO = STRAIGTHENED_IMAGE_HEIGHT / STRAIGTHENED_IMAGE
# amount of pixels to add to the width of the shoulder including what the AI estimates:
SHOULDER_ESTIMATION_BUFFER = 15
# minimum distance between shoulder points in pixels:
MINIMUM_SHOULDER_WIDTH = 100
MINIMUM_SHOULDER_WIDTH = 50
# minimum distance between the points, 2 and 3 in pixels:
MINIMUM_MID_POINT_DISTANCE = 30
......
No preview for this file type
from flask import request, jsonify
from flask_restful import Resource
from flask_jwt_extended import get_jwt_identity, jwt_required
from algorithm.train_src.new_straightening import straighten
import cv2
import os
ALLOWED_EXTENSIONS = ['jpeg', 'png', 'jpg']
......@@ -14,12 +17,17 @@ class FindSalamanderInfo(Resource):
return jsonify({"message": "no file given"})
image = request.files['image']
user_id = get_jwt_identity()
image.filename = str(user_id) + "." + extension(image.filename)
image.save("temp_images/" + image.filename)
execution_path = os.path.abspath(os.getcwd())
img = cv2.imread(execution_path + "/temp_images/" + str(user_id) + "." + extension(image.filename), 1)
if image.filename != '':
if image and allowed_image(image.filename):
# run AI on image
str_image, _, _, _, _ = straighten(img)
if str_image is not None:
cv2.imwrite("temp_images/" + str(user_id) + "_str.png", str_image)
# store processed and original image
image.filename = str(user_id) + "." + extension(image.filename)
image.save("temp_images/" + image.filename)
return jsonify({"sex": "AI_sex", "species": "AI_species"})
else:
return jsonify({"message": "file extension not allowed"})
......
......@@ -6,7 +6,6 @@ import shutil
from api import db
from api.models.dbmodels import Location
from api.models.dbmodels import Salamander, SalamanderGrowth
from algorithm.train_src.new_straightening import straighten
from algorithm.main_src.new_matching import match_file_structure
APPROVED_SEX = ["male", "female", "juvenile"]
......@@ -28,28 +27,28 @@ class MatchSalamander(Resource):
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'].lower() in approved_locations:
if data['confirmed']:
path_to_image_for_match = "./temp_images/" + str(user_id) + "_str.jpeg"
path_to_original_image = "./temp_images/" + str(user_id) + ".jpeg"
path_to_processed_image = "./temp_images/" + str(user_id) + "_str.png"
path_to_original_image = "./temp_images/" + str(user_id) + ".png"
path_to_images = "./images/" + data['location'].lower() + "/" + data['species'] + "/" + data['sex'] + "/"
if data['sex'] != "juvenile":
hi = "g"
# start processing with AI
# load preprocessed salamanders from processed_image_path
# brute force matching
# if match:
else:
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)
result = match_file_structure(path_to_processed_image, path_to_images)
if result > -1:
if "weight" in data and "length" in data:
salamander_growth = SalamanderGrowth(salamander_id=result, weight=data['weight'], length=data['length'])
db.session.add(salamander_growth)
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]})
move_and_rename_original(path_to_images + str(result) + "/", path_to_original_image)
move_and_rename_processed(path_to_images + str(result) + "/", path_to_processed_image)
return jsonify({"message": "MATCH!", "id": result})
else:
salamander_id = add_salamander(data, path_to_images, user_id, path_to_original_image, path_to_processed_image)
return jsonify({"message": "new salamander in database", "id": salamander_id})
else:
salamander_id = add_salamander(data, path_to_images, user_id, path_to_original_image, path_to_processed_image)
return jsonify({"message": "new juvenile in database", "id": salamander_id})
else:
return jsonify({"message": "image was not accepted by user"})
return jsonify({"message": "image was not accepted"})
else:
return jsonify({"message": "invalid sex, species or location"})
else:
......@@ -59,21 +58,27 @@ class MatchSalamander(Resource):
@jwt_required
def get():
user_id = get_jwt_identity()
return send_file("../temp_images/" + str(user_id) + ".jpeg", attachment_filename=str(user_id) + ".jpeg")
return send_file("../temp_images/" + str(user_id) + "_str.png", attachment_filename=str(user_id) + ".png")
def move_and_rename_original(path_for_new_salamander, path_to_original_image):
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 add_salamander(data, path_to_images, user_id, path_to_original_image, path_to_processed_image):
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)
move_and_rename_processed(path_for_new_salamander, path_to_processed_image)
return salamander_id[0]
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
def move_and_rename_original(path_to_salamander, path_to_original_image):
number_of_files = len(os.listdir(path_to_salamander))
shutil.move(path_to_original_image, path_to_salamander + str(number_of_files) + ".png")
def move_and_rename_processed(path_to_salamander, path_to_processed_image):
number_of_files = len(os.listdir(path_to_salamander))
shutil.move(path_to_processed_image, path_to_salamander + str(number_of_files) + "_str.png")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment