Regarding doubt about multiprocessing in face recognition

Please provide complete information as applicable to your setup.

• Hardware Platform (Jetson / GPU)-Jetson Orin
• DeepStream Version-6.4
• JetPack Version (valid for Jetson only)-6.0+b106
• TensorRT Version-8.6.4
• NVIDIA GPU Driver Version (valid for GPU only)-12.2
• Issue Type( questions, new requirements, bugs)- we are doing face recognition using ultralight face detection after detect how can do multi processing in face recognition package and it should display support if it is detected 10 faces at a time in cctv camera then the 20 faces output should be display after a second can you give any solution for this by achieving better result

from concurrent.futures import ProcessPoolExecutor
import time
import os
from datetime import datetime
import pickle
from imutils import paths
import faiss
import cv2
import numpy as np
from flask import Flask
from flask import Flask, app, request,jsonify
import json
from watchdog.events import FileSystemEventHandler
from flask_cors import CORS, cross_origin
import requests
from watchdog.observers import Observer
import dlib
import face_recognition_models
import faiss
from tkinter import StringVar
import multiprocessing
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import Queue, Process
import threading
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
from tkinter import Tk, Label
from threading import Thread
from threading import Timer

folder_to_watch = ‘faces/’
app = Flask(name)

processed_obj_ids = set()
face_data = {}

face_encoding_array_nor_data = pickle.load(open(“face_encoding_data/face_encoding_array_nor_data.p”,‘rb’))
label_array_nor_data = pickle.load(open(“face_encoding_data/label_data_nor.p”,‘rb’))

print(face_encoding_array_nor_data.shape)

def l2_normalize(x, axis=-1, epsilon=1e-10):
output = x / np.sqrt(np.maximum(np.sum(np.square(x), axis=axis, keepdims=True), epsilon))
return output

try:
import face_recognition_models
except Exception:
print(“Please install face_recognition_models with this command before using face_recognition:\n”)
print(“pip install git+https://github.com/ageitgey/face_recognition_models”)
face_detector = dlib.get_frontal_face_detector()

predictor_68_point_model = face_recognition_models.pose_predictor_model_location()
pose_predictor_68_point = dlib.shape_predictor(predictor_68_point_model)

face_recognition_model = face_recognition_models.face_recognition_model_location()
face_encoder = dlib.face_recognition_model_v1(face_recognition_model)

def _css_to_rect(css):

"""
Convert a tuple in (top, right, bottom, left) order to a dlib `rect` object
"""
# print("css ",css)
# print(dlib.rectangle(css[3], css[0], css[1], css[2]))
return dlib.rectangle(css[3], css[0], css[1], css[2])

def _raw_face_landmarks(face_image, face_locations, model=“large”):
“”"
Returns an array of face landmarks
“”"
face_locations = [_css_to_rect(face_location) for face_location in face_locations]
# print(“face_locations”,face_locations)
# pose_predictor = pose_predictor_68_point
return [pose_predictor_68_point(face_image, face_location) for face_location in face_locations]

def face_landmarks(face_image, face_locations, model=“large”):
“”"
Returns face landmarks
“”"
landmarks = _raw_face_landmarks(face_image, face_locations, model)
landmarks_as_tuples = [[(p.x, p.y) for p in landmark.parts()] for landmark in landmarks]

if model == 'large':
    return [{
        "chin": points[0:17],
        "left_eyebrow": points[17:22],
        "right_eyebrow": points[22:27],
        "nose_bridge": points[27:31],
        "nose_tip": points[31:36],
        "left_eye": points[36:42],
        "right_eye": points[42:48],
        "top_lip": points[48:55] + [points[64]] + [points[63]] + [points[62]] + [points[61]] + [points[60]],
        "bottom_lip": points[54:60] + [points[48]] + [points[60]] + [points[67]] + [points[66]] + [points[65]] + [points[64]]
    } for points in landmarks_as_tuples]
else:
    raise ValueError("Invalid landmarks model type. Supported models are ['large'].")

def face_encodings(face_image, known_face_locations, num_jitters=1, model=“small”):
“”"
Returns face encodings
“”"
raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model)

print("raw_landmarks:",raw_landmarks)
return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]

Add known face encodings to the index

Replace face_encoding_array_nor_data with your actual known face encodings

known_face_encodings = np.array(face_encoding_array_nor_data).astype(‘float32’)
# Initialize FAISS index
d = 128 # Dimension of face encoding
index = faiss.IndexFlatL2(d) # L2 distance index

index.add(known_face_encodings)
result = ‘’
class ImageHandler(FileSystemEventHandler):

def __init__(self, executor, app):
    super().__init__()
    self.executor = executor
    self.app = app

def on_created(self, event):
    global index, result
    if not event.is_directory and event.src_path.endswith('.jpg'):
        loaded_image = event.src_path
        print(f"Processing image: {loaded_image}")

        # Verify if the file exists
        if os.path.exists(loaded_image):
            
            # Load image using OpenCV
            img = cv2.imread(loaded_image)
            if img is None:
                print(f"Error: Unable to load image at {loaded_image}. It might be corrupted or not in a supported format.")
                return

            
            # try:
            #     img = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)
            #     img = img[:, :, ::-1]  # Convert BGR to RGB
            # except cv2.error as e:
            #     print(f"OpenCV error: {e}")
            #     return

            
            print(f"Image size: {img.shape}")

            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # landmarks = pose_predictor_68_point(gray, face)
            # if landmarks.num_parts == 68:
            detected_faces = face_detector(img, 1)
            known_face_locations = [(d.top(), d.right(), d.bottom(), d.left()) for d in detected_faces]
            gray_img = np.expand_dims(gray, axis=2).repeat(3, axis=2)
            face_encoded = face_encodings(gray_img, known_face_locations, model="large", num_jitters=5)
            if face_encoded:
                face_nor = l2_normalize([face_encoded[0]])[0]
                face_nor = face_nor.reshape(1, -1).astype('float32')
                D, I = index.search(face_nor, 1)

                if D < 0.09:
                    if label_array_nor_data[I[0]][0] == "unknown":
                        print("Unknown")
                        result = "unknown"
                    else:
                        result = f"{label_array_nor_data[I[0]][0]}"
                        print("result:",result)
                elif D > 0.09 and not (label_array_nor_data[I[0]][0] == "unknown"):
                    print("Need to send AWS for face recognition")
                    result = "Need to send AWS"
                else:
                    print("Unknown")
            else:
                print("No matching face found")

        self.app.update_result(result)

• How to reproduce the issue ? (This is for bugs. Including which sample app is using, the configuration files content, the command line used and other details for reproducing)
• Requirement details( This is for new requirement. Including the module name-for which plugin or for which sample application, the function description)

Can you reformat the description with punctuation?

This’s a forum about DeepStream. You can refer to our Guide Samples to list your whole pipeline first, and then try to integrate your model into DeepStream.

There is no update from you for a period, assuming this is not an issue anymore. Hence we are closing this topic. If need further support, please open a new one. Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.