Skip to content
Snippets Groups Projects
Commit 92802515 authored by Maria Kleppestø Mcculloch's avatar Maria Kleppestø Mcculloch
Browse files

First image

parent fdb7c312
No related branches found
No related tags found
No related merge requests found
img/
\ No newline at end of file
import cv2
import numpy as np
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
from PIL import Image
import os
from utils.baseScore import BaseHandler
path = '/Users/krunal/Desktop/code/database'
pardir = 'C:/Users/maria/Desktop/Biometrics/TermPaper/imt4126-biometricqualitymetrics/img/'
imgsnr = 10
change_me = 5
img_obs = []
# TODO make better
def convert(img: str, new_name: str, add_path: str):
img = Image.open(pardir + 'Casia/' + img)
fullpath = pardir + 'Casia_Converted/' + add_path
isExist = os.path.exists(fullpath)
if not isExist:
# Create a new directory because it does not exist
os.makedirs(fullpath)
print("The new directory is created!")
img.save( fullpath + new_name, 'jpeg')
# Check whether the specified path exists or not
# Set up detector and recognizer
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
handler = insightface.model_zoo.get_model('buffalo_l')
handler.prepare(ctx_id=0)
print("########################################################")
class img_o:
def __init__(self, img, face, name ):
self.img = img
self.face = face
self.feat = None
self.name = name
# Read in images, faces and features
for i in range (0, imgsnr):
nr = '00' + str(i) if i<10 else '0' + str(i)
path = nr + '/'
fullpath = pardir + 'Casia_Converted/' + path
img_obs.append([])
for j in range(0, change_me):
name = nr + '_' + str(j)
#print(path + name)
#convert(path + name + ".bmp", name + ".jpg", path )
image = ins_get_image(fullpath + name)
face = app.get(image)
imgob = img_o(image, face, name)
imgob.feat = handler.get(imgob.img, imgob.face[0])
img_obs[i].append(imgob)
sim_scores = []
for i in range(0, len(img_obs)):
for j in range(0, len(img_obs[i])):
pin = j
for k in range(i, len(img_obs)):
for l in range (pin, len(img_obs[k])):
sim_scores.append(img_obs[i][j].name + " x " + img_obs[k][l].name)
pin = 0
bh = BaseHandler(imgsnr, change_me)
# total number of instances
indx = bh.get(5,4,7,2,)
print("Res: " + str(sim_scores[indx]))
sim_imposter = handler.compute_sim(img_obs[0][0].feat, img_obs[3][0].feat )
sim_genuine = handler.compute_sim(img_obs[0][1].feat, img_obs[0][0].feat )
sim_exac = handler.compute_sim(img_obs[0][0].feat, img_obs[0][0].feat )
print("Final test, genuine: " + str(sim_genuine))
print("Final test, imposter: " + str(sim_imposter))
print("Final test, exac: " + str(sim_exac))
'''
img1 = ins_get_image(pardir + "img1")
img2 = ins_get_image(pardir + "img2")
img3 = ins_get_image(pardir + "img3")
img4 = ins_get_image(pardir + "img4")
face1 = app.get(img1)
face2 = app.get(img2)
face3 = app.get(img3)
face4 = app.get(img4)
feat1 = handler.get(img1, face1[0])
feat2 = handler.get(img2, face2[0])
feat3 = handler.get(img3, face3[0])
feat4 = handler.get(img4, face4[0])
# Compute matrix
sim_imposter = handler.compute_sim(feat1, feat2)
sim_exact = handler.compute_sim(feat1, feat3)
sim_genuine = handler.compute_sim(feat1, feat4)
print("Imposter: " + str(sim_imposter) + " || " + str(type(sim_imposter)))
print("Genuine: " + str(sim_genuine) + " || " + str(type(sim_genuine)))
print("Exac: " + str(sim_exact) + " || " + str(type(sim_genuine)))
# Save matrix to file
'''
'''
# -- Locate and load the database
datasetPath = ".\\"
compariosnMatrix = []
print("Hello darknes my old friend")
# Check for format
# -- Compute scores using the insightface api
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
print("Maria: " + str(type(app)))
app.prepare(ctx_id=0, det_size=(640, 640))
faces = app.get(img)
rimg = app.draw_on(img, faces)
cv2.imwrite( "t1_output.jpg", rimg)
# -- Write matrix to file
# TODO: What file format to use?
'''
\ No newline at end of file
from PIL import Image
pardir = 'C:/Users/maria/Desktop/Biometrics/TermPaper/imt4126-biometricqualitymetrics/img/'
img = Image.open(pardir + '001_2.bmp')
#new_img = img.resize( (256, 256) )
img.save( pardir + 'img4.jpg', 'jpeg')
File added
class BaseHandler:
def __init__(self, subject_tot: int, sample_tot: int) -> None:
self.subject_tot = subject_tot
self.sample_tot = sample_tot
pass
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
a = self.sample_tot
if(subject_nr_1<subject_nr_2 or (subject_nr_1 == subject_nr_2 and saple_nr_1< saple_nr_2)):
i = subject_nr_1
j = saple_nr_1
k = subject_nr_2
l = saple_nr_2
else:
k = subject_nr_1
l = saple_nr_1
i = subject_nr_2
j = saple_nr_2
i = subject_nr_1
j = saple_nr_1
k = subject_nr_2
l = saple_nr_2
v = self.subject_tot - i -1
u = self.sample_tot - j
scnd_element = k*a + l - (i * a + j)
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 []
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment