affectiva affdex问题,用于检测照片中的情绪

时间:2018-01-24 19:34:52

标签: c++ raspberry-pi imagemagick embedded-linux affdex-sdk

我正在尝试用affdex SDK分析一个图像(就像Abdelrahman Mahmoud link for tutorial所做的那样的seconde示例)我正在使用覆盆子pi 3b运行Raspbian拉伸与gcc 6.3.0

代码:



#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;
}
&#13;
&#13;
&#13;

当我用

运行脚本时
  

./ test-app image.jpg

我收到了这个错误(如果我使用.bmp图像也一样):

  

分段错误

我尝试了另一张图片,我收到了这个错误:

  

在抛出'Magick :: ErrorResourceLimit'的实例后调用终止** what():test-app:内存分配失败`download.png'@ error / png.c / ReadOnePNGImage / 2341   中止

任何指针,谢谢

1 个答案:

答案 0 :(得分:0)

pixels缓冲区不足以接受所有数据。

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);