etc/Weekend, I Learned

항해99 12주차 WIL(OCR)

yusung_ 2022. 12. 12. 21:13

OCR 트러블 슈팅

1. S3 to GCS issue

  • 기존 로직 순서: 사용자가 사진을 첨부하면 S3에 Upload ⇒ OCR후 텍스트 추출
// 1. OcrController - 사용자가 이미지파일을 첨부한다.
public class OcrController {
    private final S3Service s3Service;

    @PostMapping(value = "/ocr/cards")
    public ResponseDto<?> orcTest(@RequestPart(value = "cardImg", required = false) MultipartFile cardImg) throws IOException {

       return s3Service.upload(cardImg);
    }
}


// 2. S3Service - AWS S3에 이미지파일을 업로드한다.
public class S3Service {

    private final OcrService ocrService;

    public ResponseDto<?> upload(MultipartFile file) throws IOException {

        String fileName = CommonUtils.buildFileName(file.getOriginalFilename());// 파일이름

        long size = file.getSize(); // 파일 크기

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(file.getContentType());  // 파일타입
        objectMetadata.setContentLength(size); //파일크기
        InputStream inputStream = file.getInputStream();   // 실제 데이터를 넣어준다
        amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata)
                .withCannedAcl(CannedAccessControlList.PublicRead));  // S3 저장 및 권한설정

        String imageUrl = amazonS3.getUrl(bucket, fileName).toString();
        imageUrl = URLDecoder.decode(imageUrl, "UTF-8");
       
        OcrResponseDto ocrdata = ocrService.detectText(imageUrl); // => imageURL: S3에 저장된 이미지파일의 URL

        return ResponseDto.success(ocrdata);
    }
}

// 3. OcrService - AWS S3의 이미지를 OCR
public class OcrService {

   public OcrResponseDto detectTextGcs(String imageUrl) throws IOException {
        List<AnnotateImageRequest> requests = new ArrayList<>();

        ImageSource imgSource = ImageSource.newBuilder().setImageUri(imageUrl).build();
        Image img = Image.newBuilder().setSource(imgSource).build();
        Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
        requests.add(request);

        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests. After completing all of your requests, call
        // the "close" method on the client to safely clean up any remaining background resources.
        try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
            BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
            List<AnnotateImageResponse> responses = response.getResponsesList();

            ArrayList<Object> originList = new ArrayList<>();

            for (AnnotateImageResponse res : responses) {
                if (res.hasError()) {
                    System.out.format("Error: %s%n", res.getError().getMessage());
                    throw new IllegalArgumentException("실패");
                }
           }
      }

      ...
}

📌 에러 ) response에서 image에 대한 정보를 가져오지 못함 ⇒ ERROR

📌 에러 찾는 과정 )

  • for문 안의 **AnnotateImageResponse res**가 Error를 가지므로, 어느 부분에서 에러가 났는지 그 윗부분을 하나하나 찾아봐야 했음.
  • detectTextGcs() 매소드에서 setImageUri(filePath) 가 잘 작동하는지 디버깅 걸어봄

📌 원인 ) Google Cloud Vision API는 Google Services URL만 입·출력 대상으로 허용 ⇒ Google Cloud Storage를 제외한 URL에서 작동하지 않음

 

PDF/TIFF Document Text Detection

I am currently trying to use Google's cloud vision API for my project. The problem is that Google cloud vision API for document text detection accepts only Google Cloud Services URI as input and ou...

stackoverflow.com

 

📌 해결 방법 ) 2번으로 함

1) Amazon S3에서 Cloud Storage로 마이그레이션

https://www.notion.so/OCR-359b32dffa2a40aaa73382c53c4eb7e7#b36da50c552846e48befeb17f04b29f5

 

OCR 트러블 슈팅

1. S3 to GCS issue

www.notion.so

2) Google Cloud Storage를 사용하여 파일 업로드

: S3대신 Google Cloud Storage에 이미지파일을 업로드 하는 방법

  • 구글 저장소 관리자의 권한을 준 iam계정을 새로 만들고, json타입의 키를 새로 발급받는다.
  • GCS에 이미지파일을 업로드하는 로직을 만든다.
  • detectTextGcs() 메소드를 사용해 OCR기능을 구현한다.