Skip to content

Instantly share code, notes, and snippets.

@gouf
Created Feb 4, 2022
Embed
What would you like to do?
Google Cloud Vision API を利用して、画像中に指定文字列が含まれているか検出
namespace MyProject;
using Google.Cloud.Vision.V1;
public class GoogleVisionApi {
private ImageAnnotatorClient _client;
public GoogleVisionApi() {
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\Users\user\VisionApiDemo\key.json");
_client = ImageAnnotatorClient.Create();
}
// 画像にテキスト「ゲーム終了」が含まれているかチェック
public bool detectOperationEnd() {
String imagePath = @"C:\Users\user\captured_images\game_end.jpg";
String searchText = "ゲーム終了";
return detectImageText(searchText, imagePath);
}
// 渡された画像に指定テキストが含まれているかチェック
private bool detectImageText(String searchText, String imagePath) {
Image image = Image.FromFile(imagePath);
IReadOnlyList<EntityAnnotation> response = _client.DetectText(image);
int detectedCount = response.Count(annotation => annotation.Description.Contains(searchText));
return detectedCount > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment