阿里云高校计划视觉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);
    }
}

MainController——控制进程

异常情况下clear

if (faceImages.size() != backImages.size()) {
    faceImages.clear();
    backImages.clear();
    faceResults.clear();
    backResults.clear();
    }

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

 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));
    }

上传文件判定

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";
}

存储逻辑

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;
}

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();
}

调用逻辑

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>>(){});
}

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>

识别后展示

<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>

application.properties——若干配置文件

server.port=
file.uplaod.path=
aliapi.accessKeyId=
aliapi.accessKeySecret=

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>
相关文章
|
1月前
|
人工智能 文字识别 监控
|
1月前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
1月前
|
云安全 人工智能 自然语言处理
阿里云x硅基流动:AI安全护栏助力构建可信模型生态
阿里云AI安全护栏:大模型的“智能过滤系统”。
|
1月前
|
人工智能 vr&ar UED
获奖公布|第十九届"挑战杯"竞赛2025年度中国青年科技创新"揭榜挂帅"擂台赛阿里云“AI技术助力乡村振兴”专题赛拟授奖名单公示
获奖公布|第十九届"挑战杯"竞赛2025年度中国青年科技创新"揭榜挂帅"擂台赛阿里云“AI技术助力乡村振兴”专题赛拟授奖名单公示
|
1月前
|
人工智能 数据处理 API
阿里云、Ververica、Confluent 与 LinkedIn 携手推进流式创新,共筑基于 Apache Flink Agents 的智能体 AI 未来
Apache Flink Agents 是由阿里云、Ververica、Confluent 与 LinkedIn 联合推出的开源子项目,旨在基于 Flink 构建可扩展、事件驱动的生产级 AI 智能体框架,实现数据与智能的实时融合。
329 6
阿里云、Ververica、Confluent 与 LinkedIn 携手推进流式创新,共筑基于 Apache Flink Agents 的智能体 AI 未来
|
2月前
|
人工智能 安全 中间件
阿里云 AI 中间件重磅发布,打通 AI 应用落地“最后一公里”
9 月 26 日,2025 云栖大会 AI 中间件:AI 时代的中间件技术演进与创新实践论坛上,阿里云智能集团资深技术专家林清山发表主题演讲《未来已来:下一代 AI 中间件重磅发布,解锁 AI 应用架构新范式》,重磅发布阿里云 AI 中间件,提供面向分布式多 Agent 架构的基座,包括:AgentScope-Java(兼容 Spring AI Alibaba 生态),AI MQ(基于Apache RocketMQ 的 AI 能力升级),AI 网关 Higress,AI 注册与配置中心 Nacos,以及覆盖模型与算力的 AI 可观测体系。
782 43
|
2月前
|
消息中间件 人工智能 运维
事件驱动重塑 AI 数据链路:阿里云 EventBridge 发布 AI ETL 新范式
“一个简单的数据集成任务,开始时总是轻松愉快的,但随着业务扩展,数据源越来越多,格式越来越乱,整个数据链路就会变得一团糟。”陈涛在演讲中指出了当前 AI 数据处理的普遍困境。扩展难、运维难、稳定性差,这三大挑战已成为制约 AI 应用创新和落地的关键瓶颈。针对这些痛点,在2025云栖大会期间,阿里云重磅发布了事件驱动 AI ETL 新范式,其核心产品 EventBridge 通过深度集成 AI 能力,为开发者提供了一套革命性的解决方案,旨在彻底改变 AI 时代的数据准备与处理方式。
360 24
|
1月前
|
机器学习/深度学习 人工智能 Serverless
吉利汽车携手阿里云函数计算,打造新一代 AI 座舱推理引擎
当前吉利汽车研究院人工智能团队承担了吉利汽车座舱 AI 智能化的方案建设,在和阿里云的合作中,基于星睿智算中心 2.0 的 23.5EFLOPS 强大算力,构建 AI 混合云架构,面向百万级用户的实时推理计算引入阿里云函数计算的 Serverless GPU 算力集群,共同为智能座舱的交互和娱乐功能提供大模型推理业务服务,涵盖的场景如针对模糊指令的复杂意图解析、文生图、情感 TTS 等。
|
1月前
|
存储 人工智能 OLAP
AI Agent越用越笨?阿里云AnalyticDB「AI上下文工程」一招破解!
AI 上下文工程是管理大模型输入信息的系统化框架,解决提示工程中的幻觉、上下文溢出与信息冲突等问题。通过上下文的采集、存储、加工与调度,提升AI推理准确性与交互体验。AnalyticDB PostgreSQL 版提供增强 RAG、长记忆、Supabase 等能力,助力企业构建高效、稳定的 AI 应用。

热门文章

最新文章