前言
大家有没有遇到过这种状况,在浏览微博或者公众号时看到一段乏味的文字,于是截图发到朋友圈想和好友分享。然而在公布图片时,软件会对图片强制进行压缩,导致图片分辨率降落,文字变得含糊难以浏览。那么有没有什么方法能够解决这种状况呢?当然有啦。华为HMS ML Kit提供了文字超分技术,能够冲破图像中文本分辨率的物理限度,对蕴含文字内容的图像进行9倍放大(长宽各放大3倍),同时显著加强图像中文字的清晰度和可辨识度,轻松解决图片中文字分辨率低的问题。
利用场景
文字超分技术在生活中有很多的利用场景,比方刚刚提到朋友圈公布的截图被压缩时,文字超分技术能够把截图还原到高清晰度。
或者是在文档翻拍时,因为距离远、未聚焦等起因,导致拍摄的文字不清晰。文字超分技术能够进步翻拍文档的清晰度和可辨识度,让文档中的字变得清晰。
怎么样,是不是很实用?上面给大家简略介绍如何集成HMS ML Kit文字超分服务。
开发实战
1. 配置Maven仓地址
1.1 关上Android Studio我的项目级“build.gradle”文件
1.2 增加HUAWEI agcp插件以及Maven代码库。
在allprojects ->repositories外面配置HMS Core SDK的Maven仓地址。
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
在buildscript->repositories外面配置HMS Core SDK的Maven仓地址。
buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
2. 集成文字图像超分辨率服务SDK
2.1 Full SDK形式集成(举荐应用)
dependencies{
// 引入根底SDK
Implementation 'com.huawei.hms:ml-computer-vision-textimagesuperresolution:2.0.3.300'
// 引入文字图像超分辨率模型包
implementation 'com.huawei.hms:ml-computer-vision-textimagesuperresolution-model:2.0.3.300'
}
2.2 文件头增加配置
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
2.3 更新机器学习模型
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value= "tisr"/>
3. 代码开发
3.1 创立文字图像超分辨率分析器
MLTextImageSuperResolutionAnalyzer analyzer = MLTextImageSuperResolutionAnalyzerFactory.getInstance().getTextImageSuperResolutionAnalyzer();
3.2 通过android.graphics.Bitmap结构MLFrame(留神此处的bitmap类型必须为ARGB8888,请留神做必要的转换)
// 通过bitmap创立MLFrame,bitmap为输出的图片数据。
MLFrame frame = new MLFrame.Creator().setBitmap(bitmap).create();
3.3 对蕴含文字的图片进行超分辨率解决
Task<MLTextImageSuperResolutionResult> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<MLTextImageSuperResolutionResult>() {
public void onSuccess(MLTextImageSuperResolutionResult result) {
// 超分胜利的解决逻辑。
}})
.addOnFailureListener(new OnFailureListener() {
public void onFailure(Exception e) {
// 超分失败的解决逻辑。
if (e instanceof MLException) {
MLException mlException = (MLException)e;
// 获取错误码,开发者能够对错误码进行解决,依据错误码进行差异化的页面提醒。
int errorCode = mlException.getErrCode();
// 获取报错信息,开发者能够联合错误码,疾速定位问题。
String errorMessage = mlException.getMessage();
} else {
// 其余异样。
}
});
3.4 超分实现,进行分析器,开释检测资源
if (analyzer != null) {
analyzer.stop();
}
Github地址
查看Demo源码:https://github.com/HMS-Core/h…
更具体的开发指南参考华为开发者联盟官网
https://developer.huawei.com/consumer/cn/hms/huawei-mlkit
欲了解更多详情,请参阅:
华为开发者联盟官网:https://developer.huawei.com/consumer/cn/hms
获取开发领导文档:https://developer.huawei.com/consumer/cn/doc/development
参加开发者探讨请到Reddit社区:https://www.reddit.com/r/HMSCore/
下载demo和示例代码请到Github:https://github.com/HMS-Core
解决集成问题请到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest
原文链接:
https://developer.huawei.com/consumer/cn/forum/topicview?tid=0203349782674210504&fid=18
作者:留下落叶
发表回复