File size: 2,259 Bytes
4268477 e070794 4268477 | 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 | import os
import cv2
import numpy as np
import streamlit as st
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
from glob import glob
from tqdm import tqdm
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
import shutil
import zipfile
import image_app
import utils
from utils import app
class FaceRecognitionTransformer(VideoTransformerBase):
def __init__(self):
self.app = FaceAnalysis(name='buffalo_l')
self.app.prepare(ctx_id=0, det_size=(640, 640))
self.names = None
self.embeddings = None
def _recognize_faces(self, frame):
if self.names is None or self.embeddings is None:
return frame
# Perform face analysis on the frame
faces = self.app.get(frame)
# Process each detected face separately
for face in faces:
# Retrieve the embedding for the detected face
detected_embedding = face.normed_embedding
# Calculate similarity scores with known embeddings
scores = np.dot(detected_embedding, np.array(self.embeddings).T)
scores = np.clip(scores, 0., 1.)
# Find the index with the highest score
idx = np.argmax(scores)
max_score = scores[idx]
# Check if the maximum score is above a certain threshold (adjust as needed)
threshold = 0.7
if max_score >= threshold:
recognized_name = self.names[idx]
else:
recognized_name = "Unknown"
# Draw bounding box around the detected face
bbox = face.bbox.astype(int)
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
# Write recognized name within the bounding box
cv2.putText(frame, recognized_name, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Debug print
print("Detected face:", recognized_name, "with confidence:", max_score)
return frame
def transform(self, frame):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = self._recognize_faces(frame)
return frame |