python输入代码无响应_为什么python这个代码会显示无响应?

大猫

1级

2018-05-13 回答

整个项目的结构图:

编写detectfacedemo.java,代码如下:

[java] view

plaincopyprint?

package com.njupt.zhb.test;

import org.opencv.core.core;

import org.opencv.core.mat;

import org.opencv.core.matofrect;

import org.opencv.core.point;

import org.opencv.core.rect;

import org.opencv.core.scalar;

import org.opencv.highgui.highgui;

import org.opencv.objdetect.cascadeclassifier;

//

// detects faces in an image, draws boxes around them, and writes the results

// to "facedetection.png".

//

public class detectfacedemo {

public void run() {

system.out.println("\nrunning detectfacedemo");

system.out.println(getclass().getresource("lbpcascade_frontalface.xml").getpath());

// create a face detector from the cascade file in the resources

// directory.

//cascadeclassifier facedetector = new cascadeclassifier(getclass().getresource("lbpcascade_frontalface.xml").getpath());

//mat image = highgui.imread(getclass().getresource("lena.png").getpath());

//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误

/*

* detected 0 faces writing facedetection.png libpng warning: image

* width is zero in ihdr libpng warning: image height is zero in ihdr

* libpng error: invalid ihdr data

*/

//因此,我们将第一个字符去掉

string xmlfilepath=getclass().getresource("lbpcascade_frontalface.xml").getpath().substring(1);

cascadeclassifier facedetector = new cascadeclassifier(xmlfilepath);

mat image = highgui.imread(getclass().getresource("we.jpg").getpath().substring(1));

// detect faces in the image.

// matofrect is a special container class for rect.

matofrect facedetections = new matofrect();

facedetector.detectmultiscale(image, facedetections);

system.out.println(string.format("detected %s faces", facedetections.toarray().length));

// draw a bounding box around each face.

for (rect rect : facedetections.toarray()) {

core.rectangle(image, new point(rect.x, rect.y), new point(rect.x + rect.width, rect.y + rect.height), new scalar(0, 255, 0));

}

// save the visualized detection.

string filename = "facedetection.png";

system.out.println(string.format("writing %s", filename));

highgui.imwrite(filename, image);

}

}

package com.njupt.zhb.test;

import org.opencv.core.core;

import org.opencv.core.mat;

import org.opencv.core.matofrect;

import org.opencv.core.point;

import org.opencv.core.rect;

import org.opencv.core.scalar;

import org.opencv.highgui.highgui;

import org.opencv.objdetect.cascadeclassifier;

//

// detects faces in an image, draws boxes around them, and writes the results

// to "facedetection.png".

//

public class detectfacedemo {

public void run() {

system.out.println("\nrunning detectfacedemo");

system.out.println(getclass().getresource("lbpcascade_frontalface.xml").getpath());

// create a face detector from the cascade file in the resources

// directory.

//cascadeclassifier facedetector = new cascadeclassifier(getclass().getresource("lbpcascade_frontalface.xml").getpath());

//mat image = highgui.imread(getclass().getresource("lena.png").getpath());

//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误

/*

* detected 0 faces writing facedetection.png libpng warning: image

* width is zero in ihdr libpng warning: image height is zero in ihdr

* libpng error: invalid ihdr data

*/

//因此,我们将第一个字符去掉

string xmlfilepath=getclass().getresource("lbpcascade_frontalface.xml").getpath().substring(1);

cascadeclassifier facedetector = new cascadeclassifier(xmlfilepath);

mat image = highgui.imread(getclass().getresource("we.jpg").getpath().substring(1));

// detect faces in the image.

// matofrect is a special container class for rect.

matofrect facedetections = new matofrect();

facedetector.detectmultiscale(image, facedetections);

system.out.println(string.format("detected %s faces", facedetections.toarray().length));

// draw a bounding box around each face.

for (rect rect : facedetections.toarray()) {

core.rectangle(image, new point(rect.x, rect.y), new point(rect.x + rect.width, rect.y + rect.height), new scalar(0, 255, 0));

}

// save the visualized detection.

string filename = "facedetection.png";

system.out.println(string.format("writing %s", filename));

highgui.imwrite(filename, image);

}

}

3.编写测试类:

[java] view

plaincopyprint?

package com.njupt.zhb.test;

public class testmain {

public static void main(string[] args) {

system.out.println("hello, opencv");

// load the native library.

system.loadlibrary("opencv_java246");

new detectfacedemo().run();

}

}

//运行结果:

//hello, opencv

//

//running detectfacedemo

///e:/eclipse_jee/workspace/javaopencv246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//detected 8 faces

//writing facedetection.png

package com.njupt.zhb.test;

public class testmain {

public static void main(string[] args) {

system.out.println("hello, opencv");

// load the native library.

system.loadlibrary("opencv_java246");

new detectfacedemo().run();

}

}

//运行结果:

//hello, opencv

//

//running detectfacedemo

///e:/eclipse_jee/workspace/javaopencv246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//detected 8 faces

//writing facedetection.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值