Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ dependencies {
//E-MAIL
implementation "org.springframework.boot:spring-boot-starter-mail"

// Add json-simple for JSON processing
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation 'org.json:json:20230618'


}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mycom.backenddaengplace.ocrtest;

public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.mycom.backenddaengplace.ocrtest.controller;

import com.mycom.backenddaengplace.ocrtest.service.OcrService;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/ocr")
public class OcrController {

private final OcrService ocrService;

@Autowired
public OcrController(OcrService ocrService) {
this.ocrService = ocrService;
}

@GetMapping("/upload")
public String showUploadPage() {
return "upload";
}

@PostMapping("/upload")
public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
try {
String filePath = ocrService.saveFile(file);
return ResponseEntity.ok(filePath);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + e.getMessage());
}
}

@PostMapping("/analyze")
public ResponseEntity<List<String>> analyzeImage(@RequestParam String filePath) {
try {
String ocrResult = ocrService.performOCR(filePath); // OCR μˆ˜ν–‰
List<String> inferTexts = extractInferTexts(ocrResult); // ν…μŠ€νŠΈλ§Œ μΆ”μΆœ
return ResponseEntity.ok(inferTexts); // κ²°κ³Ό λ°˜ν™˜
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}

private List<String> extractInferTexts(String jsonData) {
List<String> inferTexts = new ArrayList<>();

try {
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonData);
JSONArray images = (JSONArray) jsonObject.get("images");

for (Object imageObj : images) {
JSONObject image = (JSONObject) imageObj;
JSONArray fields = (JSONArray) image.get("fields");

for (Object fieldObj : fields) {
JSONObject field = (JSONObject) fieldObj;
String inferText = (String) field.get("inferText"); // ν…μŠ€νŠΈλ§Œ μΆ”μΆœ
inferTexts.add(inferText);
}
}
} catch (Exception e) {
e.printStackTrace();
}

return inferTexts;
}

@PostMapping("/format")
public ResponseEntity<String> formatInferText(@RequestBody String jsonData) {
try {
String result = ocrService.removeVertices(jsonData);
return ResponseEntity.ok(result);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mycom.backenddaengplace.ocrtest.controller;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class OcrResultProcessor {

public static String extractInferText(String jsonData) {
try {
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonData);
JSONArray images = (JSONArray) jsonObject.get("images");
JSONArray inferTextArray = new JSONArray();

for (Object imageObj : images) {
JSONObject image = (JSONObject) imageObj;
JSONArray fields = (JSONArray) image.get("fields");

for (Object fieldObj : fields) {
JSONObject field = (JSONObject) fieldObj;
String inferText = (String) field.get("inferText");
inferTextArray.add(inferText);
}
}

// κ²°κ³Όλ₯Ό JSON λ°°μ—΄λ‘œ λ°˜ν™˜
return inferTextArray.toJSONString();
} catch (Exception e) {
e.printStackTrace();
return "[]"; // 였λ₯˜ λ°œμƒ μ‹œ 빈 λ°°μ—΄ λ°˜ν™˜
}
}

public static void main(String[] args) {
String jsonData = "YOUR_JSON_DATA_HERE"; // JSON 데이터 λ¬Έμžμ—΄ μž…λ ₯
String result = extractInferText(jsonData);
System.out.println(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mycom.backenddaengplace.ocrtest.controller;

import com.mycom.backenddaengplace.ocrtest.service.OcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/receipt")
public class ReceiptOCRController {
private final OcrService ocrService;

@Autowired
public ReceiptOCRController(OcrService ocrService) {
this.ocrService = ocrService;
}
@PostMapping("/process")
public ResponseEntity<String> processInferText(@RequestBody String jsonData) {
try {
String result = ocrService.removeVertices(jsonData);
return ResponseEntity.ok(result);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mycom.backenddaengplace.ocrtest.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class OcrResult {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String filePath;

private String concatenatedText;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mycom.backenddaengplace.ocrtest.repository;

import com.mycom.backenddaengplace.ocrtest.entity.OcrResult;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OcrResultRepository extends JpaRepository<OcrResult, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.mycom.backenddaengplace.ocrtest.service;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class OCRProcessor {
public List<String> extractInferTexts(String jsonData) {
List<String> inferTexts = new ArrayList<>();

try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray images = jsonObject.getJSONArray("images");

for (int i = 0; i < images.length(); i++) {
JSONObject image = images.getJSONObject(i);
JSONArray fields = image.getJSONArray("fields");

for (int j = 0; j < fields.length(); j++) {
JSONObject field = fields.getJSONObject(j);

// `vertices`λ₯Ό λ¬΄μ‹œν•˜κ³  `inferText`만 μΆ”μΆœ
if (field.has("inferText")) {
inferTexts.add(field.getString("inferText"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

return inferTexts;
}

public String preprocessJson(String jsonData) {
try {
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray images = jsonObject.getJSONArray("images");

for (int i = 0; i < images.length(); i++) {
JSONObject image = images.getJSONObject(i);
JSONArray fields = image.getJSONArray("fields");

for (int j = 0; j < fields.length(); j++) {
JSONObject field = fields.getJSONObject(j);
// boundingPoly ν•„λ“œ 제거
field.remove("boundingPoly");
}
}

// μ „μ²˜λ¦¬λœ JSON 데이터λ₯Ό λ¬Έμžμ—΄λ‘œ λ°˜ν™˜
return jsonObject.toString();
} catch (Exception e) {
e.printStackTrace();
return "{}"; // μ‹€νŒ¨ μ‹œ 빈 JSON λ°˜ν™˜
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mycom.backenddaengplace.ocrtest.service;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public interface OcrService {
String saveFile(MultipartFile file) throws IOException;

String performOCR(String filePath) throws IOException;

String processJsonData(String jsonData);

String removeVertices(String jsonData);
}
Loading
Loading