Update api.py
Browse files
api.py
CHANGED
|
@@ -4,7 +4,6 @@ import cv2
|
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
from fastapi import FastAPI, File, UploadFile, HTTPException, Request
|
| 7 |
-
import fitz
|
| 8 |
import detect_with_paddle
|
| 9 |
import base64
|
| 10 |
from fastapi.templating import Jinja2Templates
|
|
@@ -33,41 +32,8 @@ async def create_upload_file(file: UploadFile = File(...)):
|
|
| 33 |
def perform_ocr(file_content, file_name):
|
| 34 |
# Dosyayı base64'e kodla
|
| 35 |
base64_encoded = base64.b64encode(file_content).decode('utf-8')
|
|
|
|
| 36 |
|
| 37 |
-
if file_name.lower().endswith('.pdf'):
|
| 38 |
-
# PDF dosyasını işle
|
| 39 |
-
results = apply_ocr_on_pdf(base64_encoded)
|
| 40 |
-
elif file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 41 |
-
# Resmi doğrudan işle
|
| 42 |
-
results = apply_ocr_on_image(base64_encoded)
|
| 43 |
-
else:
|
| 44 |
-
raise HTTPException(status_code=400, detail="Unsupported file format")
|
| 45 |
-
|
| 46 |
-
return results
|
| 47 |
-
|
| 48 |
-
def apply_ocr_on_pdf(base64_encoded):
|
| 49 |
-
# Base64 kodlu veriyi geri çöz
|
| 50 |
-
pdf_bytes = base64.b64decode(base64_encoded)
|
| 51 |
-
pdf_document = fitz.open("pdf", pdf_bytes)
|
| 52 |
-
|
| 53 |
-
images = []
|
| 54 |
-
for page_number in range(pdf_document.page_count):
|
| 55 |
-
page = pdf_document[page_number]
|
| 56 |
-
pixmap = page.get_pixmap(dpi=400)
|
| 57 |
-
image = Image.frombytes("RGB", [pixmap.width, pixmap.height], pixmap.samples)
|
| 58 |
-
img_array = np.array(image)
|
| 59 |
-
#img_array_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
| 60 |
-
images.append(img_array)
|
| 61 |
-
pdf_document.close()
|
| 62 |
-
|
| 63 |
-
results = []
|
| 64 |
-
for image in images:
|
| 65 |
-
try:
|
| 66 |
-
result = detect_with_paddle.detect_paddle_OCR(image)
|
| 67 |
-
results.extend(result)
|
| 68 |
-
except:
|
| 69 |
-
print("Sayfada yazı bulunamadı!")
|
| 70 |
-
|
| 71 |
return results
|
| 72 |
|
| 73 |
def apply_ocr_on_image(base64_encoded):
|
|
|
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
from fastapi import FastAPI, File, UploadFile, HTTPException, Request
|
|
|
|
| 7 |
import detect_with_paddle
|
| 8 |
import base64
|
| 9 |
from fastapi.templating import Jinja2Templates
|
|
|
|
| 32 |
def perform_ocr(file_content, file_name):
|
| 33 |
# Dosyayı base64'e kodla
|
| 34 |
base64_encoded = base64.b64encode(file_content).decode('utf-8')
|
| 35 |
+
results = apply_ocr_on_image(base64_encoded)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return results
|
| 38 |
|
| 39 |
def apply_ocr_on_image(base64_encoded):
|