Commit e663e61f authored by Maria Kleppestø Mcculloch's avatar Maria Kleppestø Mcculloch
Browse files

Added scripts to compute pipeline

parent 1df20c14
img/
data/
sim_scores.npy
utils/__pycache__/
DET/
__pycache__/
from qualityEvaluation import QualityEvaluator
from utils.baseScore import BaseHandler
import numpy as np
# Deals with similarity scores
bh = BaseHandler(51, 5)
qe = QualityEvaluator()
qual_scores = qe.getBrisqueValues();
sim_scores = np.load('data/sim_scores_noise.npy')
gens = qe.getAllGenuine(bh, sim_scores)
imps = qe.getAllImposters(bh, sim_scores)
# Balck Purple Blue DarkGreen Red LightGreen BlackDots
colour = [ "Kitty", "Disco", "Rabbit","Dog" , "Card", "Mickey", "Mask"]
# Genereate sim scores
for i in range(1, 5):
#gq,iq = qe.removeLowQualValues(sim_scores, qual_scores, bh, i)
g_p,i_p = qe.removeLowQualValues(sim_scores, qual_scores, bh, i)
#g_p, i_p = qe.prepare(gq,iq)
print(max(i_p), " " , min(i_p))
print(len(g_p), " ", len(i_p))
#qe.plotDET(g_p, i_p, colour[i] )
qe.plotDET(gens, imps, colour[1] )
qe.showDET()
\ No newline at end of file
......@@ -3,16 +3,14 @@ import numpy as np
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
from utils.baseScore import BaseHandler
from utils.imageHandling import convert
# Vars
#path = '/Users/krunal/Desktop/code/database'
projectpath = 'C:/Users/maria/Desktop/Biometrics/TermPaper/imt4126-biometricqualitymetrics/'
imgsdir = 'img/Casia_Converted/'
imgsdir = 'img/Casia_Converted_Noise/Gaussian/'
# TODO change names
imgsnr = 2
imgsnr = 51
change_me = 5
img_obs = []
......@@ -26,6 +24,7 @@ class img_o:
# Set up detector and recognizer
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
#gc.collect()
handler = insightface.model_zoo.get_model('buffalo_l')
handler.prepare(ctx_id=0)
......@@ -34,6 +33,8 @@ for i in range (0, imgsnr):
subjectnr = '00' + str(i) if i<10 else '0' + str(i)
subjectdir = subjectnr + '/'
fullpath = projectpath + imgsdir + subjectdir
print("Path" + imgsdir + subjectdir)
img_obs.append([])
print("Read in for: " + str(i) + "-th subject")
......@@ -66,9 +67,10 @@ for i in range(0, len(img_obs)):
#bh = BaseHandler(imgsnr, change_me)
# Save to file
new_array = np.array(sim_scores)
np.save("sim_scores.npy", new_array)
np.save("data/sim_scores_noise.npy", new_array)
......
function computeBrisque()
high_path = "img/Casia_Converted";
% For all subjects
for i =1:51
if (i-1) <10
nr = "0" + (i-1);
else
nr = "" + (i-1);
end
sub_nr = "0" + nr;
for j=1:5
path = high_path + '/' + sub_nr + '/' + sub_nr + '_' + (j-1) + '.jpg';
img = imread(path);
quality_scores(i,j) = brisque(img);
end
end
norm_scores = Normalize_Q(quality_scores);
%disp(max(max(quality_scores)))
disp(sum(norm_scores))
disp(sum(norm_scores')')
writematrix( norm_scores, "data/brisque/quality_values.csv")
end
function q= Normalize_Q(qualityscores)
m = 1;
v = [0,0,0,0,0,0,0];
for i=1:51
for j=1:5
if qualityscores(i,j)< 38 && qualityscores(i,j) >= 34
m = 1;
elseif qualityscores(i,j)< 42 && qualityscores(i,j) >= 38
m = 2;
elseif qualityscores(i,j)< 44 && qualityscores(i,j) >= 42
m = 3;
elseif qualityscores(i,j)< 46 && qualityscores(i,j) >= 44
m = 4;
elseif qualityscores(i,j)< 48 && qualityscores(i,j) >= 46
m = 5;
elseif qualityscores(i,j) >= 48
m = 6;
disp("Hello")
end
v(m) = v(m) +1;
q(i,j) = m;
end
end
disp(v)
end
\ No newline at end of file
function generateArtificalData(Path)
high_path_destination = "img/Casia_Converted_Noise/Gaussian";
high_path_source = "img/Casia_Converted";
% For all subjects
for i =1:51
if (i-1) <10
nr = "0" + (i-1);
else
nr = "" + (i-1);
end
sub_nr = "0" + nr;
mkdir(high_path_destination + '/' + sub_nr )
for j=1:5
imgpath = '/' + sub_nr + '/' + sub_nr + '_' + (j-1) + '.jpg';
path_source = high_path_source + imgpath;
img = imread(path_source);
i = imnoise(img,'gaussian');
imwrite(i, high_path_destination + imgpath)
%J = imnoise(I,'gaussian')
%J = imnoise(I,'gaussian',m)
% J = imnoise(I,'gaussian',m,var_gauss)
% J = imnoise(I,'localvar',var_local)
% J = imnoise(I,'localvar',intensity_map,var_local)
% J = imnoise(I,'poisson')
% J = imnoise(I,'salt & pepper')
% J = imnoise(I,'salt & pepper',d)
% J = imnoise(I,'speckle')
% J = imnoise(I,'speckle',var_speckle)
end
end
end
\ No newline at end of file
import csv
from re import S
from webbrowser import get
import numpy as np
from utils.baseScore import BaseHandler
from DET.DET import DET
import matplotlib.pyplot as mpl
#bh = BaseHandler(51, 5)
class QualityEvaluator:
def __init__(self) -> None:
self.d = DET(biometric_evaluation_type = 'algorithm')
self.d.create_figure()
pass
def getBrisqueValues(self):
path_brisque = "data/"
with open('data/brisque/quality_values.csv', newline='') as csvfile:
data_s = list(csv.reader(csvfile))
data = [ [0]*len(data_s[0]) for i in range(len(data_s))]
for i in range(0, len(data_s)):
for j in range(0, len(data_s[0])):
data[i][j] = float(data_s[i][ j])
return data
# Rejection set
def getNotRejectedSimScores(self, bh: BaseHandler, sim_values: list, quality_values: list, threshhold_quality: list, ref_nr: int, probe_nr: int, threshold_simscore: int):
if len(threshhold_quality) != 2: return None
R = []
tot = 0;
below_threshhold = 0;
rejected = 0
# Get all genuine sim scores below a threshold not in the quality rejection set.
for i in range(0,51):
for j in range(5):
for k in range(5):
if not ((quality_values[i][j] < threshhold_quality[0]) and (quality_values[i][k] < threshhold_quality[1])):
tot = tot +1
if float(sim_values[bh.get( i, j, i, k)][0]) < threshold_simscore:
below_threshhold = below_threshhold + 1;
else:
rejected = rejected +1
#print("Rejected: ", rejected, " || Not rejected: ", tot, " || Percentage: ", (below_threshhold/tot)*100)
return below_threshhold/tot
# Find fraction between them, this is the FNMR
def plotFNMR(self, sim_scores, quality_scores, nr_quality_levels):
# PLot FMNR veruse the rejection rate.
quality_t = [3,3]
for i in range(1,7):
for j in range(1,7):
#pprint("\n[", i, ",", j, "]")
for k in range(10):
#print(k, "%: ",
self.getNotRejectedSimScores(sim_scores, quality_scores, [i,j], -1,-1, (0.1*k))#*100, "%")
def getAllImposters(self, bh: BaseHandler, sim_scores ):
imposters = []
for i in range(0, 51):
for j in range(0, 5):
pin = j
for k in range(i+1, 51):
for l in range (0, 5):
if (i!=k):
imposters.append(float(sim_scores[bh.get( i, j, k, l)][0]))
pin = 0
return imposters
def getAllGenuine(self, bh: BaseHandler, sim_scores):
genuine = []
for i in range(0, bh.subject_tot):
for j in range(0, bh.sample_tot):
pin = j
for k in range(i, bh.subject_tot):
for l in range (pin+1, bh.sample_tot):
if (i==k):
genuine.append(float(sim_scores[bh.get( i, j, k, l)][0]))
#print(sim_scores[bh.get( i, j, k, l)][1])
pin = 0
return genuine
def H(self, q1: int, q2: int):
return int((q1+q2)/2)
def eval_quality(self, q1: int, q2:int, k: int) -> bool:
return True
#return self.H(q1, q2) == k
def removeLowQualValues(self, sim_scores: list, quality_scores: list, bh: BaseHandler, ql:int) -> list:
#g_s, i_s = prepare(getAllGenuine(bh, sim_scores), getAllImposters(bh, sim_scores))
g_s_new = []
i_s_new = []
# For all subject combinations
for i in range(bh.subject_tot):
for j in range(bh.sample_tot):
for k in range(i, bh.subject_tot):
for l in range(bh.sample_tot):
#Save if quality scores exeeds requierments
q1 = quality_scores[i][j]
q2 = quality_scores[k][l]
s = sim_scores[bh.get(i,j,k,l)]
#print("S score: " + s[1], "|| ", i, " ", j, " x ", k, " ", l)
if (self.eval_quality(q1, q2, ql)):
if(i==k):
if k<l: g_s_new.append(float(s[0]))
else:
i_s_new.append(float(s[0]))
return g_s_new, i_s_new
def plotDET(self, genuine: list, imposters:list, col: str):
self.d.plot(np.array(genuine), np.array(imposters), col )
def showDET(self):
self.d.show()
def prepare(self, g: list, imps: list):
shift_by = 0.4 #np.ones(len(i))*0.4
il = []
gl = []
for i in range(len(imps)):
il.append(imps[i] + shift_by)
for i in range(len(g)):
gl.append(g[i])
return gl,il
class BaseHandler:
def __init__(self, subject_tot: int, sample_tot: int) -> None:
self.subject_tot = subject_tot
self.sample_tot = sample_tot
pass
'''
Takes in the subject number and sample number of two samples, and returns the index
of the correlating similarity score in the similarity_score list.
'''
def get(self, subject_nr_1: int, saple_nr_1: int, subject_nr_2: int, saple_nr_2: int):
n = self.subject_tot * self.sample_tot
......@@ -33,15 +36,11 @@ class BaseHandler:
indx = int((n*(n+1))/2) - int(((v*a+u)*(v*a+u+1))/2) + scnd_element
return indx
def get_by_string(self, imgstr1: str, imgstr2:str):
# retrive numbers
return self.get(0,0,0,0)
def write(sim_scores: list) -> None:
return
def read() -> list:
return []
Supports Markdown
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