File size: 5,623 Bytes
91b3708 bb17b44 91b3708 1c7c7e7 91b3708 1c7c7e7 91b3708 1c7c7e7 91b3708 1c7c7e7 91b3708 1c7c7e7 91b3708 1c7c7e7 91b3708 1c7c7e7 91b3708 bb17b44 23c4525 91b3708 1c7c7e7 91b3708 1c7c7e7 2c6a71d 1c7c7e7 23c4525 91b3708 1c7c7e7 91b3708 1c7c7e7 91b3708 | 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 | import cv2
import os
import numpy as np
from io import BytesIO
from PIL import Image
from fastapi import FastAPI, File, UploadFile, HTTPException
import easyocr
import fitz
app = FastAPI()
current_directory = os.getcwd()
@app.get('/')
async def index():
return "Welcome EasyOCR By SENEL"
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
try:
# Dosyayı bellekte geçici bir nesne olarak tut
file_content = await file.read()
result = perform_ocr(file_content, file.filename)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
def perform_ocr(file_content, file_name):
# Dosyayı bellekte geçici bir dosya olarak kullan
with BytesIO(file_content) as buffer:
if file_name.lower().endswith('.pdf'):
# PDF dosyasını işle
results = apply_ocr_on_pdf(buffer)
elif file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
# Resmi doğrudan işle
results = apply_ocr_on_image(buffer)
else:
raise HTTPException(status_code=400, detail="Unsupported file format")
return results
def apply_ocr_on_pdf(buffer):
pdf_document = fitz.open("pdf", buffer.read())
images = []
for page_number in range(pdf_document.page_count):
page = pdf_document[page_number]
pixmap = page.get_pixmap(dpi=400)
image = Image.frombytes("RGB", [pixmap.width, pixmap.height], pixmap.samples)
img_array = np.array(image)
img_array_bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
image = preprocess_image(img_array_bgr)
images.append(image)
pdf_document.close()
results = []
for image in images:
reader = easyocr.Reader(['tr'], gpu=True, model_storage_directory=f"{current_directory}/model", user_network_directory=f"{current_directory}/user_network", download_enabled=False)
result = reader.readtext(image,min_size=50, detail=0, paragraph=False, width_ths=0.9, blocklist="|[]{}")
results.extend(result)
return results
def apply_ocr_on_image(buffer):
reader = easyocr.Reader(['tr'], gpu=True, model_storage_directory=f"{current_directory}/model", user_network_directory=f"{current_directory}/user_network", download_enabled=False)
image = cv2.imdecode(np.frombuffer(buffer.read(), np.uint8), cv2.IMREAD_COLOR)
image = preprocess_image(image)
result = reader.readtext(image, min_size=50, detail=0, paragraph=False, width_ths=1)
return result
def getSkewAngle(cvImage) -> float:
# Prep image, copy, convert to gray scale, blur, and threshold
newImage = cvImage.copy()
gray = cv2.cvtColor(newImage, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9, 9), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Apply dilate to merge text into meaningful lines/paragraphs.
# Use larger kernel on X axis to merge characters into single line, cancelling out any spaces.
# But use smaller kernel on Y axis to separate between different blocks of text
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 5))
dilate = cv2.dilate(thresh, kernel, iterations=2)
# Find all contours
contours, hierarchy = cv2.findContours(dilate, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 1000]
contours = sorted(contours, key = cv2.contourArea, reverse = True)
for c in contours:
rect = cv2.boundingRect(c)
x,y,w,h = rect
cv2.rectangle(newImage,(x,y),(x+w,y+h),(0,255,0),2)
# Find largest contour and surround in min area box
largestContour = contours[0]
print (len(contours))
minAreaRect = cv2.minAreaRect(largestContour)
# Determine the angle. Convert it to the value that was originally used to obtain skewed image
angle = minAreaRect[-1]
if angle < -45:
angle = 90 + angle
return -1.0 * angle
# Rotate the image around its center
def rotateImage(cvImage, angle: float):
newImage = cvImage.copy()
(h, w) = newImage.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
newImage = cv2.warpAffine(newImage, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return newImage
def deskew(cvImage):
angle = getSkewAngle(cvImage)
check_angle = angle * -1.0
if check_angle > -60 and check_angle < 60:
rotated_image = rotateImage(cvImage, -1.0 * angle)
else:
rotated_image = cvImage
return rotated_image
def preprocess_image(img):
norm_img = np.zeros((img.shape[0], img.shape[1]))
img = cv2.normalize(img, norm_img, 0, 255, cv2.NORM_MINMAX)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
noiseless_image_bw = cv2.fastNlMeansDenoising(thresh, None, 10, 7, 21)
kernel = np.ones((2,2),np.uint8)
dilate = cv2.dilate(noiseless_image_bw, kernel, iterations = 1)
erosion = cv2.erode(dilate, kernel, iterations = 1)
image = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, kernel)
image = cv2.medianBlur(image, 3)
image = cv2.bitwise_not(image)
kernel = np.ones((2,2),np.uint8)
image = cv2.dilate(image, kernel, iterations=1)
image = cv2.erode(image, kernel, iterations = 1)
image = cv2.bitwise_not(image)
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
image = cv2.detailEnhance(img, sigma_s=80, sigma_r=0.8)
image = deskew(image)
return image |