Anonymizer  5.0.0
SDK for anonymization of human faces and licence plates in images.
example-buffers.cpp

This is an example of how to use the Anonymizer library. This example shows how to explicitly link anonymizer library and run anonymization of a JPEG buffer and an image buffer. Example uses default AnParams parameters

Example example-buffers.cpp uses OpenCV library for image IO operation. Necessary documentation how to set up OpenCV is in examples/example-buffer/ directory.

/* **************************************************************** */
/* **************************************************************** */
/* * * */
/* * Anonymizer example console application * */
/* * This example shows how to link Anonymizer library, * */
/* * initialize it and run anonymization of image file. * */
/* * * */
/* * use(Windows): example.exe image.jpg * */
/* * use(Linux) : ./example image.jpg * */
/* * * */
/* * ------------------------------------------------------------ * */
/* * * */
/* * Eyedea Recognition, Ltd. (C) 2013, Mar 6th * */
/* * * */
/* * Contact: * */
/* * web: http://www.eyedea.cz * */
/* * email: info@eyedea.cz * */
/* * * */
/* **************************************************************** */
/* **************************************************************** */
#include <stdio.h>
#include <string>
#include <opencv2/opencv.hpp>
using namespace std;
#include <er_explink.h> /* multi-platform explicit linking macros */
#include <Anonymizer.h> /* Anonymizer API */
#define ANONYMIZER_SDK_DIR "../../AnonymizerSDK/"
#define ANONYMIZER_LIB ANONYMIZER_SDK_DIR "/lib/" ER_LIB_PREFIX "anonymizer-" ER_LIB_TARGET ER_LIB_EXT
//#define ANONYMIZER_LIB ANONYMIZER_SDK_DIR "/lib/" ER_LIB_PREFIX "anonymizer.unsecured-" ER_LIB_TARGET "d" ER_LIB_EXT
#define ANONYMIZER_INI "config.ini"
int fread_buffer( char *name, unsigned char **ppBuffer, size_t *length);
int fwrite_buffer(char *name, unsigned char *pBuffer, size_t length);
int main (int argc, const char ** argv)
{
string output = "image_anonymized.jpg";
const string outputOpt = "-o=";
size_t outputOptLen = outputOpt.length();
const string helpOpt = "--help";
size_t helpOptLen = helpOpt.length();
string input = "../../data/image.jpg";
/* ////////////////////////////////////////////////////////// */
/* Reading input parameters */
/* */
for( int i = 1; i < argc; i++ )
{
int res = 0;
if( outputOpt.compare( 0, outputOptLen, argv[i], outputOptLen ) == 0 )
output.assign( argv[i] + outputOptLen);
else if( (res = helpOpt.compare( 0, helpOptLen, argv[i], helpOptLen )) == 0 ){
printf("USAGE: %s [options] [filename]\n"
" options: -o=filename sets the output filename for anonymized image\n"
" filename: input image filename, default ../../data/image.jpg\n"
"\nEXAMPLE:\n %s\n", argv[0], argv[0]);
return 0;
}
else if( argv[i][0] == '-' )
{
printf("WARNING: Unknown option %s\nSee `%s --help' for more information.\n",argv[i], argv[0]);
}
else
input.assign( argv[i] );
}
if( input.empty() ){
printf("ERROR: Input filename is missing.\nTry `%s --help' for more information.\n", argv[0]);
return -1;
}
/* EXPLICIT LINKING */
printf("Loading library and functions\n");
fcn_anInit fcnAnInit;
fcn_anFree fcnAnFree;
fcn_anAnonymizeImageBuffer fcnAnAnonymizeImageBuffer;
fcn_anFreeBuffer fcnAnFreeBuffer;
fcn_anAnonymizeJpegBuffer fcnAnAnonymizeJpegBuffer;
fcn_anVersion fcnAnVersion;
fcn_anGetErrorMsg fcnAnGetErrorMsg;
shlib_hnd hDll;
/* load shared library */
ER_OPEN_SHLIB(hDll, ANONYMIZER_LIB);
if (!hDll){
printf(" library '%s' not loaded!\n", ANONYMIZER_LIB);
return -1;
}
/* link AnonymizerSDK functions */
ER_LOAD_SHFCN(fcnAnInit, fcn_anInit, hDll, "anInit");
ER_LOAD_SHFCN(fcnAnFree, fcn_anFree, hDll, "anFree");
ER_LOAD_SHFCN(fcnAnAnonymizeImageBuffer, fcn_anAnonymizeImageBuffer, hDll, "anAnonymizeImageBuffer");
ER_LOAD_SHFCN(fcnAnFreeBuffer, fcn_anFreeBuffer, hDll, "anFreeBuffer");
ER_LOAD_SHFCN(fcnAnAnonymizeJpegBuffer, fcn_anAnonymizeJpegBuffer, hDll, "anAnonymizeJpegBuffer");
ER_LOAD_SHFCN(fcnAnVersion, fcn_anVersion, hDll, "anVersion");
ER_LOAD_SHFCN(fcnAnGetErrorMsg, fcn_anGetErrorMsg, hDll, "anGetErrorMsg");
if (!fcnAnInit || !fcnAnFree || !fcnAnAnonymizeImageBuffer || !fcnAnAnonymizeJpegBuffer || !fcnAnVersion || !fcnAnGetErrorMsg){
printf(" Loading Anonymizer functions failed!\n");
return -1;
}
printf(" ... done\n");
printf("Loaded %s\n", fcnAnVersion(0));
/* AnonymizerSDK API - Anonymizer initialization. Inputs are path to SDK package, configuration filename (default config.ini) */
ANState state; /* sdk state handler*/
int ern;
printf("Anonymizer initialization:\n");
if ( (ern=fcnAnInit((char*)ANONYMIZER_SDK_DIR, ANONYMIZER_INI, &state))!=0 ){
printf("failed!\n%s\n", fcnAnGetErrorMsg(ern));
return -1;
}else
printf(" ... done.\n");
/* anonymization of JPEG buffer */
printf("JPEG buffer anonymization\n");
AnBuffer srcBuffer, dstBuffer;
size_t srcLen = 0, dstLen = 0;
/* read image to buffer */
if (fread_buffer((char*)input.c_str(), &(srcBuffer.raw_data), &(srcBuffer.length)) != 0) {
fcnAnFree(state);
return -1;
}
/* run anonymization */
if (ern=fcnAnAnonymizeJpegBuffer(srcBuffer, NULL, state, &dstBuffer) != 0) {
printf(" ... failed!\n%s\n",fcnAnGetErrorMsg(ern));
fcnAnFree(state);
return -1;
}
/* write buffer to file */
if (fwrite_buffer((char*)((output + ".JpegBuffer.jpg").c_str()), dstBuffer.raw_data, dstBuffer.length) != 0) {
fcnAnFree(state);
return -1;
}
delete [] srcBuffer.raw_data;
fcnAnFreeBuffer(dstBuffer);
printf(" ...done\n");
/* anonymization of image buffer */
printf("Image data buffer anonymization\n");
cv::Mat imageBGR, imageAnonymized;
imageBGR = cv::imread(input.c_str(), cv::IMREAD_COLOR); // Read the file - BGR order
srcBuffer.raw_data = imageBGR.data;
unsigned char *anonymizedBuffer = NULL; // anonymized buffer - ouptut of anonymization
cv::Size s = imageBGR.size();
/* run anonymization */
if (fcnAnAnonymizeImageBuffer(srcBuffer, s.width,s.height, NULL, state, &dstBuffer) != 0) {
printf(" ... failed!\n%s\n",fcnAnGetErrorMsg(ern));
fcnAnFree(state);
return -1;
}
memcpy(imageBGR.data, dstBuffer.raw_data, imageBGR.total()*imageBGR.elemSize());// set anonymized data back to BGR image
cv::imwrite(output + ".Buffer.jpg", imageBGR); // and write
fcnAnFreeBuffer(dstBuffer); // free buffer allocated by Anonymizer
printf(" ...done\n");
/* free Anonymizer state */
printf("Cleaning up\n");
fcnAnFree(state);
/* free loaded library */
ER_FREE_LIB(hDll); // NOTE: unloading secured library may cause application crash (Windows, randomly)
printf(" ... done.\n");
return 0;
}
int fread_buffer(char *name, unsigned char **buffer_, size_t *length)
{
FILE *file;
//Open file
file = fopen(name, "rb");
if (!file)
{
printf("Unable to open file %s", name);
return -1;
}
//Get file length
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
//Allocate memory
unsigned char *buffer;
try {
buffer = new unsigned char[*length + 1];
}
catch (std::bad_alloc e) {
printf("%s\n", e.what());
fclose(file);
return -1;
}
//Read file contents into buffer
fread(buffer, *length, 1, file);
fclose(file);
*buffer_ = buffer;
return 0;
}
int fwrite_buffer(char *name, unsigned char *pBuffer, size_t length)
{
FILE *file = fopen(name, "wb");
if (!file)
{
printf("Writing buffer to file failed!\n");
return -1;
}
fwrite(pBuffer, length, 1, file);
fclose(file);
return 0;
}