Spaces:
Build error
Build error
deneme-4
Browse files- Dockerfile +22 -0
- README.md +4 -5
- api.py +111 -0
- deskew.py +55 -0
- detect.py +41 -0
- read_data.py +14 -0
- requirements.txt +14 -0
- tableocr.py +85 -0
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ana imaj olarak Python imajını kullanın
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Uygulamanın çalışacağı dizini oluşturun
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Gerekli bağımlılıkları kopyalayın ve yükleyin
|
| 8 |
+
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install\
|
| 9 |
+
libgl1\
|
| 10 |
+
libgl1-mesa-glx \
|
| 11 |
+
libglib2.0-0 -y\
|
| 12 |
+
libgomp1
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
RUN pip install python-multipart
|
| 15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
+
|
| 17 |
+
# Uygulama dosyalarını kopyalayın
|
| 18 |
+
COPY . .
|
| 19 |
+
|
| 20 |
+
# Docker içinde FastAPI uygulamasını çalıştırın
|
| 21 |
+
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 22 |
+
|
README.md
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
-
license: mit
|
| 9 |
---
|
| 10 |
|
| 11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Invoices Api
|
| 3 |
+
emoji: 📚
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
api.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
+
from typing import List, Any
|
| 8 |
+
from pydantic import BaseModel
|
| 9 |
+
from pymongo import MongoClient
|
| 10 |
+
import dns.resolver
|
| 11 |
+
from pymongo.server_api import ServerApi
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
import base64
|
| 14 |
+
from detect import detect_with_yolo
|
| 15 |
+
from tableocr import get_list
|
| 16 |
+
from deskew import deskew
|
| 17 |
+
from read_data import get_text
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
| 21 |
+
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
|
| 25 |
+
dns.resolver.default_resolver.nameservers=['8.8.8.8']
|
| 26 |
+
|
| 27 |
+
uri = "mongodb+srv://senel1806:dEWqI0FVFMDRpiFl@cluster0.f2kg7rn.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
| 28 |
+
client = MongoClient(uri, server_api=ServerApi('1'))
|
| 29 |
+
db = client['invoices']
|
| 30 |
+
collection = db['invoices']
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class ProcessedData(BaseModel):
|
| 34 |
+
company_name: str
|
| 35 |
+
invoice_number: str
|
| 36 |
+
invoice_date: str
|
| 37 |
+
table_data: List[Any]
|
| 38 |
+
|
| 39 |
+
class ImageData(BaseModel):
|
| 40 |
+
image: str
|
| 41 |
+
|
| 42 |
+
class BatchProcessedData(BaseModel):
|
| 43 |
+
results: List[ProcessedData]
|
| 44 |
+
|
| 45 |
+
class Invoice(BaseModel):
|
| 46 |
+
code: str
|
| 47 |
+
timestamp: str
|
| 48 |
+
date: str
|
| 49 |
+
isTransferred: bool
|
| 50 |
+
images: List[Any]
|
| 51 |
+
data: List[Any]
|
| 52 |
+
|
| 53 |
+
def extractData(input_img):
|
| 54 |
+
result_img, table_img, company_img, number_img, date_img = detect_with_yolo(input_img)
|
| 55 |
+
|
| 56 |
+
company_name = "" # Şirket adı
|
| 57 |
+
if company_img is not None and company_img.size > 0:
|
| 58 |
+
company_name = get_text(company_img)
|
| 59 |
+
|
| 60 |
+
invoice_number = "" # Fatura numarası
|
| 61 |
+
if number_img is not None and number_img.size > 0:
|
| 62 |
+
invoice_number = get_text(number_img)
|
| 63 |
+
|
| 64 |
+
invoice_date = "" # Fatura tarihi
|
| 65 |
+
if date_img is not None and date_img.size > 0:
|
| 66 |
+
invoice_date = get_text(date_img)
|
| 67 |
+
|
| 68 |
+
table_img = deskew(table_img)
|
| 69 |
+
data = get_list(table_img)
|
| 70 |
+
max_columns = max(len(row) for row in data)
|
| 71 |
+
column_names = [f"Column{i+1}" for i in range(max_columns)] # Column1, Column2, ... şeklinde sütun adları oluştur
|
| 72 |
+
df = pd.DataFrame(data, columns=column_names)
|
| 73 |
+
|
| 74 |
+
return df, company_name, invoice_number, invoice_date
|
| 75 |
+
|
| 76 |
+
@app.post("/process-images/", response_model=BatchProcessedData)
|
| 77 |
+
async def process_images(image_data_list: List[ImageData]):
|
| 78 |
+
if not image_data_list:
|
| 79 |
+
raise HTTPException(status_code=400, detail="Image data list cannot be empty.")
|
| 80 |
+
results = []
|
| 81 |
+
for image_data in image_data_list:
|
| 82 |
+
image_bytes = base64.b64decode(image_data.image)
|
| 83 |
+
nparr = np.frombuffer(image_bytes, np.uint8)
|
| 84 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 85 |
+
|
| 86 |
+
df, company_name, invoice_number, invoice_date = extractData(img)
|
| 87 |
+
|
| 88 |
+
table_data = df.to_dict(orient='records')
|
| 89 |
+
|
| 90 |
+
response_data = ProcessedData(
|
| 91 |
+
company_name=company_name,
|
| 92 |
+
invoice_number=invoice_number,
|
| 93 |
+
invoice_date=invoice_date,
|
| 94 |
+
table_data=table_data
|
| 95 |
+
)
|
| 96 |
+
results.append(response_data.model_dump()) # Use model_dump instead of dict
|
| 97 |
+
|
| 98 |
+
return JSONResponse(content=results)
|
| 99 |
+
|
| 100 |
+
@app.post("/save-invoice")
|
| 101 |
+
async def save_invoice(invoice: Invoice, request: Request):
|
| 102 |
+
try:
|
| 103 |
+
invoice_data = await request.json()
|
| 104 |
+
print("Received payload:", invoice_data) # Log the payload
|
| 105 |
+
invoice = Invoice(**invoice_data) # Validate the payload using Pydantic
|
| 106 |
+
collection.insert_one(invoice.model_dump())
|
| 107 |
+
return {"message": "Invoice saved successfully"}
|
| 108 |
+
except ValidationError as e:
|
| 109 |
+
raise HTTPException(status_code=422, detail=e.errors())
|
| 110 |
+
except Exception as e:
|
| 111 |
+
raise HTTPException(status_code=500, detail=str(e))
|
deskew.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def getSkewAngle(cvImage) -> float:
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Prep image, copy, convert to gray scale, blur, and threshold
|
| 8 |
+
newImage = cvImage.copy()
|
| 9 |
+
gray = cv2.cvtColor(newImage, cv2.COLOR_BGR2GRAY)
|
| 10 |
+
blur = cv2.GaussianBlur(gray, (9, 9), 0)
|
| 11 |
+
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
| 12 |
+
|
| 13 |
+
# Apply dilate to merge text into meaningful lines/paragraphs.
|
| 14 |
+
# Use larger kernel on X axis to merge characters into single line, cancelling out any spaces.
|
| 15 |
+
# But use smaller kernel on Y axis to separate between different blocks of text
|
| 16 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 5))
|
| 17 |
+
dilate = cv2.dilate(thresh, kernel, iterations=2)
|
| 18 |
+
|
| 19 |
+
# Find all contours
|
| 20 |
+
contours, hierarchy = cv2.findContours(dilate, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
|
| 21 |
+
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 1000]
|
| 22 |
+
contours = sorted(contours, key = cv2.contourArea, reverse = True)
|
| 23 |
+
for c in contours:
|
| 24 |
+
rect = cv2.boundingRect(c)
|
| 25 |
+
x,y,w,h = rect
|
| 26 |
+
cv2.rectangle(newImage,(x,y),(x+w,y+h),(0,255,0),2)
|
| 27 |
+
|
| 28 |
+
# Find largest contour and surround in min area box
|
| 29 |
+
largestContour = contours[0]
|
| 30 |
+
minAreaRect = cv2.minAreaRect(largestContour)
|
| 31 |
+
# Determine the angle. Convert it to the value that was originally used to obtain skewed image
|
| 32 |
+
angle = minAreaRect[-1]
|
| 33 |
+
print(angle)
|
| 34 |
+
if angle < -45:
|
| 35 |
+
angle = 90 + angle
|
| 36 |
+
|
| 37 |
+
return -1.0 * angle
|
| 38 |
+
# Rotate the image around its center
|
| 39 |
+
def rotateImage(cvImage, angle: float):
|
| 40 |
+
newImage = cvImage.copy()
|
| 41 |
+
(h, w) = newImage.shape[:2]
|
| 42 |
+
center = (w // 2, h // 2)
|
| 43 |
+
M = cv2.getRotationMatrix2D(center, angle, 1.0)
|
| 44 |
+
newImage = cv2.warpAffine(newImage, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
|
| 45 |
+
return newImage
|
| 46 |
+
|
| 47 |
+
def deskew(cvImage):
|
| 48 |
+
angle = getSkewAngle(cvImage)
|
| 49 |
+
check_angle = angle * -1.0
|
| 50 |
+
if check_angle > -60 and check_angle < 60:
|
| 51 |
+
rotated_image = rotateImage(cvImage, -1.0 * angle)
|
| 52 |
+
else:
|
| 53 |
+
rotated_image = cvImage
|
| 54 |
+
return rotated_image
|
| 55 |
+
|
detect.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import cv2
|
| 3 |
+
|
| 4 |
+
model = YOLO('last (1).pt')
|
| 5 |
+
|
| 6 |
+
def detect_with_yolo(img):
|
| 7 |
+
color_palette = [(0, 255, 0), (0, 0, 255), (255, 0, 0), (0, 165, 255), (128, 0, 128), (255, 255, 0), (255, 128, 0)]
|
| 8 |
+
|
| 9 |
+
results = model.predict(img, stream=True)
|
| 10 |
+
|
| 11 |
+
detected_img = img.copy() # Copy of the original image
|
| 12 |
+
table_roi = None
|
| 13 |
+
company_roi = None
|
| 14 |
+
number_roi = None
|
| 15 |
+
date_roi = None
|
| 16 |
+
|
| 17 |
+
for result in results:
|
| 18 |
+
boxes = result.boxes.cpu().numpy() # Get boxes on CPU in numpy format
|
| 19 |
+
for box in boxes: # Iterate over boxes
|
| 20 |
+
r = box.xyxy[0].astype(int) # Get corner points as int
|
| 21 |
+
class_id = int(box.cls[0]) # Get class ID
|
| 22 |
+
class_name = model.names[class_id] # Get class name using the class ID
|
| 23 |
+
print(f"Class: {class_name}, Box: {r}") # Print class name and box coordinates
|
| 24 |
+
# Draw boxes on the image
|
| 25 |
+
color = color_palette[class_id]
|
| 26 |
+
cv2.rectangle(detected_img, (r[0], r[1]), (r[2], r[3]), color, 2)
|
| 27 |
+
|
| 28 |
+
# Check if the detected object is a table
|
| 29 |
+
if class_name == 'table':
|
| 30 |
+
table_roi = img[r[1]:r[3], r[0]:r[2]] # Extract ROI for the table
|
| 31 |
+
if class_name == 'company':
|
| 32 |
+
company_roi = img[r[1]:r[3], r[0]:r[2]] # Extract ROI for the table
|
| 33 |
+
if class_name == 'number':
|
| 34 |
+
number_roi = img[r[1]:r[3], r[0]:r[2]] # Extract ROI for the table
|
| 35 |
+
if class_name == 'date':
|
| 36 |
+
date_roi = img[r[1]:r[3], r[0]:r[2]] # Extract ROI for the table
|
| 37 |
+
print("TABLE ROI ",table_roi)
|
| 38 |
+
print("COMPANY ROI ",company_roi)
|
| 39 |
+
print("NUMBER ROI ",number_roi)
|
| 40 |
+
print("DATE ROI ",date_roi)
|
| 41 |
+
return detected_img, table_roi, company_roi, number_roi, date_roi
|
read_data.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from paddleocr import PaddleOCR
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
current_directory = os.getcwd()
|
| 5 |
+
|
| 6 |
+
model = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=True, det_model_dir=f"{current_directory}/.paddleocr/whl/det/en/en_PP-OCRv3_det_infer", rec_model_dir=f"{current_directory}/.paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer", cls_model_dir=f"{current_directory}/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer")
|
| 7 |
+
|
| 8 |
+
def get_text(img) -> str:
|
| 9 |
+
result = model.ocr(img, cls=False)
|
| 10 |
+
# OCR sonuçlarını ayıkla
|
| 11 |
+
for iResult in result:
|
| 12 |
+
for res in iResult:
|
| 13 |
+
line_text = res[1][0]
|
| 14 |
+
return line_text
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
gunicorn
|
| 3 |
+
shutil-extra
|
| 4 |
+
PyMuPDF
|
| 5 |
+
opencv-python==4.6.0.66
|
| 6 |
+
pillow==10.2.0
|
| 7 |
+
uvicorn
|
| 8 |
+
paddleocr==2.7.0
|
| 9 |
+
paddlepaddle
|
| 10 |
+
python-dotenv
|
| 11 |
+
ultralytics
|
| 12 |
+
dill
|
| 13 |
+
scikit-learn
|
| 14 |
+
pymongo==4.8.0
|
tableocr.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from paddleocr import PaddleOCR
|
| 2 |
+
import cv2
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.cluster import DBSCAN
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
current_directory = os.getcwd()
|
| 9 |
+
# OCR modelini başlat
|
| 10 |
+
model = PaddleOCR(use_angle_cls=True, lang='en', use_gpu=True, det_model_dir=f"{current_directory}/.paddleocr/whl/det/en/en_PP-OCRv3_det_infer", rec_model_dir=f"{current_directory}/.paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer", cls_model_dir=f"{current_directory}/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer")
|
| 11 |
+
|
| 12 |
+
# Resmi oku ve RGB formatına dönüştür
|
| 13 |
+
# image_path = "test_images/3.jpg"
|
| 14 |
+
# image = cv2.imread(image_path)
|
| 15 |
+
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 16 |
+
|
| 17 |
+
def get_list(img) -> list:
|
| 18 |
+
result = model.ocr(img, cls=False)
|
| 19 |
+
sorted_text = ocr_result_to_dataframe(result)
|
| 20 |
+
df = pd.DataFrame(sorted_text)
|
| 21 |
+
return sorted_text
|
| 22 |
+
|
| 23 |
+
def ocr_result_to_dataframe(result):
|
| 24 |
+
all_texts = []
|
| 25 |
+
all_coords = []
|
| 26 |
+
|
| 27 |
+
# OCR sonuçlarını ayıkla
|
| 28 |
+
for iResult in result:
|
| 29 |
+
for res in iResult:
|
| 30 |
+
line_text = res[1][0]
|
| 31 |
+
box_coordinates = res[0]
|
| 32 |
+
all_texts.append(line_text)
|
| 33 |
+
all_coords.append(box_coordinates)
|
| 34 |
+
|
| 35 |
+
# Her bir kutunun merkez y koordinatını hesapla
|
| 36 |
+
centers_y = [np.mean([coord[1] for coord in box]) for box in all_coords]
|
| 37 |
+
heights = [max(coord[1] for coord in box) - min(coord[1] for coord in box) for box in all_coords]
|
| 38 |
+
|
| 39 |
+
# Ortalama yüksekliği hesapla ve eşik değeri olarak kullan
|
| 40 |
+
avg_height = np.mean(heights)
|
| 41 |
+
y_threshold = avg_height * 0.5 # Y ekseni farkı için eşik
|
| 42 |
+
|
| 43 |
+
# DBSCAN kullanarak satırları gruplandır
|
| 44 |
+
dbscan = DBSCAN(eps=y_threshold, min_samples=1)
|
| 45 |
+
labels = dbscan.fit_predict(np.array(centers_y).reshape(-1, 1))
|
| 46 |
+
|
| 47 |
+
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
|
| 48 |
+
grouped_texts = [[] for _ in range(n_clusters)]
|
| 49 |
+
grouped_coords = [[] for _ in range(n_clusters)]
|
| 50 |
+
for text, coords, label in zip(all_texts, all_coords, labels):
|
| 51 |
+
if label != -1:
|
| 52 |
+
grouped_texts[label].append(text)
|
| 53 |
+
grouped_coords[label].append(coords)
|
| 54 |
+
|
| 55 |
+
# Grupları y koordinatlarına göre sırala
|
| 56 |
+
grouped_texts_coords = sorted(zip(grouped_texts, grouped_coords), key=lambda x: np.mean([np.mean([coord[1] for coord in box]) for box in x[1]]))
|
| 57 |
+
|
| 58 |
+
sorted_texts = []
|
| 59 |
+
for group_texts, group_coords in grouped_texts_coords:
|
| 60 |
+
# Her grubu x koordinatına göre sırala (soldan sağa)
|
| 61 |
+
sorted_group = sorted(zip(group_texts, group_coords), key=lambda x: x[1][0][0])
|
| 62 |
+
if len(sorted_group) > 3:
|
| 63 |
+
first_text, first_coord = sorted_group[0] # Listenin ilk elemanını al
|
| 64 |
+
if any(char.isalpha() for char in first_text): # Harf kontrolü
|
| 65 |
+
# Harf içeren bölümü ayır
|
| 66 |
+
split_text = first_text.split()
|
| 67 |
+
index = len(split_text[0])
|
| 68 |
+
if index < len(first_text): # İlk kelime sonrasında bir şey varsa
|
| 69 |
+
remaining_text = first_text[index:]
|
| 70 |
+
sorted_group[0] = (split_text[0], first_coord) # İlk elemanı güncelle
|
| 71 |
+
# Yeni elemanı oluşturarak listeye ekle
|
| 72 |
+
sorted_group.insert(1, (remaining_text, first_coord))
|
| 73 |
+
sorted_texts.append([text for text, _ in sorted_group])
|
| 74 |
+
|
| 75 |
+
return sorted_texts
|
| 76 |
+
|
| 77 |
+
# # OCR sonuçlarını satırlara dönüştür
|
| 78 |
+
# grouped_text = ocr_result_to_dataframe(result)
|
| 79 |
+
|
| 80 |
+
# DataFrame'e dönüştür ve Excel dosyasına yaz
|
| 81 |
+
# df = pd.DataFrame(grouped_text)
|
| 82 |
+
# df.to_excel('ocr_results.xlsx', index=False, header=False)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# get_list(image)
|