-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
286 lines (223 loc) · 9.43 KB
/
main.py
File metadata and controls
286 lines (223 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import JSONResponse
import cv2
import numpy as np
from typing import Tuple
from pathlib import Path
from skimage.metrics import structural_similarity as ssim
import math
import logging
from google.cloud import vision
import re
import requests
from bs4 import BeautifulSoup
from google.cloud import vision
from google.oauth2 import service_account
import re
app = FastAPI()
SERVICE_ACCOUNT_FILE = "./ancient-pipe-447417-i4-db9826a14abe.json"
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
client = vision.ImageAnnotatorClient(credentials=credentials)
SAVED_IMAG_PATH = Path("Comparative-image.png")
SIMILARITY_THRESHOLD= 93.0
logging.basicConfig(level=logging.INFO)
BASE_URL = "https://computer.hufs.ac.kr"
def calculate_histogram_similarity(img1: np.ndarray, img2: np.ndarray) -> float:
hist1 = cv2.calcHist([img1], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
hist2 = cv2.calcHist([img2], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256])
cv2.normalize(hist1, hist1)
cv2.normalize(hist2, hist2)
hist_similarity = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
return hist_similarity
def resize_min_size(img: np.ndarray, min_side: int = 7) -> np.ndarray:
h, w = img.shape[:2]
if h < min_side or w < min_side:
scale = max(min_side / h, min_side / w)
new_h = math.ceil(h * scale)
new_w = math.ceil(w * scale)
logging.info(f"이미지 크기를 리사이즈: ({h}, {w}) -> ({new_h}, {new_w})")
img = cv2.resize(img, (new_w, new_h))
return img
def calculate_ssim(img1: np.ndarray, img2: np.ndarray, win_size: int = 3) -> float:
img1 = resize_min_size(img1, min_side=win_size)
img2 = resize_min_size(img2, min_side=win_size)
if img1.shape != img2.shape:
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
logging.info(f"SSIM 계산: img1 크기 {img1.shape}, img2 크기 {img2.shape}, win_size={win_size}")
similarity_index, _ = ssim(img1, img2, full=True, multichannel=True, win_size=win_size)
return similarity_index
def combined_similarity(img1: np.ndarray, img2: np.ndarray, weight_hist=0.5, weight_ssim=0.5) -> float:
hist_sim = calculate_histogram_similarity(img1, img2)
ssim_sim = calculate_ssim(img1, img2, win_size=3)
combined = (weight_hist * hist_sim) + (weight_ssim * ssim_sim)
return combined
def calculate_similarity(img1: np.ndarray, img2: np.ndarray) -> float:
if img1.shape != img2.shape:
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
sim_score_0_to_1 = combined_similarity(img1, img2)
sim_score_0_to_100 = sim_score_0_to_1 * 100.0
return sim_score_0_to_100
def perform_ocr(image: np.ndarray) -> dict:
try:
logging.info("perform_ocr 함수 실행")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred_image = cv2.medianBlur(gray_image, 3)
binary_image = cv2.adaptiveThreshold(
blurred_image,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
11,
2
)
height, width = binary_image.shape
if height < 800 or width < 800:
resized_image = cv2.resize(binary_image, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
else:
resized_image = binary_image
success, encoded_image = cv2.imencode('.png', resized_image)
if not success:
raise HTTPException(status_code=500, detail="이미지 인코딩 실패")
content = encoded_image.tobytes()
vision_image = vision.Image(content=content)
image_context = vision.ImageContext(language_hints=["ko", "en"])
response = client.document_text_detection(image=vision_image, image_context=image_context)
if response.error.message:
logging.error(f"Vision API 오류: {response.error.message}")
raise HTTPException(status_code=500, detail=f"Vision API 오류: {response.error.message}")
texts = response.text_annotations
logging.debug(f"인식된 텍스트: {texts}")
if not texts:
return {"error": "텍스트를 인식할 수 없습니다."}
ocr_text = texts[0].description
logging.info(f"OCR 추출 텍스트: {ocr_text}")
fields = extract_fields(ocr_text)
logging.info(f"추출된 필드: {fields}")
return fields
except FileNotFoundError as fnf_error:
logging.error(f"키 파일 오류: {fnf_error}")
raise HTTPException(status_code=500, detail="Google Cloud Vision API 키 파일을 찾을 수 없습니다.")
except Exception as e:
logging.error(f"OCR 오류 발생: {e}")
raise HTTPException(status_code=500, detail="OCR 처리 중 오류가 발생했습니다.")
def extract_fields(ocr_text: str) -> dict:
name = None
student_id = None
major = None
lines = ocr_text.splitlines()
student_id_pattern = re.compile(r'\b\d{9}\b')
major_pattern = re.compile(r'.*전공.*')
previous_line = None
for line in lines:
line = line.strip()
if not line:
continue
if not student_id:
match = student_id_pattern.search(line)
if match:
student_id = match.group()
if previous_line:
name = previous_line
continue
if not major and major_pattern.match(line):
major = line
continue
previous_line = line
if name and student_id and major:
break
return {
'name': name,
'student_id': student_id,
'major': major
}
@app.post("/api/v1/compare-and-ocr")
async def compare_and_ocr(file: UploadFile = File(...)):
print("이미지 넘어옴")
try:
saved_image = cv2.imread(str(SAVED_IMAG_PATH))
if saved_image is None:
raise HTTPException(status_code=500, detail="저장된 이미지를 로드할 수 없습니다.")
uploaded_content = await file.read()
np_uploaded_image = np.frombuffer(uploaded_content, np.uint8)
uploaded_image = cv2.imdecode(np_uploaded_image, cv2.IMREAD_COLOR)
if uploaded_image is None:
raise HTTPException(status_code=400, detail="업로드된 이미지를 읽을 수 없습니다.")
similarity = calculate_similarity(saved_image, uploaded_image)
if similarity < SIMILARITY_THRESHOLD:
return JSONResponse(
{
"message": f"유사도가 기준({SIMILARITY_THRESHOLD}%) 미만입니다.",
"similarity": f"{similarity:.2f}%"
},
status_code=400
)
extracted_text = perform_ocr(uploaded_image)
return JSONResponse(
{
"message": "유사도 일치합니다.",
"similarity": f"{similarity:.2f}%",
"extracted_text": extracted_text
}
)
except HTTPException as http_ex:
raise http_ex
except Exception as e:
logging.error(f"오류 발생: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/v1/scrape/notice")
def scrape_hufs_notices():
logging.info("notice 함수 실행")
url = "https://computer.hufs.ac.kr/computer/10058/subview.do"
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
notice_table = soup.select_one("tbody")
rows = notice_table.select("tr")
notices = []
for row in rows:
num_tag = row.select_one(".td-num")
subject_tag = row.select_one(".td-subject a")
date_tag = row.select_one(".td-date")
if num_tag and subject_tag and date_tag:
span_new = subject_tag.select_one("span.new")
if span_new:
span_new.decompose()
notice_id = num_tag.get_text(strip=True)
title = subject_tag.get_text(strip=True)
date = date_tag.get_text(strip=True)
link = BASE_URL + subject_tag["href"]
notices.append({
"notice_id": notice_id,
"title": title,
"date": date,
"link": link
})
return {"notices": notices}
@app.get("/api/v1/scrape/employment")
def scrape_hufs_notices():
url = "https://computer.hufs.ac.kr/computer/10077/subview.do"
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
notice_table = soup.select_one("tbody")
rows = notice_table.select("tr")
notices = []
for row in rows:
num_tag = row.select_one(".td-num")
subject_tag = row.select_one(".td-subject a")
date_tag = row.select_one(".td-date")
if num_tag and subject_tag and date_tag:
span_new = subject_tag.select_one("span.new")
if span_new:
span_new.decompose()
notice_id = num_tag.get_text(strip=True)
title = subject_tag.get_text(strip=True)
date = date_tag.get_text(strip=True)
link = BASE_URL + subject_tag["href"]
notices.append({
"notice_id": notice_id,
"title": title,
"date": date,
"link": link
})
return {"notices": notices}