Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,69 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile
|
| 2 |
-
import numpy as np
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from paddleocr import PaddleOCR
|
| 5 |
-
from doctr.io import DocumentFile
|
| 6 |
-
from doctr.models import ocr_predictor
|
| 7 |
-
import io
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
app = FastAPI()
|
| 11 |
-
|
| 12 |
-
# # Load the doctr OCR model
|
| 13 |
-
# os.environ['USE_TORCH'] = 'YES'
|
| 14 |
-
# os.environ['USE_TF'] = 'NO'
|
| 15 |
-
model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
|
| 16 |
-
|
| 17 |
-
def ocr_with_doctr(file):
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
|
| 34 |
-
def ocr_with_paddle(img):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
|
| 45 |
-
def generate_text_from_image(img):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
@app.post("/ocr/")
|
| 51 |
-
async def perform_ocr(file: UploadFile = File(...)):
|
| 52 |
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# from PIL import Image
|
| 4 |
+
# from paddleocr import PaddleOCR
|
| 5 |
+
# from doctr.io import DocumentFile
|
| 6 |
+
# from doctr.models import ocr_predictor
|
| 7 |
+
# import io
|
| 8 |
+
# import os
|
| 9 |
+
|
| 10 |
+
# app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# # # Load the doctr OCR model
|
| 13 |
+
# # os.environ['USE_TORCH'] = 'YES'
|
| 14 |
+
# # os.environ['USE_TF'] = 'NO'
|
| 15 |
+
# model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
|
| 16 |
+
|
| 17 |
+
# def ocr_with_doctr(file):
|
| 18 |
+
# text_output = ''
|
| 19 |
|
| 20 |
+
# # Load the document
|
| 21 |
+
# doc = DocumentFile.from_pdf(file)
|
| 22 |
|
| 23 |
+
# # Perform OCR
|
| 24 |
+
# result = ocr_model(doc)
|
| 25 |
|
| 26 |
+
# # Extract text from OCR result
|
| 27 |
+
# for page in result.pages:
|
| 28 |
+
# for block in page.blocks:
|
| 29 |
+
# for line in block.lines:
|
| 30 |
+
# text_output += " ".join([word.value for word in line.words]) + "\n"
|
| 31 |
|
| 32 |
+
# return text_output
|
| 33 |
|
| 34 |
+
# def ocr_with_paddle(img):
|
| 35 |
+
# finaltext = ''
|
| 36 |
+
# ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
| 37 |
+
# result = ocr.ocr(img)
|
| 38 |
|
| 39 |
+
# for i in range(len(result[0])):
|
| 40 |
+
# text = result[0][i][1][0]
|
| 41 |
+
# finaltext += ' ' + text
|
| 42 |
+
# return finaltext
|
| 43 |
|
| 44 |
|
| 45 |
+
# def generate_text_from_image(img):
|
| 46 |
+
# text_output = ''
|
| 47 |
+
# text_output = ocr_with_paddle(img)
|
| 48 |
+
# return text_output
|
| 49 |
|
| 50 |
+
# @app.post("/ocr/")
|
| 51 |
+
# async def perform_ocr(file: UploadFile = File(...)):
|
| 52 |
|
| 53 |
+
# file_bytes = await file.read()
|
| 54 |
|
| 55 |
+
# if file.filename.endswith('.pdf'):
|
| 56 |
+
# text_output = ocr_with_doctr(io.BytesIO(file_bytes))
|
| 57 |
+
# else:
|
| 58 |
+
# img = np.array(Image.open(io.BytesIO(file_bytes)))
|
| 59 |
+
# text_output = generate_text_from_image(img)
|
| 60 |
+
|
| 61 |
+
# return {"ocr_text": text_output}
|
| 62 |
|
| 63 |
+
mport paddle
|
| 64 |
+
print("PaddlePaddle Version:", paddle.__version__)
|
| 65 |
+
print("Is GPU available:", paddle.is_compiled_with_cuda())
|
| 66 |
+
import tensorflow as tf
|
| 67 |
+
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
|
| 68 |
+
import tensorflow as tf
|
| 69 |
+
print(tf.test.is_built_with_cuda())
|