Anonymizer  4.0
SDK for anonymization of human faces and licence plates in images.
 All Data Structures Functions Variables Typedefs Groups Pages
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 example_img.jpg * */
/* * use(Linux) : ./example example_img.jpg * */
/* * * */
/* * ------------------------------------------------------------ * */
/* * * */
/* * Eyedea Recognition, Ltd. (C) 2013, Mar 6th * */
/* * * */
/* * Contact: * */
/* * web: http://www.eyedea.cz * */
/* * email: info@eyedea.cz * */
/* * * */
/* **************************************************************** */
/* **************************************************************** */
#include <string>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
#include <explink.h> /* multi-platfoprm explicit linking macros */
#include <Anonymizer.h> /* Anonymizer API */
#define ANONYMIZER_SDK_DIR "../../AnonymizerSDK/"
#define ANONYMIZER_LIB ANONYMIZER_SDK_DIR "/lib/" LIB_PREFFIX "anonymizer.unsecured-" LIB_TARGET LIB_EXT
int fread_buffer(char *name, char **ppBuffer, size_t *length);
int fwrite_buffer(char *name, char *pBuffer, size_t length);
int main (int argc, const char ** argv)
{
string output = "image.jpg";
const string outputOpt = "-o=";
size_t outputOptLen = outputOpt.length();
const string helpOpt = "--help";
size_t helpOptLen = helpOpt.length();
string input;
/* ////////////////////////////////////////////////////////// */
/* 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"
"\nEXAMPLE:\n ./%s example_img.jpg\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_anAnonymizeBuffer fcnAnAnonymizeBuffer;
fcn_anFreeBuffer fcnAnFreeBuffer;
fcn_anAnonymizeJpegBuffer fcnAnAnonymizeJpegBuffer;
fcn_anFreeJpegBuffer fcnAnFreeJpegBuffer;
shlib_hnd hDll;
/* load shared library */
OPEN_SHLIB(hDll, ANONYMIZER_LIB);
if (!hDll){
printf(" library '%s' not loaded!\n", ANONYMIZER_LIB);
return -1;
}
/* link AnonymizerSDK functions */
LOAD_SHFCN(fcnAnInit, fcn_anInit, hDll, "anInit");
LOAD_SHFCN(fcnAnFree, fcn_anFree, hDll, "anFree");
LOAD_SHFCN(fcnAnAnonymizeBuffer, fcn_anAnonymizeBuffer, hDll, "anAnonymizeBuffer");
LOAD_SHFCN(fcnAnFreeBuffer, fcn_anFreeBuffer, hDll, "anFreeBuffer");
LOAD_SHFCN(fcnAnAnonymizeJpegBuffer, fcn_anAnonymizeJpegBuffer, hDll, "anAnonymizeJpegBuffer");
LOAD_SHFCN(fcnAnFreeJpegBuffer, fcn_anFreeJpegBuffer, hDll, "anFreeJpegBuffer");
if (!fcnAnInit || !fcnAnFree || !fcnAnAnonymizeBuffer || !fcnAnAnonymizeJpegBuffer || !fcnAnFreeJpegBuffer){
printf(" Loading Anonymizer functions failed!\n");
return -1;
}
printf(" ... done\n");
/* AnonymizerSDK API - Anonymizer initialization. Inputs are path to SDK package, configuration filename (default config.ini) */
void *pState; /* pointer to sdk state */
printf("Anonymizer initialization:\n");
if (!(pState = fcnAnInit((char*)ANONYMIZER_SDK_DIR, NULL)) ){
printf("failed.\n");
return -1;
}else
printf(" ... done.\n");
/* anonymization of JPEG buffer */
printf("JPEG buffer anonymization\n");
char *srcBuffer = NULL, *dstBuffer = NULL;
size_t srcLen = 0, dstLen = 0;
/* read image to buffer */
if (fread_buffer((char*)input.c_str(), &srcBuffer, &srcLen) != 0) {
fcnAnFree(pState);
return -1;
}
/* run anonymization */
if (fcnAnAnonymizeJpegBuffer(srcBuffer, srcLen, NULL, pState, &dstBuffer, &dstLen) != 0) {
printf(" ... FAILED!\n");
fcnAnFree(pState);
return -1;
}
/* write buffer to file */
if (fwrite_buffer((char*)((output + ".JpegBuffer.jpg").c_str()), dstBuffer, dstLen) != 0) {
fcnAnFree(pState);
return -1;
}
free(srcBuffer);
fcnAnFreeJpegBuffer(dstBuffer);
printf(" ...done\n");
/* anonymization of image buffer */
printf("Image data buffer anonymization\n");
cv::Mat image, imageRGB, imageAnonymized;
image = cv::imread(input.c_str(), cv::IMREAD_COLOR); // Read the file - BGR order
cv::cvtColor(image, imageRGB, cv::COLOR_BGR2RGB); // convert to RGB - input of Anonymizer
unsigned char *anonymizedBuffer = NULL; // anonymized buffer - ouptut of anonymization
cv::Size s = imageRGB.size();
/* run anonymization */
if (fcnAnAnonymizeBuffer(imageRGB.data, s.width,s.height, NULL, pState, &anonymizedBuffer) != 0) {
printf(" ... FAILED!\n");
fcnAnFree(pState);
return -1;
}
memcpy(imageRGB.data, anonymizedBuffer, imageRGB.total()*imageRGB.elemSize());// set anonymized data back to RGB image
cv::cvtColor(imageRGB, image, cv::COLOR_RGB2BGR); // convert to BGR
cv::imwrite((char*)((output + ".Buffer.jpg").c_str()), image); // and write
fcnAnFreeBuffer(anonymizedBuffer); // free buffer allocated by Anonymizer
printf(" ...done\n");
/* free Anonymizer state */
printf("Cleaning up\n");
fcnAnFree(pState);
/* free loaded library */
FREE_LIB(hDll);
printf(" ... done.\n");
return 0;
}
int fread_buffer(char *name, char **ppBuffer, size_t *length)
{
FILE *file;
char *buffer;
//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
buffer = (char *)malloc(*length + 1);
if (!buffer)
{
printf("Memory error!");
fclose(file);
return -1;
}
//Read file contents into buffer
fread(buffer, *length, 1, file);
fclose(file);
*ppBuffer = buffer;
return 0;
}
int fwrite_buffer(char *name, 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;
}