最近尝试写一个离线的人脸识别小程序,运用了javafx和百度人脸识别的离线sdk,这里记录一下:
默认已经在百度人脸识别sdk中导入了javafx和hbuilder(百度sdk参考官方文档,javafx参考csdn)
核心难点是实时显示摄像头画面,这里我使用了java中的Timer类,用于定时更新更新ImageView中图像,而ImageView中的图像就是摄像头捕获的帧,代码如下:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
Platform.runLater(()->{
video.imageProperty().set(image);
}
);
}
}, 200,100);
参考百度sdk中的方法,通过opencv获取视频流,并调用封装好的接口进行识别,完整代码如下:
void startCamera(ActionEvent event){
/* sdk初始化 */
Face api = new Face();
String modelPath ="";
int res = api.sdkInit(modelPath);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
this.capture = new VideoCapture();
//默认笔记本摄像头
capture.open(0);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
Platform.runLater(()->{
Mat frame = grabFrame();
BufferedImage bufferedImage=ShowVideo.conver2Image(frame);
WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);
//这里的video为javafx中的ImageView类
video.imageProperty().set(image);
}
);
}
}, 200,100);
}
private Mat grabFrame(){
Mat frame = new Mat();
// check if the capture is open
if (this.capture.isOpened()) {
try {
// read the current frame
this.capture.read(frame);
// if the frame is not empty, process it
if (!frame.empty()) {
// face detection
long matAddr = frame.getNativeObjAddr();
FaceBox[] infos = Face.detect(matAddr, 0);
// 检测到人脸
if (infos != null && infos.length > 0) {
System.out.println("detect face");
return frame;
}
}
} catch (Exception e) {
System.err.println("Exception during the image elaboration: " + e);
}
}
return frame;
}