java opencv 嘴巴_Java和haarcascade面部和嘴巴检测 – 嘴作为鼻子

本文档描述了在Java和OpenCV中使用haarcascade进行面部和嘴巴检测的问题。开发者遇到的问题是,项目在某些情况下将鼻子误识别为嘴巴。代码示例展示了如何检测脸部和嘴巴,并通过SVM训练来识别微笑。尽管检测到正确案例,但在其他图像中存在误识别问题。
摘要由CSDN通过智能技术生成

今天我开始测试在

Java和OpenCv中检测到微笑的项目.识别脸和嘴项目使用haarcascade_frontalface_alt和haarcascade_mcs_mouth但我不明白为什么在某些原因项目检测鼻子作为一个嘴.

我有两种方法:

private ArrayList detectMouth(String filename) {

int i = 0;

ArrayList mouths = new ArrayList();

// reading image in grayscale from the given path

image = Highgui.imread(filename, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

MatOfRect faceDetections = new MatOfRect();

// detecting face(s) on given image and saving them to MatofRect object

faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

MatOfRect mouthDetections = new MatOfRect();

// detecting mouth(s) on given image and saving them to MatOfRect object

mouthDetector.detectMultiScale(image, mouthDetections);

System.out.println(String.format("Detected %s mouths", mouthDetections.toArray().length));

for (Rect face : faceDetections.toArray()) {

Mat outFace = image.submat(face);

// saving cropped face to picture

Highgui.imwrite("face" + i + ".png", outFace);

for (Rect mouth : mouthDetections.toArray()) {

// trying to find right mouth

// if the mouth is in the lower 2/5 of the face

// and the lower edge of mouth is above of the face

// and the horizontal center of the mouth is the enter of the face

if (mouth.y > face.y + face.height * 3 / 5 && mouth.y + mouth.height < face.y + face.height

&& Math.abs((mouth.x + mouth.width / 2)) - (face.x + face.width / 2) < face.width / 10) {

Mat outMouth = image.submat(mouth);

// resizing mouth to the unified size of trainSize

Imgproc.resize(outMouth, outMouth, trainSize);

mouths.add(outMouth);

// saving mouth to picture

Highgui.imwrite("mouth" + i + ".png", outMouth);

i++;

}

}

}

return mouths;

}

并检测到微笑

private void detectSmile(ArrayList mouths) {

trainSVM();

CvSVMParams params = new CvSVMParams();

// set linear kernel (no mapping, regression is done in the original feature space)

params.set_kernel_type(CvSVM.LINEAR);

// train SVM with images in trainingImages, labels in trainingLabels, given params with empty samples

clasificador = new CvSVM(trainingImages, trainingLabels, new Mat(), new Mat(), params);

// save generated SVM to file, so we can see what it generated

clasificador.save("svm.xml");

// loading previously saved file

clasificador.load("svm.xml");

// returnin, if there aren't any samples

if (mouths.isEmpty()) {

System.out.println("No mouth detected");

return;

}

for (Mat mouth : mouths) {

Mat out = new Mat();

// converting to 32 bit floating point in gray scale

mouth.convertTo(out, CvType.CV_32FC1);

if (clasificador.predict(out.reshape(1, 1)) == 1.0) {

System.out.println("Detected happy face");

} else {

System.out.println("Detected not a happy face");

}

}

}

例子:

对于那张照片

9b83D.jpg

正确检测这个单位:

ikQpM.png

但在其他图片

eaddc84f09df0a73074f381dea69c9b0.png

检测到鼻子

737dda25a51d4102b0b1470b6bfd4470.png

你的意见有什么问题?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值