Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,47 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import detect_with_paddle
|
| 4 |
-
import detect
|
| 5 |
import cv2
|
| 6 |
-
import
|
| 7 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import fitz
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
with st.container():
|
| 14 |
-
st.title("Epoch Technology OCR")
|
| 15 |
-
col1, col2 = st.columns(2, gap="medium")
|
| 16 |
-
|
| 17 |
-
with col1:
|
| 18 |
-
uploaded_file = st.file_uploader("Upload a file", type=["pdf", "jpg", "jpeg", "png"])
|
| 19 |
-
if uploaded_file is not None:
|
| 20 |
-
# Görüntüyü yükle
|
| 21 |
-
file_extension = get_file_extension(uploaded_file.name)
|
| 22 |
-
if file_extension == "pdf":
|
| 23 |
-
pdf_to_images = pdf_to_image(uploaded_file)
|
| 24 |
-
else:
|
| 25 |
-
image = Image.open(uploaded_file)
|
| 26 |
-
st.image(image, caption="Uploaded File", use_column_width=True)
|
| 27 |
-
|
| 28 |
-
with col2:
|
| 29 |
-
if uploaded_file is not None:
|
| 30 |
-
|
| 31 |
-
if st.button(key="read",label="Read Document"):
|
| 32 |
-
if file_extension == "pdf":
|
| 33 |
-
extracted_text = extract_text_on_pdf(pdf_to_images)
|
| 34 |
-
st.write(extracted_text)
|
| 35 |
-
else:
|
| 36 |
-
extracted_text = extract_text_on_image(image)
|
| 37 |
-
st.write(extracted_text)
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
return results
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
images = []
|
| 55 |
for page_number in range(pdf_document.page_count):
|
| 56 |
page = pdf_document[page_number]
|
|
@@ -62,20 +53,17 @@ def pdf_to_image(pdf_file):
|
|
| 62 |
images.append(image)
|
| 63 |
pdf_document.close()
|
| 64 |
|
|
|
|
| 65 |
for image in images:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
return
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
# Dosya adındaki en son kısmı alarak dosya uzantısını bulma
|
| 74 |
-
parts = file_name.split(".")
|
| 75 |
-
if len(parts) > 1:
|
| 76 |
-
return parts[-1].lower()
|
| 77 |
-
else:
|
| 78 |
-
return None
|
| 79 |
-
|
| 80 |
-
if __name__ == "__main__":
|
| 81 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import os
|
| 3 |
import numpy as np
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 7 |
+
import easyocr
|
| 8 |
import fitz
|
| 9 |
+
import preproccess
|
| 10 |
+
import detect_with_paddle
|
| 11 |
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
current_directory = os.getcwd()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
@app.get('/')
|
| 16 |
+
async def index():
|
| 17 |
+
return "Welcome PaddleOCR By SENEL"
|
| 18 |
|
| 19 |
+
@app.post("/uploadfile/")
|
| 20 |
+
async def create_upload_file(file: UploadFile = File(...)):
|
| 21 |
+
try:
|
| 22 |
+
# Dosyayı bellekte geçici bir nesne olarak tut
|
| 23 |
+
file_content = await file.read()
|
| 24 |
+
result = perform_ocr(file_content, file.filename)
|
| 25 |
+
return result
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 28 |
|
| 29 |
+
def perform_ocr(file_content, file_name):
|
| 30 |
+
# Dosyayı bellekte geçici bir dosya olarak kullan
|
| 31 |
+
with BytesIO(file_content) as buffer:
|
| 32 |
+
if file_name.lower().endswith('.pdf'):
|
| 33 |
+
# PDF dosyasını işle
|
| 34 |
+
results = apply_ocr_on_pdf(buffer)
|
| 35 |
+
elif file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 36 |
+
# Resmi doğrudan işle
|
| 37 |
+
results = apply_ocr_on_image(buffer)
|
| 38 |
+
else:
|
| 39 |
+
raise HTTPException(status_code=400, detail="Unsupported file format")
|
| 40 |
|
| 41 |
return results
|
| 42 |
+
|
| 43 |
+
def apply_ocr_on_pdf(buffer):
|
| 44 |
+
pdf_document = fitz.open("pdf", buffer.read())
|
| 45 |
images = []
|
| 46 |
for page_number in range(pdf_document.page_count):
|
| 47 |
page = pdf_document[page_number]
|
|
|
|
| 53 |
images.append(image)
|
| 54 |
pdf_document.close()
|
| 55 |
|
| 56 |
+
results = []
|
| 57 |
for image in images:
|
| 58 |
+
result = detect_with_paddle.detect_paddle_OCR(image)
|
| 59 |
+
results.extend(result)
|
| 60 |
+
|
| 61 |
+
return results
|
| 62 |
|
| 63 |
+
def apply_ocr_on_image(buffer):
|
| 64 |
+
image = cv2.imdecode(np.frombuffer(buffer.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 65 |
+
image = preproccess.preprocess_image(image)
|
| 66 |
+
result = detect_with_paddle.detect_paddle_OCR(image)
|
| 67 |
+
return result
|
| 68 |
|
| 69 |
+
# Rotate the image around its center
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|