mmdeploy/demo/java/Ocr.java
hanrui1sensetime 051e1577a3
Add java apis and demos (#563)
* add java classifier detector

* add segmentor

* fix lint

* add ImageRestorer java apis and demo

* remove useless count parameter for Segmentor and Restorer, add PoseDetector

* add RotatedDetection java api and demo

* add Ocr java demo and apis

* remove mmrotate ncnn java api and demo

* fix lint

* sync java api folder after rebase to master

* fix include

* remove record

* fix java apis dir path in cmake

* add java demo readme

* fix lint mdformat

* add test javaapi ci

* fix lint

* fix flake8

* fix test javaapi ci

* refactor readme.md

* fix install opencv for ci

* fix install opencv : add permission

* add all codebases and mmcv install

* add torch

* install mmdeploy

* fix image path

* fix picture path

* fix import ncnn

* fix import ncnn

* add submodule of pybind

* fix pybind submodule

* change download to git clone for submodule

* fix ncnn dir

* fix README error

* simplify the github ci

* fix ci

* fix yapf

* add JNI as required

* fix Capitalize

* fix Capitalize

* fix copyright

* ignore .class changed

* add OpenJDK installation docs

* install target of javaapi

* simplify ci

* add jar

* fix ci

* fix ci

* fix test java command

* debugging what failed

* debugging what failed

* debugging what failed

* add java version info

* install openjdk

* add java env var

* fix export

* fix export

* fix export

* fix export

* fix picture path

* fix picture path

* fix file name

* fix file name

* fix README

* remove java_api strategy

* fix python version

* format task name

* move args position

* extract common utils code

* show image class result

* add detector result

* segmentation result format

* add ImageRestorer result

* add PoseDetection java result format

* fix ci

* stage ocr

* add visualize

* move utils

* fix lint

* fix ocr bugs

* fix ci demo

* fix java classpath for ci

* fix popd

* fix ocr demo text garbled

* fix ci

* fix ci

* fix ci

* fix path of utils ci
2022-06-29 11:02:08 +08:00

61 lines
2.1 KiB
Java

import mmdeploy.TextDetector;
import mmdeploy.TextRecognizer;
import mmdeploy.PixelFormat;
import mmdeploy.DataType;
import mmdeploy.Mat;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
public class Ocr {
public static void main(String[] args) {
// Parse arguments
if (args.length != 4) {
System.out.println("usage:\njava TextDetection deviceName detModelPath recModelPath imagePath");
return;
}
String deviceName = args[0];
String detModelPath = args[1];
String recModelPath = args[2];
String imagePath = args[3];
// create text detector and recognizer
TextDetector text_detector = null;
TextRecognizer text_recognizer = null;
try {
text_detector = new TextDetector(detModelPath, deviceName, 0);
text_recognizer = new TextRecognizer(recModelPath, deviceName, 0);
// load image
Mat img = Utils.loadImage(imagePath);
// apply text detector
TextDetector.Result[] detResult = text_detector.apply(img);
int [] detResultCount = {detResult.length};
TextRecognizer.Result[] recResult = text_recognizer.applyBbox(img, detResult, detResultCount);
// print results
for (int i = 0; i < detResultCount[0]; ++i) {
System.out.printf("box[%d]: %s\n", i, new String(recResult[i].text));
for (int j = 0; j < 4; ++j) {
System.out.printf("x: %.2f, y: %.2f, ", detResult[i].bbox[j].x, detResult[i].bbox[j].y);
}
System.out.printf("\n");
}
} catch (Exception e) {
System.out.println("exception: " + e.getMessage());
} finally {
// release text detector and recognizer
if (text_recognizer != null) {
text_recognizer.release();
}
if (text_detector != null) {
text_detector.release();
}
}
}
}