javafx+百度人脸识别sdk实现人脸检测

最近尝试写一个离线的人脸识别小程序,运用了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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值