阿里云高校计划视觉AI五天训练营教程 Day 2 - 身份证识别系统搭建

简介: 借助阿里云视觉开放平台OCR实现身份证识别系统

初始界面

image.png

项目结构

SpringBootApplication——SpringBoot启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class IdCardorcApplication {
    public static void main(String[] args) {
        SpringApplication.run(IdCardorcApplication.class, args);
    }
}
AI 代码解读

MainController——控制进程

异常情况下clear

if (faceImages.size() != backImages.size()) {
    faceImages.clear();
    backImages.clear();
    faceResults.clear();
    backResults.clear();
    }
AI 代码解读

刷新页面重新加载已识别结果

 if (!CollectionUtils.isEmpty(faceImages) && faceImages.size() == backImages.size()) {
        model.addAttribute("faceImage", faceImages.get(faceImages.size() - 1));
        model.addAttribute("faceResult", faceResults.get(faceResults.size() - 1));
        model.addAttribute("backImage", backImages.get(backImages.size() - 1));
        model.addAttribute("backResult", backResults.get(backResults.size() - 1));
    }
AI 代码解读

上传文件判定

public String upload(@RequestParam("face") MultipartFile face, @RequestParam("back") MultipartFile back,
                     RedirectAttributes redirectAttributes) {
    if (face.isEmpty() || back.isEmpty()) {
        redirectAttributes.addFlashAttribute("messages", "请选择一个文件进行上传。");
        return "redirect:/index";
    }
    String errorMessages = null;
    Path dir = Paths.get(uploadDir);
    if (!Files.exists(dir)) {
        try {
            Files.createDirectories(dir);
        } catch (IOException e) {
            e.printStackTrace();
            errorMessages += e.getMessage() + "\n";
        }
    }
    try {
        if (!face.isEmpty()) {
            String filename = saveFile(face);
            Map<String, String> faceResult = ocrService.RecognizeIdCard(uploadDir + filename, "face");
            faceImages.add("/images/" + filename);
            faceResults.add(faceResult);
        }
        if (!back.isEmpty()) {
            String filename = saveFile(back);
            Map<String, String> faceResult = ocrService.RecognizeIdCard(uploadDir + filename, "back");
            backImages.add("/images/" + filename);
            backResults.add(faceResult);
        }
    } catch (Exception e) {
        e.printStackTrace();
        errorMessages += e.getMessage() + "\n";
    }
    if (StringUtils.isNoneBlank(errorMessages)) {
        redirectAttributes.addFlashAttribute("messages", errorMessages);
    }
    return "redirect:/index";
}
AI 代码解读

存储逻辑

public String saveFile(MultipartFile file) {
    String filename = UUID.randomUUID().toString() + "."
            + StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
    Path path = Paths.get(uploadDir + filename);
    System.out.println(faceResults);
    try {
        Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        return null;
    }
    return filename;
}
AI 代码解读

OcrService——通过SDK调用视觉开放平台的OCR

初始化config

private void init() throws Exception {
    Config config = new Config();
    config.endpointType = "access_key";
    config.regionId = "cn-shanghai";
    config.accessKeyId = accessKeyId;
    config.accessKeySecret = accessKeySecret;
    config.endpoint = "ocr.cn-shanghai.aliyuncs.com";

    orcClient = new Client(config);
    runtimeOptions = new RuntimeOptions();
}
AI 代码解读

调用逻辑

public Map<String, String> RecognizeIdCard(String fielpath, String side) throws Exception {
    RecognizeIdentityCardAdvanceRequest request = new RecognizeIdentityCardAdvanceRequest();
    request.imageURLObject = Files.newInputStream(Paths.get(fielpath));
    request.side = side;
    RecognizeIdentityCardResponse response = orcClient.recognizeIdentityCardAdvance(request, runtimeOptions);
    return "face".equals(side) ?
            JSON.parseObject(JSON.toJSONString(response.data.frontResult), new TypeReference<Map<String, String>>(){})
            : JSON.parseObject(JSON.toJSONString(response.data.backResult), new TypeReference<Map<String, String>>(){});
}
AI 代码解读

index.html——前端模板

上传界面

<form method="post" th:action="@{/upload}" enctype="multipart/form-data">
    <div class="row">
        <div class="col-sm-4">
            <div class="input-group">
                <input id="location" class="form-control" onclick="$('#i-face').click();">
                <label class="input-group-btn">
                    <input type="button" id="i-check" value="上传人像面" class="btn btn-primary" onclick="$('#i-face').click();">
                </label>
            </div>
        </div>
        <input type="file" name="face" id="i-face" accept=".jpg, .png, .jpeg" onchange="$('#location').val($('#i-face').val());" style="display: none">
        <div class="col-sm-4">
            <div class="input-group">
                <input id="locationf" class="form-control" onclick="$('#i-back').click();">
                <label class="input-group-btn">
                    <input type="button" id="i-checkb" value="上传国徽面" class="btn btn-primary" onclick="$('#i-back').click();">
                </label>
            </div>
        </div>
        <input type="file" name="back" id="i-back" accept=".jpg, .png, .jpeg" onchange="$('#locationf').val($('#i-back').val());" style="display: none">
        <div class="col-sm-4">
            <button type="submit" class="btn btn-primary form-control">开始识别</button>
        </div>
    </div>
</form>
AI 代码解读

识别后展示

<div class="row" style="margin-top: 38px;">
    <div class="col-md-12 mx-auto">
        <div class="row">
            <div class="col-sm-4">
                <img th:src="${faceImage}" th:if="faceImage != null" class="img-fluid">
            </div>
            <div class="col-sm-4">
                <img th:src="${backImage}" th:if="backImage != null" class="img-fluid">
            </div>
        </div>
    </div>
</div>
<div class="row" style="margin-top: 38px;">
    <div class="col-md-12 mx-auto" th:if="${faceResult != null}">
        <div class="row">
            <div class="col-sm-4">
                <p><span>姓名: </span><span th:text="${faceResult.name}"></span></p>
                <p><span>性别: </span><span th:text="${faceResult.gender}"></span></p>
                <p><span>民族: </span><span th:text="${faceResult.nationality}"></span></p>
                <p><span>出生日期: </span><span th:text="${faceResult.birthDate}"></span></p>
                <p><span>住址: </span><span th:text="${faceResult.address}"></span></p>
                <p><span>身份证号: </span><span th:text="${faceResult.IDNumber}"></span></p>
            </div>
             <div class="col-sm-4">
                <p><span>签发机关: </span><span th:text="${backResult.issue}"></span></p>
                <p><span>有效日期: </span><span th:text="${backResult.startDate}">-<span th:text="${faceResult.endDate}"></span></span></p>
             </div>
        </div>
    </div>
</div>
AI 代码解读

application.properties——若干配置文件

server.port=
file.uplaod.path=
aliapi.accessKeyId=
aliapi.accessKeySecret=
AI 代码解读

pom.xml——若干相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.10</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.4.8</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.52</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>ocr</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </dependency>
</dependencies>
AI 代码解读
目录
打赏
0
0
0
0
1
分享
相关文章
AI竟能独立完成顶会论文!The AI Scientist-v2:开源端到端AI自主科研系统,自动探索科学假设生成论文
The AI Scientist-v2 是由 Sakana AI 等机构开发的端到端自主科研系统,通过树搜索算法与视觉语言模型反馈实现科学假设生成、实验执行及论文撰写全流程自动化,其生成论文已通过国际顶会同行评审。
97 34
AI竟能独立完成顶会论文!The AI Scientist-v2:开源端到端AI自主科研系统,自动探索科学假设生成论文
星云智控科技-优雅草星云物联网AI智控系统软件产品技术栈一览表-优雅草卓伊凡
星云智控科技-优雅草星云物联网AI智控系统软件产品技术栈一览表-优雅草卓伊凡
34 7
星云智控科技-优雅草星云物联网AI智控系统软件产品技术栈一览表-优雅草卓伊凡
electron35-vue3-deepseek客户端流式输出AI对话系统
Electron35-DeepSeek桌面端AI系统|vue3.5+electron+arco客户端ai模板。2025跨平台ai实战electron35+vite6+arco仿DeepSeek/豆包ai流式打字聊天助手。
49 1
【05】20250416优雅草星云物联网AI智控系统从0开发鸿蒙端适配-deveco studio-增加告警中心相关卡片页面WarningCardWidget相关-增加Canvas 绘制折线图-Canvas 绘制柱状图-首页-优雅草卓伊凡
【05】20250416优雅草星云物联网AI智控系统从0开发鸿蒙端适配-deveco studio-增加告警中心相关卡片页面WarningCardWidget相关-增加Canvas 绘制折线图-Canvas 绘制柱状图-首页-优雅草卓伊凡
29 0
【05】20250416优雅草星云物联网AI智控系统从0开发鸿蒙端适配-deveco studio-增加告警中心相关卡片页面WarningCardWidget相关-增加Canvas 绘制折线图-Canvas 绘制柱状图-首页-优雅草卓伊凡
阿里云高校计划视觉AI五天训练营教程 Day 2 - 身份证识别系统搭建
实战讲述如何使用阿里云视觉平台API快速开发在线视觉AI平台,主要如何为如何搭建身份证识别系统,进而扩展到其他的识别系统。
阿里云高校计划视觉AI五天训练营教程 Day 2 - 身份证识别系统搭建
阿里云高校计划视觉AI五天训练营教程 Day 2 - 身份证识别系统搭建
本篇文章是基于阿里云高校计划AI课程第二讲,身份证识别系统搭建。在阿里云视觉开放平台上已有调试功能。
327 0
阿里云高校计划视觉AI五天训练营教程 Day 2 - 身份证识别系统搭建
视觉AI训练营-DAY 2 身份证识别系统搭建
身份证识别系统搭建-达摩院视觉智能实验室
zbm
291 0
视觉AI训练营-DAY 2 身份证识别系统搭建

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等