|
|
import streamlit as st |
|
|
import cv2 |
|
|
import numpy as np |
|
|
import os |
|
|
from tqdm import tqdm |
|
|
from glob import glob |
|
|
import numpy as np |
|
|
import cv2 as cv2 |
|
|
import insightface |
|
|
from insightface.app import FaceAnalysis |
|
|
from insightface.data import get_image as ins_get_image |
|
|
import cv2 |
|
|
from insightface.app import FaceAnalysis |
|
|
import matplotlib.pyplot as plt |
|
|
import utils |
|
|
from utils import app |
|
|
|
|
|
def face_recognition_in_video(video_path, output_path, names, embeddings): |
|
|
|
|
|
cap = cv2.VideoCapture(video_path) |
|
|
|
|
|
|
|
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
|
fps = cap.get(cv2.CAP_PROP_FPS) |
|
|
|
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
|
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) |
|
|
|
|
|
|
|
|
while cap.isOpened(): |
|
|
ret, frame = cap.read() |
|
|
if not ret: |
|
|
break |
|
|
|
|
|
|
|
|
faces = app.get(frame) |
|
|
|
|
|
|
|
|
for face in faces: |
|
|
|
|
|
detected_embedding = face.normed_embedding |
|
|
|
|
|
|
|
|
scores = np.dot(detected_embedding, embeddings.T) |
|
|
scores = np.clip(scores, 0., 1.) |
|
|
|
|
|
|
|
|
idx = np.argmax(scores) |
|
|
max_score = scores[idx] |
|
|
|
|
|
|
|
|
threshold = 0.7 |
|
|
if max_score >= threshold: |
|
|
recognized_name = names[idx] |
|
|
else: |
|
|
recognized_name = "Unknown" |
|
|
|
|
|
|
|
|
bbox = face.bbox.astype(int) |
|
|
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2) |
|
|
|
|
|
cv2.putText(frame, recognized_name, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) |
|
|
|
|
|
|
|
|
out.write(frame) |
|
|
|
|
|
|
|
|
cap.release() |
|
|
out.release() |
|
|
return output_path |
|
|
|