openfree commited on
Commit
f82f1ef
·
verified ·
1 Parent(s): e8abc11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -3
app.py CHANGED
@@ -8,11 +8,19 @@ import re
8
  import uuid
9
  import pymupdf
10
 
 
 
 
 
11
  ###############################
12
  # 환경 설정
13
  ###############################
14
  os.system('pip uninstall -y magic-pdf')
15
  os.system('pip install git+https://github.com/opendatalab/MinerU.git@dev')
 
 
 
 
16
  os.system('wget https://github.com/opendatalab/MinerU/raw/dev/scripts/download_models_hf.py -O download_models_hf.py')
17
  os.system('python download_models_hf.py')
18
 
@@ -145,15 +153,67 @@ def replace_image_with_base64(markdown_text, image_dir_path):
145
  return f"![{relative_path}](data:image/jpeg;base64,{base64_image})"
146
  return re.sub(pattern, replace, markdown_text)
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  def to_pdf(file_path):
149
  """
150
- 이미지(JPG/PNG 등)를 PDF로 컨버팅.
 
151
  """
152
  with pymupdf.open(file_path) as f:
153
  if f.is_pdf:
154
  return file_path
155
  else:
156
- pdf_bytes = f.convert_to_pdf()
 
 
 
 
 
 
 
157
  unique_filename = f"{uuid.uuid4()}.pdf"
158
  tmp_file_path = os.path.join(os.path.dirname(file_path), unique_filename)
159
  with open(tmp_file_path, 'wb') as tmp_pdf_file:
@@ -384,7 +444,7 @@ latex_delimiters = [
384
  def to_markdown_ocr_flex(file_path, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language):
385
  """
386
  스니펫에서 사용:
387
- 업로드된 PDF/이미지를 변환
388
  (마크다운 렌더링 / 마크다운 텍스트 / 압축파일 / PDF미리보기) 반환
389
  """
390
  file_path = to_pdf(file_path)
 
8
  import uuid
9
  import pymupdf
10
 
11
+ # 이미지 전처리에 필요한 라이브러리
12
+ import cv2
13
+ import numpy as np
14
+
15
  ###############################
16
  # 환경 설정
17
  ###############################
18
  os.system('pip uninstall -y magic-pdf')
19
  os.system('pip install git+https://github.com/opendatalab/MinerU.git@dev')
20
+
21
+ # ★ OpenCV 설치 (headless 버전)
22
+ os.system('pip install opencv-python-headless')
23
+
24
  os.system('wget https://github.com/opendatalab/MinerU/raw/dev/scripts/download_models_hf.py -O download_models_hf.py')
25
  os.system('python download_models_hf.py')
26
 
 
153
  return f"![{relative_path}](data:image/jpeg;base64,{base64_image})"
154
  return re.sub(pattern, replace, markdown_text)
155
 
156
+ ###############################
157
+ # 이미지 전처리 함수 (Grayscale/Binarization + Deskew)
158
+ ###############################
159
+ def preprocess_image(image_path):
160
+ """
161
+ 1) Grayscale + Binarization(OTSU)
162
+ 2) Deskew(기울임 보정)
163
+ 전처리된 이미지를 임시 경로에 저장 후 해당 경로를 반환
164
+ """
165
+ img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
166
+ if img is None:
167
+ # 이미지 파일이 아닌 경우 혹은 로딩 실패 시 원본 경로 그대로 반환
168
+ return image_path
169
+
170
+ # (a) 이진화(binarization)
171
+ _, img_bin = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
172
+
173
+ # (b) 기울임 보정(deskew)
174
+ coords = np.column_stack(np.where(img_bin > 0))
175
+ angle = cv2.minAreaRect(coords)[-1]
176
+ # OpenCV는 회전 각도를 [-90, 0)로 반환할 때가 많으므로 보정
177
+ if angle < -45:
178
+ angle = -(90 + angle)
179
+ else:
180
+ angle = -angle
181
+
182
+ (h, w) = img_bin.shape[:2]
183
+ center = (w // 2, h // 2)
184
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
185
+ img_rotated = cv2.warpAffine(
186
+ img_bin,
187
+ M,
188
+ (w, h),
189
+ flags=cv2.INTER_CUBIC,
190
+ borderMode=cv2.BORDER_CONSTANT,
191
+ borderValue=255
192
+ )
193
+
194
+ # 임시 파일로 저장
195
+ preprocessed_path = image_path + "_preprocessed.png"
196
+ cv2.imwrite(preprocessed_path, img_rotated)
197
+
198
+ return preprocessed_path
199
+
200
  def to_pdf(file_path):
201
  """
202
+ 이미지(JPG/PNG 등)를 PDF로 컨버팅하되,
203
+ 이미지일 경우 전처리(Grayscale/Binarization + Deskew)를 먼저 적용
204
  """
205
  with pymupdf.open(file_path) as f:
206
  if f.is_pdf:
207
  return file_path
208
  else:
209
+ # 이미지 파일인 경우, 전처리 수행 후 PDF 생성
210
+ f.close()
211
+ preprocessed_path = preprocess_image(file_path)
212
+
213
+ # 전처리된 이미지를 다시 PyMuPDF로 열어서 PDF 변환
214
+ with pymupdf.open(preprocessed_path) as img_doc:
215
+ pdf_bytes = img_doc.convert_to_pdf()
216
+
217
  unique_filename = f"{uuid.uuid4()}.pdf"
218
  tmp_file_path = os.path.join(os.path.dirname(file_path), unique_filename)
219
  with open(tmp_file_path, 'wb') as tmp_pdf_file:
 
444
  def to_markdown_ocr_flex(file_path, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language):
445
  """
446
  스니펫에서 사용:
447
+ 업로드된 PDF/이미지 -> PDF 변환 -> 마크다운 변환
448
  (마크다운 렌더링 / 마크다운 텍스트 / 압축파일 / PDF미리보기) 반환
449
  """
450
  file_path = to_pdf(file_path)