File size: 5,202 Bytes
901e379 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import sqlite3
import sys
import os
import os.path
import numpy as np
database_base_name = os.path.abspath(os.path.dirname(__file__)) + '\\person'
table_name = "feature"
sqlite_insert_blob_query = "INSERT INTO " + table_name + " (id, filename, count, boxes, landmarks, alignimgs, features) VALUES (?, ?, ?, ?, ?, ?, ?)"
sqlite_create_table_query = "CREATE TABLE " + table_name + " ( id INTEGER PRIMARY KEY, filename TEXT, count INTEGER, boxes BLOB NOT NULL, landmarks BLOB NOT NULL, alignimgs BLOB NOT NULL, features BLOB NOT NULL)"
sqlite_update_all_query = "UPDATE " + table_name + " set filename = ?, count = ?, boxes = ?, landmarks = ?, alignimgs = ?, features = ? where id = ?"
sqlite_search_query = "SELECT * FROM " + table_name
sqlite_delete_all = "DELETE FROM " + table_name
data_all = []
threshold = 68
max_feat_count = 8
max_id = -1
feature_update = False
face_database = None
#open databse
def open_database(db_no):
global max_id
global face_database
db_name = database_base_name + str(db_no) + ".db"
face_database = sqlite3.connect(db_name)
cursor = face_database.execute("SELECT name FROM sqlite_master WHERE type='table';")
#check tables exist in database
tables = [
v[0] for v in cursor.fetchall()
if v[0] != "sqlite_sequence"
]
cursor.close()
if not "feature" in tables:
face_database.execute(sqlite_create_table_query)
cursor = face_database.execute(sqlite_search_query)
#load index and feature in "feature table"
for row in cursor.fetchall():
id = row[0]
filename = row[1]
count = row[2]
boxes = np.fromstring(row[3], dtype=np.float32)
landmarks = np.fromstring(row[4], dtype=np.float32)
alignimgs = np.fromstring(row[5], dtype=np.uint8)
features = np.fromstring(row[6], dtype=np.float32)
if not boxes.shape[0] == count * 4:
continue
if not landmarks.shape[0] == count * 136:
continue
if not alignimgs.shape[0] == count * 49152:
continue
if not features.shape[0] == count * 256:
continue
boxes = boxes.reshape(count, 4)
landmarks = landmarks.reshape(count, 136)
alignimgs = alignimgs.reshape(count, 49152)
features = features.reshape(count, 256)
data_all.append({'id':id, 'filename':filename, 'count':count, 'boxes':boxes, 'landmarks':landmarks, 'alignimgs':alignimgs, 'features':features})
if id > max_id:
max_id = id
cursor.close()
#create database
def create_database():
db_no = 0
db_name = ""
while True:
db_name = database_base_name + str(db_no) + ".db"
if not os.path.isfile(db_name):
break
db_no += 1
open_database(db_no)
def clear_database():
global face_database
data_all.clear()
cursor = face_database.cursor()
cursor.execute(sqlite_delete_all)
face_database.commit()
cursor.close()
return
def register_face(filename, count, boxes, landmarks, alignimgs, features):
# boxes = boxes.reshape(count, 4)
# landmarks = landmarks.reshape(count, 136)
# alignimgs = alignimgs.reshape(count, 49152)
# features = features.reshape(count, 256)
global face_database
global max_id
max_id = max_id + 1
id = max_id
cursor = face_database.cursor()
cursor.execute(sqlite_insert_blob_query, (id, filename, count, boxes.tostring(), landmarks.tostring(), alignimgs.tostring(), features.tostring()))
face_database.commit()
cursor.close()
data_all.append({'id':id, 'filename':filename, 'count':count, 'boxes':boxes, 'landmarks':landmarks, 'alignimgs':alignimgs, 'features':features})
print('id = ', id)
return id
def update_face(id = None, filename = None, count = None, boxes = None, landmarks = None, alignimgs = None, features = None):
global face_database
cursor = face_database.cursor()
cursor.execute(sqlite_update_all_query, (filename, count, boxes.tostring(), landmarks.tostring(), alignimgs.tostring(), features.tostring(), id))
face_database.commit()
cursor.close()
def get_similarity(feat1, feat2):
return (np.sum(feat1 * feat2) + 1) * 50
def verify_face(feat):
global max_id
max_score = 0
for data in data_all:
id = data['id']
sub_id = data['count']
features = data['features']
# for sub_id in range(count):
score = get_similarity(feat, features)
if score >= max_score:
max_score = score
if score >= threshold:
print("score = ", score)
return id, data['filename'], sub_id
return -1, None, None
def get_info(id, sub_id):
for data in data_all:
nid = data['id']
if nid == id:
count = data['count']
if count < sub_id:
return data['filename'], data['boxes'][sub_id], data['landmarsk'][sub_id], data['alignimgs'][sub_id], data['features'][sub_id]
else:
return None, None, None, None, None
return None, None, None, None, None
def set_threshold(th):
threshold = th
def get_threshold():
return threshold
|