第三节课——【文字识别项目讲解及使用说明】(一)|学习笔记

本文涉及的产品
视觉智能开放平台,图像资源包5000点
视觉智能开放平台,分割抠图1万点
视觉智能开放平台,视频资源包5000点
简介: 快速学习第三节课——【文字识别项目讲解及使用说明】

开发者学堂课程【达摩院视觉 AI 精品课第三节课——【文字识别项目讲解及使用说明】笔记与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/912/detail/14420


第三节课——【文字识别项目讲解及使用说明】(一)


内容介绍:
一、如何使用 SDK

二、介绍 Request 阶层

三、OcrService 的实现逻辑


一、如何使用 SDK

首先进入智能视觉开放平台官网:http://vision.aliyun.com/,点击文字识别-身份证识别,点击查看产品文档,点击 SDK 参考,对 Java 来说有两种SDK 的选择,第一种是通用的 SDK,这里选择使用第二种的支持本地上传的 SDK。

接着复制下方说明里的链接粘贴到网页在最后加一个ocr/,目前代码使用的版本是1.0.3,点击就可以看到Maven坐标,就是代码里使用 SDK 的坐标了。

接下来是整个项目的实现路径和其中需要注意的点和方法:

<html lang="en" xmlns:th="http:/ /www.thymeleaf.org">

<head>

<title>VIAPI</title>

<link rel=”stylesheet" href=https://cdn.bootcss.com/bootstrap/3. 3.7/css/bootstrap.min.css>

<script src="https://apps.bdimg. com/libs/jquery/2.1.4/jquery.min.js"</script>

</head>

<body>

<div class="container">

<div class="row">

<div class="col-md-12 mx-auto">

<h2>VIAPI RecognizeIdentityCard Example</h2>

<div class=”col-sm-12">

<p th:text="${message}" th:if="$imessage ne null}" class="alert alert-primary"></p

</div>

<form method="post" th:action="@{/upload}" enctype="multipart/form-data">

<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,.p1g,.jpeg" onchange=" Ş('#location').val(Ş('#i-face'). val());" style=”display: none”>

<div class="col-sm-4">

<div class="input-group">

<input id='location1' class="form-control" onclick=”Ş('#i-back ' ) .click();"

<label class=”input-group-btn">

<input type="button"id="i-check-1" 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="Ş('#location1').val(Ş('#i-back '),val()):" style=”…”>

<div class="col-sm-4">

<button type="submit" class="btn btn-primary">开始识别</button>

</div>

</form>

</div>

</div> 

<div class="row" style="margin-top: 30px; ">

<div class=col-md-12 mx-auto">

<div class="col-sm-4>

<img style="width: 100%;" th:src="${faceImagc}" th:if="$faceImage ne null}" class="img-fluid" alt=""/>

</div>

<div class="col-sm-4>

<img style=”width: 100%;" th:src="${backImage}" th:if="${backImage ne null}" class"

mg-fluid" alt=""/>

</div>

</div>

</div>

<div class="row" style="…">

<div class="col-md-12 mx-auto">

<div class="col-sm-4>

<p th:if="${faceResult ne null}"><span>姓名

</span><span th:text="${faceResult.name}">/span></p>

<p th:if="${faceResult ne null}"><span>性别:</span><span th:text="${faceResult.gender}">/span></p>

<p th:if="${faceResult ne null}"><span>民族

</span><span th:text="${faceResult.nationality}">/span></p>

<p th:if="${faceResult ne null}"><span>

出生日期:</span><span th:text="${faceResult.birthDate}">/span></p>

<p th:if="${faceResult ne null}"><span>

住址</span><span th:text="${faceResult.address}">/span></p>

<p th:if="${faceResult ne null}"><span>

身份证号码:</span><span th:text="${faceResult.IDNumber}">/span></p>

</div>

<div class-“col-sm-4”>

<p th:if="${backResult ne null}"><span>

签发机关:</span><span th:text="${backResult.issue}">/span></p>

<p th:if="${backResult ne null}"><span>

有效日期:</span><span th:text="${backResult.startDate}">/span>-<span th:text=”${backResult.click:”}></p>

</div>

</div>

</div>

</div>

</body>

效果如图:

image.png

以上就是一个前端页面结构以及它实现的逻辑点。

二、介绍 Request 阶层

@Controller

@RequestMapping("/ ")

public class MainController {

private String uploadDirectory;

private OcrService ocrService;

private List<String> faceImages;

private List<String> backImages;

private List<Map<String,String>> faceResults;

private List<Map<string,String>> backResults;

public MainController(@value(""[file.upload. path)" ) String uploadDirectory,OcrService ocrService){

this.uploadDirectory = uploadDirectory;

this.ocrService = ocrService;

faceImages = new ArrayList<>();

backimages = new ArrayList<>();

faceResults = new ArrayList<>();

backResults = new ArrayList<>();

}

private String saveFile(MultipartFile file) throws Exception {

String suffix = StringUtils.substringAfterLast(file.getOriginalFilenae(), separator: " . ");

String filename = UUID.randomUUID().toString() + "." + suffix;

Path path = Paths.get( first: uploadDirectory + filename);Files.copy(file.getInputStream(),path,StandardCopyOption.REPLACE_EXISTING);

return filename;

}

@RequestMapping()

public String index (Model model) {

if (faceimages.size() != backImages.size()) {

facelmages.clear();

backImages.clear();

faceResults.clearO);

backResults.clear(;

}

if (!CollectionUtils.isEmpty (facelmages) && faceimages.size() == backImages.size()){

model.addAttribute(s: "faceImage",faceImages.get(faceImages.size() - 1));

model.addAttribute(s: "faceResult",faceResults.get(faceResults.size() - 1));

model.addAttribute(s: "backlmage", backImages.get(backimages.size() - 1));model.addAttribute(s: "backResult", backResults.get(backResults.size() - 1));

}

return "index";

}

l RequestMaping 给 MainController 添加了注解

l 第一个 uploadDirectory 定义的上传图片文件本地保存地址

第二个 ocrService 是提供了一层调用视觉智能开放平台 OCR 能力的一层封装;

第三和第四的 faceImagesbackImages 是缓存了之上传图片的路径地址;

第四和第五的 faceImagesbackImages 是缓存了之前的识别

结果

l 因为没有用到数据库,所以在这里其实是在内存里面缓存了一个上传图片和识别结果的信息。这配置的本机的目录,现在这个目录是放在 resources 下的 static目录下的,方便进行一些件的存储和读取的操作。images 是相对于 static 下的自定义目录,相当于是存放图片类的据。


三、OcrService 的实现逻辑

@PostMapping("/upload")

public String uploadFile(@RequestParam("face”) MultipartFile face,@Requestaram("back") MultipartFi1le back,Redirectittributes attribute{

if (face.isEmpty() | | back.isEmpty()) {

attributes.addFlashAttribute( attributeName: "message", attributeValue: "Please select a file to upload.”);

return "redirect:/;

}

String errorMessage = null;

try {

Path dir = Paths.get(uploadDirectory);

if (!Files.exists(dir)) {

Files.createDirectories(dir);

}

if (!face.isEmpty()) {

String filename = saveFile(face);

Map<Strin,String> res = ocrService.RecognizeIdcard( filePath: uploadDirectory + filename,side: "face");

faceImages.add(" /images/" + filename) ;

faceResults.add(res);

}

if (!back.isEmpty()) {

String filename = saveFile(back);

Map<String,String)> res = ocrService.RecognizeIdcard( filePath: uploadDirectory + filename,side: "back");

backImages.add("/images/" + filename);

backResults.add(res);

}

}catch (TeaException e) {

e.printStackTrace();

errorMessage = JsON.toJSONString(e.getData());

}catch (Exception e) {

e.printStackTrace();

errorMessage = e.getMessage();

}

if (StringUtils.isNotBlank(errorMessage)) {

attributes.addFlashAttribute( attributeName: "message", errorMessage);

}

return "redirect:/”;

}

相关文章
|
5月前
|
文字识别 异构计算 Python
关于Github中开源OCR项目的实验过程与思考
新手尝试Git clone Python OCR项目,遇到各种报错。测试了Paddle OCR、Tesseract OCR和EasyOCR。Paddle OCR因平台限制未能在Notebook部署,Tesseract OCR在Colab成功但无法复现。EasyOCR最终在阿里云天池和Colab部署成功,但天池GPU资源不足。建议使用魔搭社区的实例,阿里云提供免费OCR服务。寻求简单OCR项目推荐。附EasyOCR安装和使用代码。
212 2
|
3月前
|
文字识别 Java Spring
文本,文字识别,SpringBoot服务开发,SpringBoot如何提供上传服务,接口的设计,它做了将Base64重新转为图片,SpringBoot的应用实例,项目基础搭建
文本,文字识别,SpringBoot服务开发,SpringBoot如何提供上传服务,接口的设计,它做了将Base64重新转为图片,SpringBoot的应用实例,项目基础搭建
|
存储 文字识别 算法
基于飞桨实现项目2 中文场景文字识别
基于飞桨实现项目2 中文场景文字识别
247 0
基于飞桨实现项目2 中文场景文字识别
|
文字识别
【OCR学习笔记】9、OCR中文项目综合实践(CTPN+CRNN+CTC Loss原理讲解)(三)
【OCR学习笔记】9、OCR中文项目综合实践(CTPN+CRNN+CTC Loss原理讲解)(三)
221 0
|
机器学习/深度学习 文字识别 算法
【OCR学习笔记】9、OCR中文项目综合实践(CTPN+CRNN+CTC Loss原理讲解)(二)
【OCR学习笔记】9、OCR中文项目综合实践(CTPN+CRNN+CTC Loss原理讲解)(二)
329 0
|
机器学习/深度学习 文字识别 算法
【OCR学习笔记】9、OCR中文项目综合实践(CTPN+CRNN+CTC Loss原理讲解)(一)
【OCR学习笔记】9、OCR中文项目综合实践(CTPN+CRNN+CTC Loss原理讲解)(一)
407 0
|
SQL 机器学习/深度学习 人工智能
王炸:这个GitHub 20000+ Star的OCR项目迎来四大重磅升级
王炸:这个GitHub 20000+ Star的OCR项目迎来四大重磅升级
715 0
|
缓存 人工智能 JSON
文字识别项目讲解及使用说明 | 学习笔记
快速学习文字识别项目讲解及使用说明
文字识别项目讲解及使用说明 | 学习笔记
|
存储 文字识别 自然语言处理
【Spring Cloud】新闻头条微服务项目:文章内容安全审核(新增DFA+OCR过滤敏感词需求)
对文章审核新增加了需求----敏感词过滤,介绍了基于NFA的文本敏感词过滤及OCR提取图片文字的原理及实现,最后将文章审核代码做了合并。
523 0
【Spring Cloud】新闻头条微服务项目:文章内容安全审核(新增DFA+OCR过滤敏感词需求)
|
文字识别 Linux
Tesseract OCR开源项目-转载
原文:http://hi.baidu.com/shirdrn/blog/item/8c775488889b5a9da4c27284.html     Tesseract OCR开源项目2009年08月28日 星期五 18:00 最近,项目中需要使用基于图像识别验证码的技术,初步探索尝试了一下开源的Tesseract OCR项目。
1174 0

热门文章

最新文章

下一篇
无影云桌面