在安卓中用fastdeploy 部署推理,如何只做文本识别呢? #14762
Unanswered
huangguifeng
asked this question in
Q&A
Replies: 1 comment 1 reply
-
当前 FastDeploy SDK 的 解决方案:
代码实现:修改 1. 修改类成员变量在 import com.baidu.paddle.fastdeploy.vision.ocr.DBDetector;
import com.baidu.paddle.fastdeploy.vision.OCRResult;
public class PPOCRHelper {
private DBDetector detector;
private String Tag = "PPOCRHelper";
private boolean initialized;
public PPOCRHelper(Context context) {
try {
// 确保目标目录存在
new File(context.getFilesDir().getPath() + "/access/en_PP-OCRv3_det_infer").mkdirs();
// 文本检测模型文件路径
String detModelFile = context.getFilesDir().getPath() + "/access/en_PP-OCRv3_det_infer/inference.pdmodel";
String detParamsFile = context.getFilesDir().getPath() + "/access/en_PP-OCRv3_det_infer/inference.pdiparams";
// 拷贝文件到目标路径
copyAssetToFile(context, "en_PP-OCRv3_det_infer/inference.pdmodel", detModelFile);
copyAssetToFile(context, "en_PP-OCRv3_det_infer/inference.pdiparams", detParamsFile);
// 初始化运行选项
RuntimeOption detOption = new RuntimeOption();
detOption.enableLiteFp16();
// 初始化 DBDetector
detector = new DBDetector(detModelFile, detParamsFile, detOption);
initialized = detector.initialized();
} catch (Exception e) {
Log.e(Tag, "文本检测模型加载失败");
}
}
// 进行文本检测
public OCRResult detectText(Bitmap img) {
if (!initialized) {
Log.e(Tag, "模型未正确初始化");
return null;
}
return detector.predict(img);
}
private void copyAssetToFile(Context context, String assetFileName, String outputPath) {
try (InputStream in = context.getAssets().open(assetFileName);
OutputStream out = new FileOutputStream(outputPath)) {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
Log.d("TAG", "模型文件拷贝成功: " + outputPath);
} catch (IOException e) {
Log.e("TAG", "拷贝模型文件失败: " + e.getMessage());
}
}
} 2. 调用
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
package com.example.mydemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import com.baidu.paddle.fastdeploy.RuntimeOption;
import com.baidu.paddle.fastdeploy.pipeline.PPOCRv3;
import com.baidu.paddle.fastdeploy.vision.OCRResult;
import com.baidu.paddle.fastdeploy.vision.ocr.DBDetector;
import com.baidu.paddle.fastdeploy.vision.ocr.Recognizer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class PPOCRHelper {
} sdk 好像并不支持单独使用rec 模型,由于特定任务通用的文本检测满足要求,然后自己训练了文本检测模型,想单独使用文本检测。
Beta Was this translation helpful? Give feedback.
All reactions