JavaCV's camera combat thirteen: age detection

Get into the habit of writing together! This is the 8th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Welcome to my GitHub

All original works of Xinchen (including supporting source code) are classified and summarized here: github.com/zq2599/blog…

Overview of this article

  • This article is the thirteenth article of the "JavaCV's Camera Actual Combat" series. In the previous article "JavaCV's Camera Actual Combat No. 12: Gender Detection" , the application of gender recognition was developed with the help of the trained convolutional neural network model. Today in the previous article On the basis, make a few changes to realize the function of age recognition. The effect is as follows:

insert image description here

  • The main functions of the application are shown in the following figure:

insert image description here

  • If you have read other articles in the "JavaCV Camera Actual Combat" series, you will find that only the blue part in the above figure is new content, and the rest of the steps are fixed routines. Every application in the "JavaCV Camera Actual Combat" series can play All are the same routine: regardless of the number of steps, it is actually the same process

About gender and age detection

  • More technical details on inferring gender and age using convolutional neural networks are explained in more detail here:

talhassner.github.io/home/public…

  • This article will use a trained Caffe model, the data for training the model is from Flickr albums, assembled by automatic upload from iPhone5 (or later) smartphone devices, and released by its author under a Creative Commons (CC) license. Publicly released, a total of 26580 photos, involving 2284 people, the ages of these people were identified into eight groups: (0-2, 4-6, 8-13, 15-20, 25-32, 38-43, 48- 53, 60 -)
  • For more details on the data source, please refer to: talhassner.github.io/home/projec…
  • Paper address: talhassner.github.io/home/projec…

Source code download

  • 《JavaCV人脸识别三部曲》的完整源码可在GitHub下载到,地址和链接信息如下表所示(github.com/zq2599/blog…
名称 链接 备注
项目主页 github.com/zq2599/blog… 该项目在GitHub上的主页
git仓库地址(https) github.com/zq2599/blog… 该项目源码的仓库地址,https协议
git仓库地址(ssh) [email protected]:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
  • 这个git项目中有多个文件夹,本篇的源码在javacv-tutorials文件夹下,如下图红框所示:

insert image description here

  • javacv-tutorials里面有多个子工程,《JavaCV的摄像头实战》系列的代码在simple-grab-push工程下:

insert image description here

准备:文件下载

  • 本次实战需要三个文件:
  1. 人脸检测的模型文件:raw.github.com/opencv/open…
  2. 年龄识别的配置文件:raw.githubusercontent.com/GilLevi/Age…
  3. 年龄识别的模型文件:raw.githubusercontent.com/GilLevi/Age…

准备:代码接口简介

  • 编码前,先把涉及到的所有java文件说明一下:
  1. AbstractCameraApplication.java:主程序的抽象类,这里面定义了打开摄像头、抓取每一帧、处理每一帧的基本框架,避免每个应用都把这些事情重复做一遍
  2. PreviewCameraWithGenderAge.java:主程序,是AbstractCameraApplication的实现类,本次实战的核心功能人脸检测和年龄检测,都委托给它的成员变量detectService去完成
  3. DetectService.java:检测服务的接口,里面定义了几个重要的api,例如初始化、处理每一帧、释放资源等
  4. AgeDetectService.java:前文GenderDetectService的子类,仅仅是处理推理结果的逻辑与前文的性别识别略有不同,其余功能完全继承自性别识别
  • 以上代码,咱们已经在前文写过一次了,今天当然不需要重复再做一次,今天是在上述代码基础上做两处小幅度修改,接下来就开始吧

改动一:主程序(PreviewCameraWithGenderAge.java)

  • 卷积神经网络所需的配置和模型文件,是在主程序的main方法内设置的,上一章是性别检测,这里替换为年龄检测的文件,如下所示,请您将路径换为自己电脑上的文件路径:
    public static void main(String[] args) {
        String base = "E:\\temp\\202112\\25\\opencv\\";
        
        DetectService detectService = new AgeDetectService(
                base + "haarcascade_frontalface_alt.xml",
                base + "age\\deploy.prototxt",
                base + "age\\age_net.caffemodel");

        new PreviewCameraWithGenderAge(detectService).action(1000);
    }
复制代码

改动二:检测服务实现(GenderDetectService的子类)

  • 前文《性别检测》的核心功能都集中在GenderDetectService.java中,今天要做的年龄检测,除了推理结果的处理逻辑略有不同,其余功能与《性别检测》完全一致
  • 所以,实现年龄检测的最简单方法就是写一个子类继承GenderDetectService,这个子类中只有神经网络推理结果的处理逻辑,完整代码如下,注释中已经有了详细说明,就不多赘述了:
@Slf4j
public class AgeDetectService extends GenderDetectService {

    /**
     * 设置训练模型时划分的年龄段,所以推理结果也是这样的年龄段
     */
    private static final String[] AGES = new String[]{"0-2", "4-6", "8-13", "15-20", "25-32", "38-43", "48-53", "60-"};

    public AgeDetectService(String classifierModelFilePath, String cnnProtoFilePath, String cnnModelFilePath) {
        super(classifierModelFilePath, cnnProtoFilePath, cnnModelFilePath);
    }

    @Override
    protected String getDescriptionFromPredictResult(Mat prob) {
        DoublePointer pointer = new DoublePointer(new double[1]);
        Point max = new Point();
        
        // 把prob理解为一个数组,
        // 第一个元素是"0-2"的置信度
        // 第二个元素是"4-6"的置信度
        // minMaxLoc方法帮忙我们找出了置信度最高的元素,max是元素位置,pointer是这个元素的置信度
        minMaxLoc(prob, null, pointer, null, max, null);

        // 如果置信度太低,那就是"难以置信",就返回空字符串
        if (pointer.get()<0.6d) {
            return "";
        } else {
            // 如果置信度可信,就返回该元素对应的年龄范围
            return AGES[max.x()];
        }
    }
}
复制代码
  • 至此,编码完成,按套路出牌让咱们省下不少时间,接下来开始验证

验证

  • 确保摄像头工作正常,运行PreviewCameraWithGenderAge类的main方法(再次提醒,main方法中文件的位置,注意是年龄检测的模型文件,不是性别检测的)
  • 天气很冷,为了领到免费盒饭,群众演员早就等得不耐烦了,让他站在摄像头前,如下图,年龄识别成功,且实时展示:

在这里插入图片描述

  • So far, the function of integrating face detection and age detection in the local window preview has been completed. Thanks to the power of JavaCV, the whole process is so easy and pleasant. Next, please continue to pay attention to Xinchen Original, "JavaCV's Camera Actual Combat" series also More rich applications will be presented;

About gender + age recognition

  • If you have paid attention to the technical articles related to gender and age recognition on the Internet, you will find that these articles usually integrate the two recognitions together, and the effect of the code also inherits the two recognitions, as shown in the following figure:

在这里插入图片描述

  • You may also have questions: why doesn't Xinchen put the two identifications in one article and one demo, so that the content is more complete and the demo is more powerful?
  • The reason why the two articles are divided is because the routines of age and gender recognition are relatively close. If you finish speaking in one article, it is nothing more than writing a few more lines of code and typing a few more lines of words, which will not improve the technology. Each article only focuses on one function, and clearly explains the technical points such as initialization, use, and resource release. As for whether it is used alone or in combination, it is left to smart readers to freely combine according to their own needs.

Welcome to the Nuggets: Programmer Xin Chen

On the road of learning, you are not alone, Xinchen Original will accompany you all the way...

Guess you like

Origin juejin.im/post/7084029249048444941