首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从照片中检测情感

从照片中检测情感
EN

Stack Overflow用户
提问于 2018-01-24 19:34:52
回答 1查看 302关注 0票数 0

我试着用affdex分析一个图像(就像Abdelrahman 教程链接做的第二个例子),我使用raspberry pi 3b运行Raspbian拉伸,用gcc 6.3.0

守则:

代码语言:javascript
运行
复制
#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;
}

当我运行脚本

/test-app image.jpg

我得到了这个错误(如果我使用.bmp映像的话也是一样):

分段故障

我尝试了另一个图像,得到了一个错误:

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

有什么建议吗,谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-07 15:32:58

pixels缓冲区不够大,无法接受所有数据。

代码语言:javascript
运行
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48430316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档