This article provides a sample for how to import users via a Python script.  Code provided in this article is provided as-is without warranty of any kind.  Source code can be re-used in accordance with the MIT License.  


The following describes how to set up and run the SAFR Person Import Script. This guide is intended for users who are familiar with running the command line. It would be helpful to have some basic knowledge of shell scripts, although this knowledge isn't required.


To run this script you will need the python sample script, a directory of images, and optionally a data file containing information about each face.  


Install Dependencies

To run the script you will need to install the following python dependencies:

  • requests
  • pandas
  • xlrd
  • openpyxl

The easiest way to do this is to download the attached requirements.txt and run the following command:

python -m pip install -r requirements.txt 


Download and Test Script

Download the attached Python script.  Run the following command to view the help:

python <path to script>/safr_people_import_cli.py -h


This will print the following:

usage: safr_people_import_cli.py [-h] -u USER_ID -p USER_PWD -ud USER_DIR
                                 [-e {int2,prod,local}] [-s SERVER]
                                 [-m {analyze,upload}] [-idf INPUT_DATA_FILE]
                                 [-odf OUTPUT_DATA_FILE] -id IMAGE_DIR [-da]
                                 [-dg] [-ds] [-mcpq MIN_CPQ] [-mfw MIN_FW]
                                 [-mfs MIN_FS] [-mfc MIN_FC] [-moccl MAX_OCCL]
                                 [-move] [-debug]
                                 [-debug_count DEBUG_MAX_COUNT]

Example:
import_people.py -u usr -p pwd -d main -e prod -detect_age -detect_gender -detect_sentiment
        -data_file myfile.xlsx -images_dir "C:/images"

Data file should be CSV or XLS format. The first line should be column headings like this:
   firstName,lastName,externalId,imageFilename,company
   John,Smith,1234567,1234567.jpg,mycompany

command line arguments:
  -h, --help            show this help message and exit
  -u USER_ID            SAFR Account Username
  -p USER_PWD           SAFR Account Password
  -ud USER_DIR          SAFR User Directory
  -e {int2,prod,local}  SAFR Environment {int2, prod (default) or local}
  -s SERVER             SAFR Local Server IP or Hostname if env=local
  -m {analyze,upload}   Mode (analyze or upload)
  -idf INPUT_DATA_FILE  Input Data File (CSV or XLS) with user attributes to add to person records. See help.
  -odf OUTPUT_DATA_FILE
                        Output Data File (CSV or XLS) to write face metrics (and person id if adding faces).
  -id IMAGE_DIR         Images Folder
  -da                   Detect Age
  -dg                   Detect Gender
  -ds                   Detect Sentiment
  -mcpq MIN_CPQ         Min Center Pose Quality (CPQ)
  -mfw MIN_FW           Min Face Width
  -mfs MIN_FS           Min Face Sharpness
  -mfc MIN_FC           Min Face Contrast
  -moccl MAX_OCCL       Max Face Occlusion
  -move                 Move low quality images to new "failed" directory
  -debug                Print debug output
  -debug_count DEBUG_MAX_COUNT
                        Max Face Count



Import Faces

When running the script you will need the following files

  • images - A directory with images
  • Input metadata file (optional) - a CSV or XLS file with one row per image.  See help above.


See above for example for how to run the script.