Skip to content

Commit

Permalink
[MERGE/#95] 네이버 OCR 에러 처리 구현
Browse files Browse the repository at this point in the history
[FEAT] #95 - 네이버 OCR 에러 처리 구현
  • Loading branch information
seokbeom00 authored Jul 15, 2024
2 parents d7f6ff1 + 93d5f9f commit c7c8534
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -18,6 +19,8 @@
import org.json.JSONObject;
import org.sopt.seonyakServer.global.common.external.naver.dto.OcrBusinessResponse;
import org.sopt.seonyakServer.global.common.external.naver.dto.OcrUnivResponse;
import org.sopt.seonyakServer.global.exception.enums.ErrorType;
import org.sopt.seonyakServer.global.exception.model.CustomException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -32,20 +35,28 @@ public OcrUnivResponse ocrUniv(MultipartFile file) throws IOException {
String apiUrl = ocrConfig.getUnivUrl();
String apiKey = ocrConfig.getUnivUrlKey();

// 대학교 OCR 응답 문자열로 받아옴
String response = requestNaverOcr(apiUrl, apiKey, file);
String response = getOcrResponse(apiUrl, apiKey, file);

// 네이버 OCR 실패 응답 처리
String responseResult = extractInferResult(response);
if (responseResult.equals("FAILURE")) {
throw new CustomException(ErrorType.NOT_VALID_OCR_IMAGE);
}
return OcrUnivResponse.of(extractUnivText(response));
}

// 명함 OCR
public OcrBusinessResponse ocrBusiness(MultipartFile file) throws IOException {

// OCR 설정파일로부터 URL, Secret Key 가져옴
String apiUrl = ocrConfig.getBusinessUrl();
String apiKey = ocrConfig.getBusinessKey();

String response = getOcrResponse(apiUrl, apiKey, file);

//회사명, 휴대전화번호 JSON 응답에서 파싱
String company = extractTextByKey(requestNaverOcr(apiUrl, apiKey, file), "company");
String phoneNumber = extractTextByKey(requestNaverOcr(apiUrl, apiKey, file), "mobile");
String company = extractTextByKey(response, "company");
String phoneNumber = extractTextByKey(response, "mobile");
String cleanedNumber = phoneNumber.replaceAll("[^\\d]", "");
String lastEightNumber =
cleanedNumber.length() > 8 ? cleanedNumber.substring(cleanedNumber.length() - 8) : cleanedNumber;
Expand Down Expand Up @@ -138,12 +149,10 @@ private static void writeMultiPart(OutputStream out, String jsonMessage, Multipa
}

// OCR 응답 JSON에서 "대학교"가 포함된 inferText만 추출하는 함수
private String extractUnivText(String jsonResponse) {
private List<String> extractUnivText(String jsonResponse) {
JSONObject responseJson = new JSONObject(jsonResponse);
JSONArray images = responseJson.getJSONArray("images");

Pattern pattern = Pattern.compile(".*?대학교");

return IntStream.range(0, images.length())
.mapToObj(images::getJSONObject)
.flatMap(image -> {
Expand All @@ -160,7 +169,7 @@ private String extractUnivText(String jsonResponse) {
}
return inferText;
})
.collect(Collectors.joining(","));
.collect(Collectors.toList());
}

// 명함 OCR 응답에서 keyword에 해당하는 필드값을 추출하는 함수
Expand All @@ -183,4 +192,26 @@ private String extractTextByKey(String jsonResponse, String key) {
.map(textObject -> textObject.getString("text"))
.collect(Collectors.joining(", "));
}

private String extractInferResult(String jsonResponse) {
JSONObject responseJson = new JSONObject(jsonResponse);
JSONArray images = responseJson.getJSONArray("images");

return IntStream.range(0, images.length())
.mapToObj(images::getJSONObject)
.map(image -> image.getString("inferResult"))
.collect(Collectors.joining(","));
}

private String getOcrResponse(String apiUrl, String apiKey, MultipartFile file) throws IOException {

String response = requestNaverOcr(apiUrl, apiKey, file);
// 네이버 OCR 실패 응답 처리
String responseResult = extractInferResult(response);
if (responseResult.equals("FAILURE")) {
throw new CustomException(ErrorType.NOT_VALID_OCR_IMAGE);
}

return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.sopt.seonyakServer.global.common.external.naver.dto;

import java.util.List;

public record OcrUnivResponse(
String univName
List<String> univName
) {
public static OcrUnivResponse of(String univName) {
public static OcrUnivResponse of(List<String> univName) {
return new OcrUnivResponse(univName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum ErrorType {
UNIV_CERT_REQUEST_ERROR(HttpStatus.BAD_REQUEST, "40018", "이미 인증이 완료된 이메일입니다."),
SAME_MEMBER_APPOINTMENT_ERROR(HttpStatus.BAD_REQUEST, "40019", "자기 자신에게는 약속을 신청할 수 없습니다."),
NOT_MEMBERS_APPOINTMENT_ERROR(HttpStatus.BAD_REQUEST, "40020", "해당 회원의 약속이 아닙니다."),
NOT_VALID_OCR_IMAGE(HttpStatus.BAD_REQUEST, "40021", "이미지 인식에 실패했습니다."),

// S3 관련 오류
IMAGE_EXTENSION_ERROR(HttpStatus.BAD_REQUEST, "40051", "이미지 확장자는 jpg, png, webp만 가능합니다."),
Expand Down

0 comments on commit c7c8534

Please sign in to comment.