・ Gradle project ・ Registered GCP account (For account registration, API usage, and authentication key issuance, refer to here)
The fastest way to add a gradle library is to look it up in the Maven Repository (https://mvnrepository.com/).
compile group: 'com.google.cloud', name: 'google-cloud-vision', version: '1.100.9'
Authentication information is required when using the GCP API. Create credentials from the GCP console and download the JSON file
Put the downloaded JSON file in the project, execution configuration
→ environment
→ add (new)
Variables are GOOGLE_APPLICATION_CREDENTIALS
The value is the absolute path of the Json file.json
sample.java
void main() {
try {
//Specify the read image
String inputImgPath = "Absolute path.png ";
//Extract the analysis result as a text file
//result.Make txt
PrintStream outputResultPath = new PrintStream(new FileOutputStream("Absolute path/result.txt"), true);
detectText(inputImgPath, outputResultPath);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Quoted from sample code
public static void detectText(String filePath, PrintStream out) throws Exception, IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
out.printf("Error: %s\n", res.getError().getMessage());
return;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
//Output text
out.printf("Text: %s\n", annotation.getDescription());
//Output coordinates (delete the sentence below if you don't need it)
out.printf("Position : %s\n", annotation.getBoundingPoly());
}
}
}
}
}
I tried to extract the character string in the image below.
This is the extraction result.
Text:All thoughts
"Felt pen]
Paid version:Second-level writing(6,700 characters or more)Recording
Free version:Educational writing(1,006 characters) +a..?4 green
Created based on the handwritten female characters of the first grade of elementary school
Original Kanji Morimori Japanese font.
The recorded characters were not read correctly. However, it is very accurate to read this font.
If you put out the coordinates of the characters, it will come out like this.
Position : vertices {
x: 33
y: 53
}
vertices {
x: 451
y: 53
}
vertices {
x: 451
y: 310
}
vertices {
x: 33
y: 310
}
Text:all
Position : vertices {
x: 76
y: 58
}
vertices {
x: 125
y: 58
}
vertices {
x: 125
y: 101
}
vertices {
x: 76
y: 101
}
Omitted below
This time I wrote it for gradle users. The source code is difficult to understand, but try copying and using it first.
Recommended Posts