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

Finishing and Cleanup

parent 92802515
img/
\ No newline at end of file
img/
sim_scores.npy
utils/__pycache__/
......@@ -3,163 +3,73 @@ 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
from utils.imageHandling import convert
path = '/Users/krunal/Desktop/code/database'
# Vars
#path = '/Users/krunal/Desktop/code/database'
projectpath = 'C:/Users/maria/Desktop/Biometrics/TermPaper/imt4126-biometricqualitymetrics/'
imgsdir = 'img/Casia_Converted/'
pardir = 'C:/Users/maria/Desktop/Biometrics/TermPaper/imt4126-biometricqualitymetrics/img/'
imgsnr = 10
# TODO change names
imgsnr = 2
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
# 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)
# 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
subjectnr = '00' + str(i) if i<10 else '0' + str(i)
subjectdir = subjectnr + '/'
fullpath = projectpath + imgsdir + subjectdir
img_obs.append([])
print("Read in for: " + str(i) + "-th subject")
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)
imgname = subjectnr + '_' + str(j)
#print(subjectdir + name)
#convert(projectpath + 'Casia/' + subjectdir + imgname + ".bmp", imgname + ".jpg", fullpath )
image = ins_get_image(fullpath + imgname)
face = app.get(image)
imgob = img_o(image, face, name)
imgob = img_o(image, face, imgname)
imgob.feat = handler.get(imgob.img, imgob.face[0])
img_obs[i].append(imgob)
# Calculate sim scores
sim_scores = []
for i in range(0, len(img_obs)):
print("Compute sim scores for: " + str(i) + "-th subject")
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)
sim_scores.append([])
pin = len(sim_scores)
sim_scores[pin-1].append(handler.compute_sim(img_obs[i][j].feat, img_obs[k][l].feat ))
sim_scores[pin-1].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")
#bh = BaseHandler(imgsnr, change_me)
# Save to file
new_array = np.array(sim_scores)
np.save("sim_scores.npy", new_array)
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')
......@@ -23,10 +23,7 @@ class BaseHandler:
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
......
from PIL import Image
import os
# TODO make better
def convert(img: str, new_name: str, new_path: str):
# Convert
img = Image.open(img)
# Save
isExist = os.path.exists(new_path)
if not isExist:
os.makedirs(new_path)
img.save( new_path + new_name, 'jpeg')
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