-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/WELLET/welletServer/ocr/controller/OcrController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "명함 업로드에 성공하였습니다."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/WELLET/welletServer/ocr/exception/OcrErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
src/main/java/WELLET/welletServer/ocr/exception/OcrException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
src/main/java/WELLET/welletServer/ocr/service/OcrService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
} | ||
} | ||
} | ||
} |