Search code examples
c++raspberry-piimagemagickembedded-linuxaffdex-sdk

affectiva affdex issue with detecting emotions from a photo


i’m trying to analyse an image with affdex SDK (like the seconde example done by Abdelrahman Mahmoud link for tutorial ) i’m using raspberry pi 3b running Raspbian stretch with gcc 6.3.0

the code :

#include <iostream>
#include <string>
#include "Magick++.h"
#include "PhotoDetector.h"

class Listener : public affdex::ImageListener{
	void onImageResults(std::map<affdex::FaceId,affdex::Face> faces,affdex::Frame image){
		std::cout << "Found Faces: "<< faces.size() << std::endl;

		for (auto pair : faces){
			std::string pronoun="they";
			std::string emotion="neutral";
			affdex::Face face=pair.second;

			if(face.appearance.gender == affdex::Gender::Male){
				pronoun="He";
			}
			else if(face.appearance.gender == affdex::Gender::Female){
				pronoun="She";
			}
			if(face.emotions.joy>25){
				emotion="Happy :)";
			}
			else if(face.emotions.sadness>25){
				emotion="Sad :(";
			}
			
			std::cout << face.id << " : " << pronoun << " looks " << emotion << std::endl;
		}
	};
	
	void onImageCapture(affdex::Frame image){};
};

int main(int argc, char ** argsv)
{
	
	//Initialize the imagemagick library
	Magick::InitializeMagick(*argsv);
	
	// Read Image into Memory
	Magick::Image img(argsv[1]);
	char * pixels = new char [img.columns() * img.rows() * 1];
	img.write(0,0,img.columns(), img.rows(), "RGB", MagickCore::StorageType::CharPixel, pixels);
	
	affdex::Frame frame(img.columns(), img.rows(), pixels, affdex::Frame::COLOR_FORMAT::BGR);
	
	affdex::PhotoDetector detector(1);
	affdex::ImageListener * listen = new Listener();
	
	detector.setImageListener(listen);
	
	detector.setClassifierPath("/home/pi/affdex-sdk/data");
	detector.setDetectAllEmotions(true);
	detector.setDetectAllAppearances(true);
	detector.setDetectAllExpressions(true);
	
	detector.start();
	detector.process(frame);
	detector.stop();

	delete listen;
	delete [] pixels;
	return 0;
}

when i run the script with

./test-app image.jpg

i got this error (the same if i use .bmp image ):

segmentation fault

i tried with an other image and i got this error :

terminate called after throwing an instance of ‘Magick::ErrorResourceLimit’ ** what(): test-app: memory allocation failed `download.png’ @ error/png.c/ReadOnePNGImage/2341 Aborted

Any pointers, thanks


Solution

  • The pixels buffer isn't large enough to accept all the data.

    size_t pixels_size = img.columns() // Width
                       * img.rows()    // Height
                       * sizeof(char)  // Size of storage
                       * 3;            // Number of parts per pixel ("RGB")
    char * pixels = new char [pixels_size];
    img.write(0,0,img.columns(), img.rows(), "RGB", MagickCore::StorageType::CharPixel, pixels);