Race Scanner

(Input Scanner)

The Race Detector acts as a safeguard in our AI chatbot application, automatically detecting and blocking images to ensure a safe and respectful environment for all users.

Purpose and Scope

Purpose: To outline the workflow for detecting faces in an uploaded image, analyzing the race of the detected face using a deep learning model, and returning the race predictions.

Scope: This document covers the interactions between the race-detector service, the detect_face() function, the DeepFace library, and the temporary file system, including image uploading, face detection, race analysis, and cleanup procedures.

Decision Points

  • Faces Found: If no faces are detected in the uploaded image, the process terminates early with no race predictions.

  • Race Predictions: If faces are detected, race analysis is performed, and the top two race predictions are returned.

How it Works:

Step 1: User Uploads an Image File

  • Procedure: The user uploads an image file through the race-detector endpoint. The image is received and saved as a temporary file on the server.

Step 2: Call detect_face() Function

  • Procedure: The detect_face() function is invoked with the path of the temporary image file to detect and align faces in the image.

Step 3: Load Image

  • Procedure: Inside detect_face(), the image is loaded using dlib.load_rgb_image() to prepare it for processing.

Step 4: Resize Image

  • Procedure: The image is resized to fit within the maximum size limit while maintaining the aspect ratio to optimize face detection.

Step 5: Detect Faces

  • Procedure: The cnn_face_detector model is used to detect faces within the resized image. Detected face regions are identified.

Step 6: Check if Faces Found

  • Procedure: Determine if any faces were detected. If no faces are found, return an empty dictionary and end the process. If faces are found, proceed to the next step.

Step 7: Facial Landmarks

  • Procedure: The sp model is used to locate facial landmarks on the detected face, which are essential for accurate face alignment.

Step 8: Reverse Resize

  • Procedure: Adjust the coordinates of the detected face to match the original image dimensions using the resize ratio.

Step 9: Align Face

  • Procedure: The face is aligned using dlib.get_face_chips() based on the detected landmarks. The aligned face image is saved to a temporary file.

Step 10: Call DeepFace.analyze() to Predict Race

  • Procedure: The aligned face image is passed to the DeepFace.analyze() function to predict the race. The result includes probabilities for different race categories.

Step 11: Extract and Sort Race Information

  • Procedure: Extract the race prediction probabilities from the DeepFace result and sort them in descending order to determine the top two races.

Step 12: Return Top Two Race Predictions

  • Procedure: Return the top two race predictions as a dictionary containing race names and their respective probabilities.

Step 13: Clean Up

  • Procedure: Delete all temporary files created during the process to free up server space and avoid clutter.

Step 14: End

  • Procedure: The process concludes with the return of race predictions or an empty result if no faces were detected.

Race Detection Policy for AI Chatbot

Create a new policy as same as shown in LLM Guardrails Policy, for Race detection select scanner Race.

Optionally, perform a test to ensure the policy is functioning as intended. Upload a JPG or PNG image to detect Race.

Last updated