Commit 336a9e59 authored by Herman Andersen Dyrkorn's avatar Herman Andersen Dyrkorn
Browse files

Merge branch '35-remove-dead-code-and-cleanup' into 'master'

Resolve "remove dead code and cleanup"

Closes #35

See merge request !38
parents 392e149d 74270774
......@@ -2,10 +2,10 @@ venv
.idea
images
__pycache__
algorithm/train_src/dlc_model
algorithm/train_src/imageai_model
algorithm/dlc_model
algorithm/imageai_model
env
temp_images
api/database/database.db
*.db
sqlite3.exe
img_analyze
import os
import algorithm.train_src.estimate_gender as bb
import algorithm.estimate_gender as bb
import cv2
from path_constants import abs_path_temp_images
......
import algorithm.train_src.new_straightening as straighten
import algorithm.straighten_with_dlc as straighten
# CROPPING IMAGE
import cv2
import os
......
import os
from cv2 import cv2
# from SalamanderImage import SalamanderImage
# from database import salamanders, load
from algorithm.main_src.imageprocessing import create_salamander_image
from algorithm.imageprocessing import create_salamander_image
import glob
min_good_match = 15
match_dist = 0.75
from path_constants import image_type
# def match_image(input_image: SalamanderImage) -> Salamander:
# TODO: Load fra den nye databasen
# salamanders = load() Må laste inn file path laget i APIen
# bf = cv2.BFMatcher()
# matches = []
# good = []
# # print (salamanders)
# for id in salamanders:
# salamander = salamanders[id]
# # print('Matching with ', id)
# for image in salamander.images:
# match = bf.knnMatch(input_image.descriptors, image.descriptors, k=2)
# matches.append(match)
# for match in matches:
# goodmatch = []
# for m, n in match:
# if m.distance < match_dist * n.distance:
# goodmatch.append([m])
# good.append([goodmatch])
#
# is_same_image = input_image.filename == image.filename #Burde ha bedre sjekk enn bare filnavn
# if is_same_image:
# del input_image
# is_match = len(goodmatch) > min_good_match and not is_same_image
# if is_match:
# print('Match found :', salamander.id)
# return salamander
# return None
def match_single_image(input_image, match_image) -> bool:
bf = cv2.BFMatcher()
input_salamander = create_salamander_image(input_image)
......@@ -69,8 +33,4 @@ def match_file_structure(input_image: str, match_directory: str) -> int:
return int(folder)
return -1
else:
return None
# print(match_file_structure("..\\..\\img_analyze\\img81_str.png", "..\\..\\Male"))
# res = match_single_image("..\\..\\img_analyze\\img40Edit_str.png", "..\\..\\img_analyze\\img43_str.png")
return None
\ No newline at end of file
import numpy as np
from cv2 import cv2
from algorithm.main_src.constants import width, height, step_size
from algorithm.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()
......
......@@ -6,7 +6,7 @@ from multiprocessing import Process
from multiprocessing import Manager
from path_constants import abs_path_imageai_config
from path_constants import abs_path_imageai_model
from algorithm.train_src.new_straightening import _ACCESS_TF_AND_GPU_SEMA
from algorithm.straighten_with_dlc import _ACCESS_TF_AND_GPU_SEMA
EXTRACT_DETECTED_OBJECTS = False
MINIMUM_PERCENTAGE_PROBABILITY = 0.1
......
from cv2 import cv2
import numpy as np
import os
from algorithm.main_src.SalamanderImage import SalamanderImage
from algorithm.SalamanderImage import SalamanderImage
#import skeletonization
#import straightening
import algorithm.main_src.dsift as dsift
import algorithm.main_src.segmentation as segmentation
import algorithm.dsift as dsift
import algorithm.segmentation as segmentation
def create_salamander_image(filename: str):
print("Processing image " + filename)
......@@ -26,7 +25,6 @@ def get_descriptors(filename: str):
Reads, straightens, resizes the image and returns it as a
"""
def get_straightened_image(filename: str):
#TODO: Fiks filnavn format
straightened_filename = filename#[0:-4] + '_str.jpg'
image = get_image(straightened_filename)
......
from typing import List
from SalamanderImage import SalamanderImage
class Salamander():
images: List[SalamanderImage] = []
def __init__(self, id):
self.id = id
import cv2
import numpy as np
def viewImage(image):
cv2.namedWindow('Display', cv2.WINDOW_NORMAL)
cv2.imshow('Display', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def findGreatesContour(contours, imsize):
largest_area = 0
largest_contour_index = -1
i = 0
total_contours = len(contours)
while (i < total_contours ):
area = cv2.contourArea(contours[i])
if(area > largest_area and area < imsize*0.9):
largest_area = area
largest_contour_index = i
i+=1
return largest_area, largest_contour_index
def create_mask(im):
gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
ret, threshold = cv2.threshold(gray, np.mean(gray)/1.5, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
viewImage(threshold)
i, contours, hierarchy= cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
largest_area, largest_contour_index = findGreatesContour(contours, gray.size)
cv2.drawContours(im, contours[largest_contour_index], -1, (0, 0, 255), 3)
mask = np.zeros(im.shape)
cv2.fillPoly(mask, pts =[contours[largest_contour_index]], color=(255,255,255))
return mask
const smartcrop = require('smartcrop-sharp');
const sharp = require('sharp');
const glob = require('glob')
const util = require('util');
const path = require('path');
const args = process.argv.slice(2);
const fs = require('fs');
const inputFolder = args[0];
const outputFolder = args[1];
const promiseGlob = util.promisify(glob);
async function cropFiles() {
const targetWidth = 512;
const targetHeight = 512;
const originalWidth = 1240;
const originalHeight = 1748;
const files = await promiseGlob(`${inputFolder}/*.jpg`);
const crops = [];
for(const image of files) {
const smartCropOptions = {
ruleOfThirds: false,
};
const row = await smartcrop.crop(image, {
width: originalWidth,
height: targetHeight,
...smartCropOptions,
});
const column = await smartcrop.crop(image, {
width: targetWidth,
height: originalHeight,
...smartCropOptions,
})
imageCrop = {
filename: path.basename(image),
x: column.topCrop.x,
y: row.topCrop.y,
width: targetWidth,
height: targetWidth,
};
await sharp(image)
.extract({
left: imageCrop.x,
top: imageCrop.y,
width: targetWidth,
height: targetHeight,
})
.toFile(path.join(outputFolder, imageCrop.filename.slice(0, -4) + '.png'))
crops.push(imageCrop);
}
fs.writeFileSync(`${outputFolder}/crops.json`, JSON.stringify(crops));
}
cropFiles();
from typing import Dict
from Salamander import Salamander
import jsonpickle.ext.numpy as jsonpickle_numpy
import jsonpickle
jsonpickle_numpy.register_handlers()
salamanders: Dict[str, Salamander] = dict()
filename = 'data/salamanders.json'
def save(new_salamanders):
with open(filename, 'w') as outfile:
data = jsonpickle.encode(new_salamanders)
outfile.write(data)
def load():
with open(filename, 'r') as infile:
data = infile.read()
return jsonpickle.decode(data)
\ No newline at end of file
import database
import imageprocessing
import cv2 as cv2
import glob
import database
import shutil
import os
from Salamander import Salamander
from constants import input_salamander_id, input_images_path,images_basepath
from imageprocessing import create_salamander_image
from matching import match_image
from database import salamanders
def main():
answer = input ("Load new input images (y/n)?")
while answer == 'y':
salamanders = database.load()
salamander = Salamander(input_salamander_id)
salamander.images = [create_salamander_image(file) for file in glob.iglob(input_images_path + '/*[!=_str and !=_th].jpg')]
if len(salamander.images) == 0 :
print ('No images in input folder, try again!')
return None
# print(salamander)
print("Matching image " + salamander.images[0].filename + " against " + str(len(salamanders)) + " individuals")
input_salamander = match_image(salamander.images)
if input_salamander is None:
print("No match for image " + salamander.images[0].filename + " could found.")
new_id = input ("Enter a new salamander id: ")
id = int(new_id)
#id_exist = salamanders.get(id)
#if id_exist is not None:
new_salamander = Salamander(id)
salamanders.update({ id: new_salamander})
new_salamander.images = salamander.images
os.mkdir(images_basepath + str(id))
for file in glob.iglob(input_images_path + '/*.jpg'):
shutil.move(file, images_basepath + str(id))
#if id_exist is None:
#print ("ID already exists!")
#return None
if input_salamander is not None:
id_exist = salamanders.get(input_salamander.id)
print("Adding image of salamander " + str(input_salamander.id) + " to database")
if id_exist is not None:
for image in salamander.images:
input_salamander.images.append(image)
for file in glob.iglob(input_images_path + '/*.jpg'):
shutil.move(file, images_basepath + input_salamander.id)
salamanders.update({input_salamander.id: input_salamander})
del salamander
database.save(salamanders)
answer = input ("Load new input images (y/n)?")
if __name__ == "__main__":
main()
\ No newline at end of file
from cv2 import cv2
from Salamander import Salamander
from SalamanderImage import SalamanderImage
from database import salamanders, load
from imageprocessing import create_salamander_image
from constants import min_good_match
from constants import match_dist
def match_image(salamander_image: SalamanderImage) -> Salamander:
salamanders = load()
bf = cv2.BFMatcher()
matches = []
good = []
salamander_match = []
# print (salamanders)
for id in salamanders:
salamander = salamanders[id]
# print('Matching with ', id)
for image in salamander.images:
for input_image in salamander_image:
match = bf.knnMatch(input_image.descriptors, image.descriptors, k=2)
matches.append(match)
for match in matches:
goodmatch = []
for m, n in match:
if m.distance < match_dist * n.distance:
goodmatch.append([m])
good.append([goodmatch])
is_same_image = input_image.filename == image.filename
if is_same_image:
del input_image
is_match = len(goodmatch) > min_good_match and not is_same_image
if is_match:
print('Match found :', salamander.id)
return salamander
return None
def num_of_good_matches(match):
salamander, good_matches = match
return good_matches
from cv2 import cv2
import numpy as np
import csv
def get_deeplabcut_data(path):
with open(path + 'videoDeepCut_resnet50_lizard-idMay10shuffle1_350000.csv', mode='r') as estimated_images:
image_data_reader = csv.reader(estimated_images)
results = []
i = 0
for pose in image_data_reader:
if i > 2:
points = np.array([
[pose[1], pose[2]],
[pose[4], pose[5]],
[pose[7], pose[8]],
[pose[10], pose[11]],
[pose[13], pose[14]],
]).astype(float)
img = cv2.imread('data/test-images/img' + str(i - 3) + '.png')
results.append((points, img))
i += 1
return results
def get_leap_data(path):
with open(path + 'positions.csv') as lines:
line_reader = csv.reader(lines)
i = 0
lines = []
for cur_line in line_reader:
lines.append(cur_line)
results = []
for i in range(len(lines)):
line = lines[i]
if i % 2 == 0:
nextline = lines[i+1]
points = np.array([
[nextline[0], line[0]],
[nextline[1], line[1]],
[nextline[2], line[2]],
[nextline[3], line[3]],
[nextline[4], line[4]],
]).astype(int) * 2
img = cv2.imread('data/test-images/img' + str(int(i/2)) + '.png')
results.append((points, img))
return results
def get_manual_data(path):
with open(path + 'manual.csv', mode='r') as labeled_images:
image_data_reader = csv.reader(labeled_images)
results = []
i = 0
for pose in image_data_reader:
if i == 0:
pose[0] = pose[0][3:]
points = np.array([
[pose[0], pose[1]],
[pose[2], pose[3]],
[pose[4], pose[5]],
[pose[6], pose[7]],
[pose[8], pose[9]],
]).astype(float)
img = cv2.imread('data/test-images/img' + str(i) + '.png')
results.append((points, img))
i += 1
return results
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment