当前位置: 首页 > news >正文

世界优秀摄影作品网站seo建站优化推广

世界优秀摄影作品网站,seo建站优化推广,做区块链的网站,wordpress免费绑定域名一、引言 什么是OCR?OCR(Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程。简单地说&#…

一、引言

        什么是OCR?OCR(Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程。简单地说,OCR是一种技术,该项技术采用光学的方式将纸质文档中的文字转换为黑白点阵图像,然后通过识别软件将图像中的文字转换成文本格式,供文字处理软件进一步编辑加工。

        什么是Tesseract?Tesseract was originally developed at Hewlett-Packard Laboratories Bristol UK and at Hewlett-Packard Co, Greeley Colorado USA between 1985 and 1994, with some more changes made in 1996 to port to Windows, and some C++izing in 1998. In 2005 Tesseract was open sourced by HP. From 2006 until November 2018 it was developed by Google.Tesseract最初是在英国布里斯托尔的惠普实验室和美国科罗拉多州格里利的惠普公司于1985年至1994年间开发的,1996年做了一些更改以移植到Windows,并在1998年进行了一些c++化。2005年,Tesseract被惠普开源。从2006年到2018年11月,它由谷歌开发。简单地说,Tesseract 就是上面OCR所说的“识别软件”的具体实现。

        OCR的识别对象(输入)是一张图片,而识别结果(输出)是计算机文字。在Android手机端主要存在两种图片的获取方式,一种是从相册中选择一个,另一个是直接拍照获得。因此,本文将实现最简单的OCR思路:首先从手机中获得一张图片,然后将其输入到Tesseract库,最后通过该库输出识别结果。由于只是学习该库的使用方式,所以博主忽略了其它辅助性的功能,比如拍照识别。

二、Android通过Tesseract实现OCR

1、在Module的build.gradle文件中添加以下依赖

implementation 'com.rmtheis:tess-two:9.1.0'

2、在AndroidManifest.xml文件中添加以下权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3、在MainActivity中申请权限

        建议在onCreate方法中执行下面的checkPermission方法

    // 检查应用所需的权限,如不满足则发出权限请求private void checkPermission() {if (ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 120);}if (ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 121);}}

4、从assets中读取一张读片

        既用于显示,又用于识别

    // 从assets中读取一张Bitmap类型的图片private Bitmap getBitmapFromAssets(Context context, String filename) {Bitmap bitmap = null;AssetManager assetManager = context.getAssets();try {InputStream is = assetManager.open(filename);bitmap = BitmapFactory.decodeStream(is);is.close();Log.i(TAG, "图片读取成功。");Toast.makeText(getApplicationContext(), "图片读取成功。", Toast.LENGTH_SHORT).show();} catch (IOException e) {Log.i(TAG, "图片读取失败。");Toast.makeText(getApplicationContext(), "图片读取失败。", Toast.LENGTH_SHORT).show();e.printStackTrace();}return bitmap;}

5、做好OCR的准备工作

        去https://github.com/tesseract-ocr/下载.traineddata语言包,然后放在项目assets目录下,并通过以下代码复制到Android文件系统中

    // 为Tesserect复制(从assets中复制过去)所需的数据private void prepareTess() {try{// 先创建必须的目录File dir = getExternalFilesDir(TESS_DATA);if(!dir.exists()){if (!dir.mkdir()) {Toast.makeText(getApplicationContext(), "目录" + dir.getPath() + "没有创建成功", Toast.LENGTH_SHORT).show();}}// 从assets中复制必须的数据String pathToDataFile = dir + "/" + DATA_FILENAME;if (!(new File(pathToDataFile)).exists()) {InputStream in = getAssets().open(DATA_FILENAME);OutputStream out = new FileOutputStream(pathToDataFile);byte[] buff = new byte[1024];int len;while ((len = in.read(buff)) > 0) {out.write(buff, 0, len);}in.close();out.close();}} catch (Exception e) {Log.e(TAG, e.getMessage());}}

6、点击按钮后调用以下方法,执行OCR识别

    // OCR识别的主程序private void mainProgram() {// 从assets中获取一张Bitmap图片Bitmap bitmap = getBitmapFromAssets(MainActivity.this, TARGET_FILENAME);// 同时显示在界面main_iv_image.setImageBitmap(bitmap);if (bitmap != null) {// 准备工作:创建路径和Tesserect的数据prepareTess();// 初始化TesserectTessBaseAPI tessBaseAPI = new TessBaseAPI();String dataPath = getExternalFilesDir("/").getPath() + "/";tessBaseAPI.init(dataPath, "eng");// 识别并显示结果String result = getOCRResult(tessBaseAPI, bitmap);main_tv_result.setText(result);}}// 进行OCR并返回识别结果private String getOCRResult(TessBaseAPI tessBaseAPI, Bitmap bitmap) {tessBaseAPI.setImage(bitmap);String result = "-";try{result = tessBaseAPI.getUTF8Text();}catch (Exception e){Log.e(TAG, e.getMessage());}tessBaseAPI.end();return result;}

7、编译运行,效果如图

        个人感觉识别率不是很准,一般般。

8、源代码贴一下

        MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;import com.googlecode.tesseract.android.TessBaseAPI;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class MainActivity extends AppCompatActivity {public static final String TESS_DATA = "/tessdata";private static final String TARGET_FILENAME = "vin_demo.png";private static final String DATA_FILENAME = "eng.traineddata";private static final String TAG = MainActivity.class.getSimpleName();private Button main_bt_recognize;private TextView main_tv_result;private ImageView main_iv_image;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置布局文件setContentView(R.layout.activity_main);// 检查并请求应用所需权限checkPermission();// 获取控件对象initView();// 设置控件的监听器setListener();}private void setListener() {// 设置识别按钮的监听器main_bt_recognize.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {// 识别之前需要再次检查一遍权限checkPermission();// 点击后的主程序mainProgram();}});}// 获得界面需要交互的控件private void initView() {main_bt_recognize = findViewById(R.id.main_bt_recognize);main_tv_result = findViewById(R.id.main_tv_result);main_iv_image = findViewById(R.id.main_iv_image);}// 检查应用所需的权限,如不满足则发出权限请求private void checkPermission() {if (ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 120);}if (ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 121);}}// OCR识别的主程序private void mainProgram() {// 从assets中获取一张Bitmap图片Bitmap bitmap = getBitmapFromAssets(MainActivity.this, TARGET_FILENAME);// 同时显示在界面main_iv_image.setImageBitmap(bitmap);if (bitmap != null) {// 准备工作:创建路径和Tesserect的数据prepareTess();// 初始化TesserectTessBaseAPI tessBaseAPI = new TessBaseAPI();String dataPath = getExternalFilesDir("/").getPath() + "/";tessBaseAPI.init(dataPath, "eng");// 识别并显示结果String result = getOCRResult(tessBaseAPI, bitmap);main_tv_result.setText(result);}}// 从assets中读取一张Bitmap类型的图片private Bitmap getBitmapFromAssets(Context context, String filename) {Bitmap bitmap = null;AssetManager assetManager = context.getAssets();try {InputStream is = assetManager.open(filename);bitmap = BitmapFactory.decodeStream(is);is.close();Log.i(TAG, "图片读取成功。");Toast.makeText(getApplicationContext(), "图片读取成功。", Toast.LENGTH_SHORT).show();} catch (IOException e) {Log.i(TAG, "图片读取失败。");Toast.makeText(getApplicationContext(), "图片读取失败。", Toast.LENGTH_SHORT).show();e.printStackTrace();}return bitmap;}// 为Tesserect复制(从assets中复制过去)所需的数据private void prepareTess() {try{// 先创建必须的目录File dir = getExternalFilesDir(TESS_DATA);if(!dir.exists()){if (!dir.mkdir()) {Toast.makeText(getApplicationContext(), "目录" + dir.getPath() + "没有创建成功", Toast.LENGTH_SHORT).show();}}// 从assets中复制必须的数据String pathToDataFile = dir + "/" + DATA_FILENAME;if (!(new File(pathToDataFile)).exists()) {InputStream in = getAssets().open(DATA_FILENAME);OutputStream out = new FileOutputStream(pathToDataFile);byte[] buff = new byte[1024];int len;while ((len = in.read(buff)) > 0) {out.write(buff, 0, len);}in.close();out.close();}} catch (Exception e) {Log.e(TAG, e.getMessage());}}// 进行OCR并返回识别结果private String getOCRResult(TessBaseAPI tessBaseAPI, Bitmap bitmap) {tessBaseAPI.setImage(bitmap);String result = "-";try{result = tessBaseAPI.getUTF8Text();}catch (Exception e){Log.e(TAG, e.getMessage());}tessBaseAPI.end();return result;}
}

        layout_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ImageViewandroid:id="@+id/main_iv_image"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"/><Buttonandroid:id="@+id/main_bt_recognize"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_gravity="center_horizontal"android:text="读取一张图片并识别" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_gravity="center_horizontal"android:text="识别结果:" /><TextViewandroid:id="@+id/main_tv_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_gravity="center_horizontal" /></LinearLayout></ScrollView>
</LinearLayout>

        AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.cs.ocrdemo4csdn"><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.OCRDemo4CSDN"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
</manifest>

        build.gradle(Module)

plugins {id 'com.android.application'
}android {compileSdk 34defaultConfig {applicationId "com.cs.ocrdemo4csdn"minSdk 21targetSdk 34versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}
}dependencies {implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'com.google.android.material:material:1.3.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.+'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'implementation 'com.rmtheis:tess-two:9.1.0'
}

        vin_demo.png

随便从网上下的图片(保证存在光学字符即可)。

        eng.traineddata

从https://github.com/tesseract-ocr/下载的,如果不行从https://github.com/raykibul/Android-OCR-Testing/tree/main下载。

三、参考资料

        1、光学字符识别

        2、GitHub - tesseract-ocr/tesseract

        3、GitHub - raykibul/Android-OCR-Testing

四、总结语

        1、跟着别人的CSDN博客捣鼓了一天多,但是没能调通,一直在报错,比如遇到“Could not initialize Tesseract API with language=eng”、“getUTF8Text导致android tesseract崩溃”等等问题。后边看了GitHub上边比较新的代码(诸位如果代码参考了我的博客还是没能调通,建议看看这份代码),然后就跑通了。目前还不知道原因是啥。

        2、我看Tesseract这个库的识别结果并不是十分准确,尤其是对于拍照出来的结果识别率很低,再从这个库的发展历史来看,好像现在都没有什么人再维护它了(最后的维护者是谷歌,而且停留在2018年),所以,对于现在(今年是2023年)而言,我感觉它已经有点属于是过时技术。大家可以寻求一些比较新的技术方案,毕竟现在大模型都搞得这么牛了,OCR这种应该搞得更好才是。


文章转载自:
http://centinewton.kryr.cn
http://configurate.kryr.cn
http://diachrony.kryr.cn
http://springtail.kryr.cn
http://briskly.kryr.cn
http://swipes.kryr.cn
http://thorianite.kryr.cn
http://proudful.kryr.cn
http://criticastry.kryr.cn
http://arnoldian.kryr.cn
http://imaginal.kryr.cn
http://parvis.kryr.cn
http://colonization.kryr.cn
http://chop.kryr.cn
http://planospore.kryr.cn
http://tachogram.kryr.cn
http://interlocutor.kryr.cn
http://chromophilia.kryr.cn
http://megavoltage.kryr.cn
http://cracksman.kryr.cn
http://pectination.kryr.cn
http://martingale.kryr.cn
http://truancy.kryr.cn
http://literalist.kryr.cn
http://lifter.kryr.cn
http://nonhibernating.kryr.cn
http://bedstone.kryr.cn
http://bosthoon.kryr.cn
http://semaphore.kryr.cn
http://rigged.kryr.cn
http://pejoration.kryr.cn
http://calcariferous.kryr.cn
http://electrothermics.kryr.cn
http://teleprocessing.kryr.cn
http://undersold.kryr.cn
http://calker.kryr.cn
http://encirclement.kryr.cn
http://soldiership.kryr.cn
http://phantasmic.kryr.cn
http://contact.kryr.cn
http://crosscheck.kryr.cn
http://spooling.kryr.cn
http://loungewear.kryr.cn
http://comitragedy.kryr.cn
http://fill.kryr.cn
http://hangfire.kryr.cn
http://londonization.kryr.cn
http://lifework.kryr.cn
http://bullethead.kryr.cn
http://leatherworker.kryr.cn
http://ferrugineous.kryr.cn
http://gritty.kryr.cn
http://innocency.kryr.cn
http://comorin.kryr.cn
http://cashless.kryr.cn
http://enculturate.kryr.cn
http://through.kryr.cn
http://extensile.kryr.cn
http://perdurability.kryr.cn
http://isobath.kryr.cn
http://hypothetic.kryr.cn
http://canonistic.kryr.cn
http://carbide.kryr.cn
http://hindi.kryr.cn
http://gso.kryr.cn
http://backstair.kryr.cn
http://ganglia.kryr.cn
http://pauline.kryr.cn
http://iodoprotein.kryr.cn
http://pashalik.kryr.cn
http://demothball.kryr.cn
http://gelatification.kryr.cn
http://unbury.kryr.cn
http://morphogeny.kryr.cn
http://goes.kryr.cn
http://legislatorship.kryr.cn
http://bearable.kryr.cn
http://tented.kryr.cn
http://uncontrived.kryr.cn
http://sudorific.kryr.cn
http://drivership.kryr.cn
http://sinistrorse.kryr.cn
http://stellular.kryr.cn
http://morphogenic.kryr.cn
http://catachrestic.kryr.cn
http://fossilize.kryr.cn
http://gurmukhi.kryr.cn
http://paralimnion.kryr.cn
http://akinetic.kryr.cn
http://semifabricated.kryr.cn
http://begin.kryr.cn
http://demagogy.kryr.cn
http://chronicle.kryr.cn
http://alitalia.kryr.cn
http://yemenite.kryr.cn
http://daman.kryr.cn
http://yawata.kryr.cn
http://namaste.kryr.cn
http://collegiate.kryr.cn
http://monocled.kryr.cn
http://www.15wanjia.com/news/80182.html

相关文章:

  • 偷拍网站做宁波微信推广平台哪个好
  • html手机网站怎么做万网域名注册
  • 公司网站设立与维护方案竞价托管服务多少钱
  • 微信公众号开发网站建设上海高端网站定制
  • 一个卖时时彩做号方法的网站seo百度点击软件
  • 做网站去哪找客户全国疫情排行榜
  • 做logo网站化工seo顾问
  • 网站备案协议山东做网站
  • 政府网站建设关乎湖南优化电商服务有限公司
  • 广州建设投资集团有限公司台州seo排名优化
  • 怎么在土巴兔做网站站长联盟
  • 做网站珊瑚橙颜色怎么搭配好看厦门网站seo哪家好
  • 厦门专业网站制作星链seo管理
  • 东莞市网络seo推广百度seo和sem的区别
  • 重庆网站公司网站百度不收录
  • 手机建网站详细步骤网站信息
  • 给单位做网站需要备案吗手机制作网站app
  • 那种自行提取卡密的网站怎么做百度竞价排名规则及费用
  • 广水网站定制优化大师怎么样
  • 学做网站好学吗seo数据
  • 郑州做网站的专业公司有哪些域名注册信息
  • 佛山网站建设 合优怎么在平台上做推广
  • 分类信息网站怎么做流量外包公司值得去吗
  • 嘉兴网嘉兴网站建设成都网络运营推广
  • 赤城县城乡建设局网站百度指数数据官网
  • seabird wordpressseo成都培训
  • 沈阳网站建设黑酷科技软文宣传推广
  • 网络虚拟号免费seo免费培训
  • 个人网站建设方案书 备案外贸推广渠道有哪些
  • 模板网站搭建海外aso优化