README / recognition (1).py
Sorawit26's picture
Upload 8 files
ef9b1b9 verified
Raw History Blame Contribute Delete
14.4 kB
from collections import defaultdict
from deepface import DeepFace
import mysql.connector
import numpy as np
import chromadb
import random
import time
import uuid
import cv2
import sys
import os
from ultralytics import YOLO
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from PIL import Image
# Initialize MySQL connection
connection = mysql.connector.connect(
host="mydb.c3a48iwi2mip.ap-southeast-2.rds.amazonaws.com", user="admin", password="0123456789", database="monitor_log"
)
command = connection.cursor()
class ImageHandler(FileSystemEventHandler):
def on_created(self, event):
# When a file is created
if not event.is_directory: # Skip directories
file_path = event.src_path
# Check if it's an image (you can modify extensions as needed)
if file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')):
print(f"New image added: {file_path}")
# Open and process the image
try:
global image
image = cv2.imread(file_path)
print(image)
head_helmet_detect = list(model(image,conf=0.67,classes=[0,1]))
for result in head_helmet_detect[0].boxes.data.tolist():
hx1,hy1,hx2,hy2,hconf,hlabel = result
if hlabel == 0:
print(f"\n\n\n\n{hx1} {hx2} {hy1} {hy2}\n\n\n\n")
cimage = image[int(hy1):int(hy2), int(hx1):int(hx2), :]
process = FaceDetection(capture=cimage)
process.verify() # Or perform any other operation
except Exception as e:
print(f"Error opening image: {e}")
# Face Detection using Deepface
class FaceDetection:
# initial set-up for new create object
def __init__(self, capture, db="facedb", root="faces"):
self.cap = capture
if not os.path.exists(root):
os.makedirs(root)
self.root = root
self.client = chromadb.PersistentClient(root)
self.db = self.client.get_or_create_collection(
name=db,
metadata={
"hnsw:space": 'cosine',
},
)
self.cropped_frame = None
self.identity = None
# detect and crop face from image
def detect(self, frame, threshold=0.5):
cropped_frame = None
boundary = None
try:
face = DeepFace.extract_faces(img_path=frame)
if face[0]['confidence'] >= threshold:
face_data = face[0]['facial_area']
x, y, w, h, leye, reye = face_data.values()
cropped_frame = frame[y:y+h, x:x+w]
COLOR = (0, 255, 0)
THICKNESS = 2
START_POINT = (x, y)
END_POINT = (x + w, y + h)
boundary = (frame, START_POINT, END_POINT, COLOR, THICKNESS)
except: pass
return cropped_frame, boundary
# manually register faces to database using folder
def manual_regis(self,folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files[:1]:
img_path = os.path.join(root, file)
img = cv2.imread(img_path)
self.cropped_frame, boundary = self.detect(img, 0.8)
if self.cropped_frame is not None and self.cropped_frame.size != 0:
try:
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
print(len(embedding['embedding']))
results = self.db.query(
query_embeddings=[embedding['embedding']], # Replace with your unknown embeddings
n_results=1
)
print(results)
except: print(0)
else:
full_name = os.path.normpath(root).split(os.sep)[-1]
print(full_name)
if results['distances'][0] and results['distances'][0][0] < 0.3:
exist_folder = results['metadatas'][0][0].get('name')
current_path = os.path.join(self.root, '__faces__', exist_folder)
new_path = os.path.join(self.root, '__faces__', full_name)
os.rename(current_path, new_path)
fcurrent_path = os.path.join(self.root, '__full_pics__', exist_folder)
fnew_path = os.path.join(self.root, '__full_pics__', full_name)
os.rename(fcurrent_path, fnew_path)
# Update database
if current_path != new_path:
for dirpath, dirnames, existfiles in os.walk(new_path):
for existfile in existfiles:
# Get filename
imgname, _ = os.path.splitext(existfile)
print(imgname)
record = self.db.get(imgname)
if record:
# Update chromadb
current_metadata = record['metadatas']
current_metadata[0]['name'] = full_name
self.db.update(imgname, metadatas=current_metadata)
# Update mySQL
sql = "UPDATE log SET name = %s WHERE id = %s"
val = (full_name, imgname)
command.execute(sql, val)
connection.commit()
# Save new data to database
idx = str(uuid.uuid4())
filename = idx + '.jpg'
path = os.path.join(self.root, '__faces__', full_name)
if not os.path.exists(path):
os.makedirs(path)
cv2.imwrite(os.path.join(path, filename), self.cropped_frame)
fpath = os.path.join(self.root, '__full_pics__', full_name)
if not os.path.exists(fpath):
os.makedirs(fpath)
cv2.imwrite(os.path.join(fpath, filename), img)
self.db.add(
ids=[idx],
embeddings=[embedding['embedding']],
metadatas=[{'name': full_name}]
)
# Insert to mySQL
sql = "INSERT INTO log (id, name) VALUES (%s, %s)"
val = (idx, full_name)
command.execute(sql, val)
connection.commit()
# Save other leftover faces in the folder
for file in files[1:]:
img_path = os.path.join(root, file)
img = cv2.imread(img_path)
self.cropped_frame, boundary = self.detect(img, 0.8)
if self.cropped_frame is not None and self.cropped_frame.size != 0:
try:
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
print(len(embedding['embedding']))
except: pass
else:
# Save new data to database
idx = str(uuid.uuid4())
filename = idx + '.jpg'
path = os.path.join(self.root, '__faces__', full_name)
cv2.imwrite(os.path.join(path, filename), self.cropped_frame)
fpath = os.path.join(self.root, '__full_pics__', full_name)
cv2.imwrite(os.path.join(fpath, filename), img)
self.db.add(
ids=[idx],
embeddings=[embedding['embedding']],
metadatas=[{'name': full_name}]
)
# Insert to mySQL
sql = "INSERT INTO log (id, name, scheck) VALUES (%s, %s)"
val = (idx, full_name)
command.execute(sql, val)
connection.commit()
print("Successfully registered faces in folder")
# register face to database (run automaticlly in the program sequence)
def auto_regis(self,name,key='0'):
if self.cropped_frame is not None and self.cropped_frame.size != 0:
try:
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
print(len(embedding['embedding']))
except: print(0)
else:
full_name = name
print(full_name)
if key == '0':
idx = str(uuid.uuid4())
else: idx = key
filename = idx + '.jpg'
path = os.path.join(self.root, '__faces__', full_name)
if not os.path.exists(path):
os.makedirs(path)
cv2.imwrite(os.path.join(path, filename), self.cropped_frame)
fpath = os.path.join(self.root, '__full_pics__', full_name)
if not os.path.exists(fpath):
os.makedirs(fpath)
cv2.imwrite(os.path.join(fpath, filename), image)
self.db.add(
ids=[idx],
embeddings=[embedding['embedding']],
metadatas=[{'name': full_name}]
)
# Insert to mySQL
sql = "INSERT INTO log (id, name, scheck) VALUES (%s, %s, %s)"
val = (idx, full_name, '1')
command.execute(sql, val)
connection.commit()
print("Successfully registered face")
# query matched face and save to mysql
def verify(self):
frame = self.cap
self.cropped_frame = frame
if self.cropped_frame is not None and self.cropped_frame.size != 0:
try:
embedding = DeepFace.represent(img_path=self.cropped_frame, enforce_detection=False)[0]
print(len(embedding['embedding']))
results = self.db.query(
query_embeddings=[embedding['embedding']], # Replace with your unknown embeddings
n_results=1
)
print(2)
except: print(0)
else:
print(results)
if results['distances'][0] and results['distances'][0][0] < 0.30:
name = results['metadatas'][0][0].get('name')
self.auto_regis(name)
else:
uniquekey = str(uuid.uuid4())
sql = "INSERT INTO anonymous_no (name) VALUES (%s)"
val = (uniquekey,)
command.execute(sql, val)
connection.commit()
sql = "SELECT number FROM anonymous_no WHERE name = (%s)"
val = (uniquekey,)
command.execute(sql,val)
numid = command.fetchall()
print(numid)
sql = "UPDATE anonymous_no SET name = 'anonymous' WHERE number = %s"
command.execute(sql,numid[0])
connection.commit()
name = "anonymous" + str(numid[0][0])
print(name)
self.auto_regis(name,uniquekey)
if __name__ == '__main__':
# Initialize YOLO model
model = YOLO('best.onnx', task="detect")
# Detect adding picture
data = sys.stdin.buffer.read()
np_data = np.frombuffer(data, np.uint8)
image = cv2.imdecode(np_data, cv2.IMREAD_COLOR)
head_helmet_detect = list(model(image,conf=0.65,classes=[0,1]))
for result in head_helmet_detect[0].boxes.data.tolist():
hx1,hy1,hx2,hy2,hconf,hlabel = result
if hlabel == 0:
print(f"\n\n\n\n{hx1} {hx2} {hy1} {hy2}\n\n\n\n")
image1 = image[int(hy1):int(hy2), int(hx1):int(hx2), :]
time.sleep(0.5)
process = FaceDetection(capture=image1)
process.verify() # Run Detection from cropped image
# set path folder
# folder_to_monitor = "img"
# event_handler = ImageHandler()
# observer = Observer()
# observer.schedule(event_handler, path=folder_to_monitor, recursive=False)
# print(f"Monitoring folder: {folder_to_monitor}")
# observer.start()
# try:
# while True:
# time.sleep(1) # Keep the script running
# except KeyboardInterrupt:
# observer.stop()
# observer.join()
# Close MySQL connection
connection.close()