구글 클라우드 비전 API는 이미지를 분석해서 라벨 감지, 문자 인식, 얼굴 감지 등을 해주는 기능으로
이걸 이용해서 텍스트를 추출해보려고한다.
https://cloud.google.com/vision/
우선 구글 클라우드 플랫폼에 가입하고 콘솔로 들어온다.
여기서 상단바에 보면 프로젝트를 선택하는 부분이 있는데 여기를 눌러 새 프로젝트를 만들어 선택한다.
이제 메뉴에서 API 및 서비스 - 사용자 인증 정보로 들어와서 새로운 서비스 계정 키를 발급받는다.
발급받으면 json 파일을 다운받을수 있는데 해당 파일을 프로젝트폴더 어딘가에 잘 보관해주자.
그리고 상단의 대시보드로 들어가서 Cloud Vision API를 찾아 들어가서 사용설정을 한다.
(https://console.cloud.google.com/apis/library/vision.googleapis.com)
그리고 다시 대시보드로 들어오면 API 리스트에 Cloud Vision API가 보일 것이다.
이러면 이제 기본적인 세팅은 끝이다.
-----------------------------------------------------------------------------------------
스크립트는 찾다가 한글로 된 예제(?)가 있길래 이걸 참고했다.
https://github.com/dang-gun/GoogleVisionAPITest0001
원래는 요청보내고 받는것까지 하나에 들어있었는데 스레드로 나누어봤다.
아까 발급받은 서비스키가 든 json파일의 위치를 잡아준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | private void VisionRequest(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(currentImagePath)) return; //구글 api 자격증명 GoogleCredential credential = null; //다운받은 '사용자 서비스 키'를 지정하여 자격증명을 만듭니다. using (var stream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\vision API test.json", FileMode.Open, FileAccess.Read)) { string[] scopes = { VisionService.Scope.CloudPlatform }; credential = GoogleCredential.FromStream(stream); credential = credential.CreateScoped(scopes); } //자격증명을 가지고 구글 비전 서비스를 생성합니다. var service = new VisionService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "google vision", GZipEnabled = true, }); service.HttpClient.Timeout = new TimeSpan(0, 1, 0); //이미지를 읽어 들입니다. //byte[] file = File.ReadAllBytes(@"ccc.png"); byte[] file = File.ReadAllBytes(currentImagePath); //분석 요청 생성 BatchAnnotateImagesRequest batchRequest = new BatchAnnotateImagesRequest(); batchRequest.Requests = new List<AnnotateImageRequest>(); batchRequest.Requests.Add(new AnnotateImageRequest() { //"TEXT_DETECTION"로 설정하면 이미지에 텍스트만 추출 합니다. Features = new List<Feature>() { new Feature() { Type = "TEXT_DETECTION", MaxResults = 1 }, }, ImageContext = new ImageContext() { LanguageHints = new List<string>() { "en", "ko" } }, Image = new Google.Apis.Vision.v1.Data.Image() { Content = Convert.ToBase64String(file) } }); Thread response_thread = new Thread(() => Response(service, batchRequest)); response_thread.Start(); } | cs |
아래는 응답받고 동작하는 스레드 부분이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | private void Response(VisionService service, BatchAnnotateImagesRequest batchRequest) { var annotate = service.Images.Annotate(batchRequest); //요청 결과 받기 BatchAnnotateImagesResponse batchAnnotateImagesResponse = annotate.Execute(); if (batchAnnotateImagesResponse.Responses.Any()) { AnnotateImageResponse annotateImageResponse = batchAnnotateImagesResponse.Responses[0]; if (annotateImageResponse.Error != null) {//에러 if (annotateImageResponse.Error.Message != null) { string result = annotateImageResponse.Error.Message; Dispatcher.BeginInvoke(new TextChangeDelegate(TextChange), result); } } else {//정상 처리 string result = annotateImageResponse.TextAnnotations[0].Description.Replace("\n", "\r\n"); Dispatcher.BeginInvoke(new TextChangeDelegate(TextChange), result); if (!isOpened) Dispatcher.BeginInvoke(new OpenTextBoxDelegate(OpenTextBox)); } } } | cs |
UI 스레드가 아니기때문에 Dispatcher.BeginInvoke()를 통해 UI를 수정해주었다.
1 2 3 4 5 | private delegate void TextChangeDelegate(string text); private void TextChange(string text) { textBox.Text = text; } | cs |
-----------------------------------------------------------------------------------------
실행결과
풀소스코드는 깃헙에 올려둠
'프로그래밍 공부 > WPF' 카테고리의 다른 글
스크린 캡쳐하기 (0) | 2018.11.26 |
---|---|
C# DES 암호화 (대칭키) (0) | 2018.10.13 |
C# RSA 암호화 (비대칭키 - 공개키 개인키) (1) | 2018.10.12 |
웹소켓 사용하기 - 2. 클라이언트 설정 (0) | 2018.06.05 |
웹소켓 사용하기 - 1.서버 설정 (0) | 2018.06.05 |