Skip to content

Instantly share code, notes, and snippets.

@gouf
Created February 4, 2022 06:42
Show Gist options
  • Save gouf/5123ee72feb4b0f3a2a0d52c20f5ac57 to your computer and use it in GitHub Desktop.
Save gouf/5123ee72feb4b0f3a2a0d52c20f5ac57 to your computer and use it in GitHub Desktop.
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