Skip to content

Commit

Permalink
Fix: 충돌 해결 #86
Browse files Browse the repository at this point in the history
  • Loading branch information
hayeyoung committed Nov 4, 2024
1 parent ea795ae commit cb15e8a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
implementation platform('com.google.cloud:libraries-bom:26.1.4')
implementation 'com.google.cloud:google-cloud-bigquery'
implementation 'com.google.cloud:google-cloud-vision:3.3.0'
implementation 'io.github.cdimascio:java-dotenv:5.2.2'

compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package WELLET.welletServer.ocr.controller;

import WELLET.welletServer.ocr.dto.OcrSaveDto;
import WELLET.welletServer.ocr.service.OcrService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/ocr")
@RequiredArgsConstructor
@Tag(name = "ocr 기능")
public class OcrController {

private final OcrService ocrService;

@PostMapping
@Operation(summary = "OCR 사진 업로드")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "회원 저장에 성공하였습니다."),
@ApiResponse(responseCode = "400", description = "중복된 회원입니다.")
})
public String create(@Valid @ModelAttribute OcrSaveDto dto) throws IOException {
return ocrService.detectTextGcs(dto.file());
// return "명함 업로드에 성공하였습니다.";
}
}
6 changes: 6 additions & 0 deletions src/main/java/WELLET/welletServer/ocr/dto/OcrSaveDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package WELLET.welletServer.ocr.dto;

import org.springframework.web.multipart.MultipartFile;

public record OcrSaveDto(MultipartFile file) {
}
13 changes: 13 additions & 0 deletions src/main/java/WELLET/welletServer/ocr/exception/OcrErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package WELLET.welletServer.ocr.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum OcrErrorCode {
FILE_NOT_FOUND("파일을 찾을 수 없습니다."),
FAILED_OCR_PROCESSING("OCR 처리에 실패하였습니다.");

private final String message;
}
15 changes: 15 additions & 0 deletions src/main/java/WELLET/welletServer/ocr/exception/OcrException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package WELLET.welletServer.ocr.exception;
import lombok.Getter;

@Getter
public class OcrException extends RuntimeException{
private OcrErrorCode code;
private String message;

public OcrException(OcrErrorCode code) {
super();
this.code = code;
this.message = code.getMessage();
}

}
68 changes: 68 additions & 0 deletions src/main/java/WELLET/welletServer/ocr/service/OcrService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package WELLET.welletServer.ocr.service;

import WELLET.welletServer.files.FileUploader;
import WELLET.welletServer.ocr.exception.OcrErrorCode;
import WELLET.welletServer.ocr.exception.OcrException;
import com.google.cloud.vision.v1.*;
import io.github.cdimascio.dotenv.Dotenv;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class OcrService {

private final FileUploader fileUploader;

public String detectTextGcs(MultipartFile file) throws IOException {
if (file != null && !file.isEmpty()) {
String OCR_KEY = "ocr/";
String url = fileUploader.uploadFile(file, OCR_KEY);
detectTextGcs(url);
return "성공";
}
return "없음";
}

public static void detectTextGcs(String gcsPath) throws IOException {

// Dotenv dotenv = Dotenv.load();
// String googleCredentialsPath = dotenv.get("GOOGLE_APPLICATION_CREDENTIALS");
// System.setProperty("GOOGLE_APPLICATION_CREDENTIALS", googleCredentialsPath);

List<AnnotateImageRequest> requests = new ArrayList<>();

ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).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);

try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
log.error("Error: {}", res.getError().getMessage());
throw new OcrException(OcrErrorCode.FAILED_OCR_PROCESSING);
}

for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
System.out.format("Text: %s%n", annotation.getDescription());
// System.out.format("Position : %s%n", annotation.getBoundingPoly());

}
}
}
}
}

0 comments on commit cb15e8a

Please sign in to comment.