<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          真厲害!Java代碼實(shí)現(xiàn)圖片提取文字功能

          共 5319字,需瀏覽 11分鐘

           ·

          2021-10-24 21:50

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

          摘要

          ???????近日瀏覽網(wǎng)上一些圖片提取文字的網(wǎng)站,覺得甚是有趣,花費(fèi)半日也做了個(gè)在線圖片識(shí)別程序,完成了兩個(gè)技術(shù)方案的選擇,一是tesseract+python flask的方案實(shí)現(xiàn),二是tesseract+spring web的技術(shù)解決方案,并簡(jiǎn)作論述,與君共勉。

          一、tesseract-ocr介紹

          ?????? ocr含義是Optical Character Recognition,含義即視覺字符識(shí)別。而tesseract是該領(lǐng)域特別優(yōu)秀開源的作品。官方的tesseract定義:OCR engine - libtesseract and a command line program - tesseract.
          ???????即tesseract包括一個(gè)視覺字符識(shí)別引擎libtesseract和命令行程序tesseract。
          當(dāng)前最新穩(wěn)定版本是4.x.x基于LSTM,源碼可從找到tesseract的GitHub: tesseract.找到。

          ???????關(guān)于tesseract的工作模式如上圖所示。?假設(shè)現(xiàn)在有一個(gè)圖片輸入,整個(gè)執(zhí)行流程為:
          1.輸入(一張圖片)
          2.有用信息提取(比如一個(gè)圖片上只有一個(gè)字,那其他留白的是無用,這個(gè)字上每個(gè)色素是有效的并且相關(guān))
          3.找出文字/線條
          4.字符分類集
          5.輸入與分類集對(duì)比找出最接近的
          6.輸出識(shí)別結(jié)果

          二、安裝tesseract

          ???????第一步下載
          ???????下載合適的exe安裝文件:
          ???????網(wǎng)址:https://digi.bib.uni-mannheim.de/tesseract/,下載完成后后裝即可


          ???????第二步環(huán)境變量配置
          ???????在path變量中加入tesseract-ocr的安裝路徑

          ???????第三步安裝成功檢測(cè)
          ???????使用tesseract指令,顯示如下:

          ???????linux環(huán)境下載安裝與上述類似

          ???????下載leptonica 和 tesseract兩個(gè)包,解壓安裝,配置環(huán)境變量即可。網(wǎng)上很容易找到該安裝包,或者留言找筆者。

          三、使用命令行

          ???????1.tesseract + 圖片路徑 + 保存結(jié)果名 + -l 語言集
          示列: tesseract 1606150081.png 1606150081 -l chi_sim
          ???????2.tesseract + 圖片路徑 +stdout -l +語言集
          示列: tesseract D:\company\ruigushop\spring-2s\test.png stdout -l chi_sim

          有了上述之后就可以完成web圖片識(shí)別程序的開發(fā)啦,廢話不多說,直接上代碼。

          四、程序?qū)崿F(xiàn)(Python)

          程序設(shè)計(jì)思路:


          上傳圖片 -> 保存 ->對(duì)上傳的圖片執(zhí)行tesseract指令->獲取識(shí)別結(jié)果


          ???????只有二十多行代碼就實(shí)現(xiàn)了,so easy,以后網(wǎng)上看到圖片識(shí)別程序再也不會(huì)感覺神奇了吧!

          #?coding=utf-8
          from?flask?import?Flask,?request
          import?os
          import?datetime
          import?time

          app?=?Flask(__name__)


          def?get_time_stamp():
          ????times?=?datetime.datetime.now().strftime('%Y-%m-%d?%H:%M:%S')
          ????array?=?time.strptime(times,?"%Y-%m-%d?%H:%M:%S")
          ????time_stamp?=?int(time.mktime(array))
          ????return?time_stamp


          @app.route('/image/extract',?methods=['POST'])
          def?pure_rec():
          ????file?=?request.files.get('file')
          ????ts?=?str(get_time_stamp())
          ????up_path?=?os.path.join(ts?+?file.filename)
          ????file.save(up_path)
          ????cmd?=?"tesseract?"+up_path+"?"?+?ts?+?"?-l?chi_sim"
          ????print(cmd)
          ????os.system(cmd)
          ????with?open(ts+".txt",?'r+',?encoding="utf-8")?as?f:
          ????????result?=?f.read()
          ????????return?result


          if?__name__?==?'__main__':
          ????app.run(debug=True)

          五、程序?qū)崿F(xiàn)(Java)

          ???????不需要任何引入第三方j(luò)ar包,搭建一個(gè)簡(jiǎn)單的springboot web項(xiàng)目就可以了,沒有其他額外的依賴。

          Controller:

          package?com.lbh.web.controller;

          /*
          ?*?Copyright@[email protected]
          ?*?Author:liubinhao
          ?*?Date:2020/11/23
          ?*?++++?______?@author???????liubinhao???______?????????????______
          ?*?+++/?????/|?????????????????????????/?????/|???????????/?????/|
          ?*?+/_____/??|???????????????????????/_____/??|?????????/_____/??|
          ?*?|?????|???|??????????????????????|?????|???|????????|?????|???|
          ?*?|?????|???|??????????????????????|?????|???|________|?????|???|
          ?*?|?????|???|??????????????????????|?????|??/?????????|?????|???|
          ?*?|?????|???|??????????????????????|?????|/___________|?????|???|
          ?*?|?????|???|___________________???|?????|____________|?????|???|
          ?*?|?????|??/??????????????????/?|??|?????|???|????????|?????|???|
          ?*?|?????|/?_________________/??/???|?????|??/?????????|?????|??/
          ?*?|_________________________|/b????|_____|/???????????|_____|/
          ?*/
          import?org.springframework.web.bind.annotation.PostMapping;
          import?org.springframework.web.bind.annotation.RequestParam;
          import?org.springframework.web.bind.annotation.RestController;
          import?org.springframework.web.multipart.MultipartFile;

          import?java.io.BufferedReader;
          import?java.io.File;
          import?java.io.IOException;
          import?java.io.InputStreamReader;

          @RestController
          public?class?LiteralExtractController?{

          ????@PostMapping("/image/extract")
          ????public?String?reg(@RequestParam("file")MultipartFile?file)?throws?IOException?{
          ????????String?result?=?"";
          ????????String?filename?=?file.getOriginalFilename();
          ????????File?save?=?new?File(System.getProperty("user.dir")+"\\"+filename);
          ????????if?(!save.exists()){
          ????????????save.createNewFile();
          ????????}
          ????????file.transferTo(save);
          ????????String?cmd?=?String.format("tesseract?%s?stdout?-l?%s",System.getProperty("user.dir")+"\\"+filename,"chi_sim");
          ????????result?=?cmd(cmd);
          ????????return?result;
          ????}

          ????public?static?String?cmd(String?cmd)?{
          ????????BufferedReader?br?=?null;
          ????????try?{
          ????????????Process?p?=?Runtime.getRuntime().exec(cmd);
          ????????????br?=?new?BufferedReader(new?InputStreamReader(p.getInputStream()));
          ????????????String?line?=?null;
          ????????????StringBuilder?sb?=?new?StringBuilder();
          ????????????while?((line?=?br.readLine())?!=?null)?{
          ????????????????sb.append(line?+?"\n");
          ????????????}
          ????????????return?sb.toString();
          ????????}?catch?(Exception?e)?{
          ????????????e.printStackTrace();
          ????????}
          ????????finally
          ????????{
          ????????????if?(br?!=?null)
          ????????????{
          ????????????????try?{
          ????????????????????br.close();
          ????????????????}?catch?(Exception?e)?{
          ????????????????????e.printStackTrace();
          ????????????????}
          ????????????}
          ????????}
          ????????return?null;
          ????}
          }

          ???????哈哈哈,還有帥氣的程序logo。

          六、實(shí)驗(yàn)測(cè)試

          ???????很簡(jiǎn)單二十多行代碼就完成了,看看效果怎么樣吧。
          測(cè)試一圖片:

          測(cè)試一結(jié)果:

          ???????測(cè)試二圖片:

          ???????測(cè)試二結(jié)果:

          ?????? perfect,識(shí)別的很準(zhǔn)確,第二個(gè)測(cè)試全部是英文字符的時(shí)候我們采用了中文訓(xùn)練的數(shù)據(jù)集,雖然也很好的識(shí)別了,但是速度會(huì)慢很多。

          七、總結(jié)

          ???????圖片識(shí)別在當(dāng)今網(wǎng)絡(luò)技術(shù)領(lǐng)域是非常熱門的一塊,而這次完成的這個(gè)程序完全是依賴別人開源框架來完成了這個(gè)技術(shù)實(shí)現(xiàn),在應(yīng)用層面這是成功的,但是本質(zhì)上并沒有實(shí)際算法,技術(shù)核心上的東西,如果只關(guān)心應(yīng)用層開發(fā)上述解決了我們計(jì)算機(jī)在規(guī)則字符識(shí)別上的問題。

          ???????上述代碼中基本沒有難點(diǎn),直接復(fù)制即可使用。此外,tesseract作為一款優(yōu)秀的開源字符識(shí)別軟件,但它也不是萬能的,tesseract只能識(shí)別規(guī)則的字符,對(duì)于一些藝術(shù)字,抽象字它是無能為力的。


          ? 作者?|??興趣使然的程序猿

          來源 |??csdn.net/weixin_44671737/article/details/110000864


          加鋒哥微信:?java3459??
          圍觀鋒哥朋友圈,每天推送Java干貨!

          瀏覽 81
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一级免费黄色电影 | 波多野结衣成人在线视频 | 91av免费 | 国产高清视频在线播放 | 久操视频在线观看 |