Source: blog.csdn.net/weixin_44671737/article/details/110000864
Abstract
Recently, I found it very interesting to browse some websites of image extraction on the Internet. I spent half a day to make an online image recognition program, and completed the selection of two technical solutions: one is the implementation of tesseract+ Python flask, the other is the technical solution of Tesseract + Spring Web. And a brief discussion, and jun mian.
Tesseract-ocr introduction
OCR means Optical Character Recognition, which means visual Character Recognition. Tesseract is a particularly good open source work in this area.
The official definition of Tesseract:
OCR engine – libtesseract and a command line program – tesseract.
Tesseract includes a visual character recognition engine called Libtesseract and a command line program called tesseract.
X is based on LSTM, source code can be found at Tesseract GitHub: Tesseract. To find it.
The tesseract mode is shown in the figure above. Suppose we now have an image input and the entire process is as follows:
- Enter (a picture)
- Useful information extraction (e.g., if there is only one word on a picture, the rest of the white space is useless, each color on the word is valid and relevant)
- Find the words/lines
- Character classification set
- The input is compared with the classification set to find the closest one
- Output identification result
Install tesseract
Step 1 Download
Download the appropriate exe installation file:
Digi.bib.uni-mannheim. de/ Tesseract /,…
Step 2 Configure environment variables
Add the tesseract-OCr installation path to the path variable
Step 3 Check the installation success
Using the Tesseract directive, it looks like this:
The Linux installation is similar to the above
Download leptonica and Tesseract, unpack them, install them, and configure environment variables. The installation package is easy to find online.
Use the command line
1. Tesseract + image path + save result name + -L language set
Tesseract 1606150081. PNG 1606150081-L CHI_sim
2. Tesseract + Image path +stdout -l + language set
Tesseract D:\company\ruigushop\spring-2s\test.png stdout -l chi_sim
With the above can be completed after the development of web image recognition procedures, nonsense not to say, directly on the code.
4. Program Implementation (Python)
Program design ideas:
Upload image -> Save -> Execute tesseract command on uploaded image -> get recognition result
Only more than 20 lines of code to achieve, so easy, after the Internet to see the image recognition program no longer feel magical!
# 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)
Copy the code
Five, program implementation (Java)
There is no need to import any third-party JAR packages and build a simple SpringBoot Web project with no additional dependencies.
The recommendation of a Spring foundation of the Boot will not introduced, the actual combat tutorial: www.javastack.cn/categories/…
Controller:
package com.lbh.web.controller; /* * Copyright@[email protected] * Author:liubinhao * Date:2020/11/23 * ++++ ______ @author liubinhao ______ ______ * + + + / / | | / / / | * + / / | | / _____ _____ _____ / * | | | | | | | | | | * | | | | | | _____ | | | * | | | | | / | | | * | | | | | / ___________ | | | * | | | ___________________ | | ____________ hold | | | * | | / / | | | | | | | * | | / _________________/ / | | / | | / * |_________________________|/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; }}Copy the code
Hahaha, and the cool app logo.
Six, experimental test
It’s pretty easy to do in about 20 lines of code, so let’s see what happens.
Test 1 Picture:
Test 1 Result:
Test 2 Picture:
Test 2 Results:
The recognition of PERFECT is very accurate. In the second test, when all English characters were used, we adopted the data set trained in Chinese. Although the recognition was very good, the speed was much slower.
Seven,
Image recognition technology in today’s network is very popular, this time to complete this program is completely dependent on others, a open source framework to complete the technical implementation at the application level it has been a success, but in essence is not the actual algorithm, on the core technology, if only concerned with the application layer development these solve the computer in our rule on the question of character recognition.
There is no difficulty in the above code, can be used directly copy. In addition, Tesseract as an excellent open source character recognition software, but it is not omnipotent, Tesseract can only recognize regular characters, for some artistic words, abstract words it is powerless.
Recent hot articles recommended:
1.1,000+ Java Interview Questions and Answers (2021)
2. Don’t use if/ else on full screen again, try strategy mode, it smells good!!
3. Oh, my gosh! What new syntax is xx ≠ null in Java?
4.Spring Boot 2.5 is a blockbuster release, and dark mode is exploding!
5. “Java Development Manual (Songshan version)” the latest release, quick download!
Feel good, don’t forget to click on + forward oh!