Spaces:
Runtime error
Runtime error
gamingflexer
commited on
Commit
·
59e430b
1
Parent(s):
620e74a
Add OCR functionality using Azure Form Recognizer and EasyOCR libraries
Browse files- src/ocr.py +31 -0
src/ocr.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from azure.ai.formrecognizer import DocumentAnalysisClient
|
| 2 |
+
from azure.core.credentials import AzureKeyCredential
|
| 3 |
+
from config import key, endpoint
|
| 4 |
+
import easyocr
|
| 5 |
+
|
| 6 |
+
def azure_ocr(image_path):
|
| 7 |
+
try:
|
| 8 |
+
# Create a DocumentAnalysisClient instance
|
| 9 |
+
document_analysis_client = DocumentAnalysisClient(
|
| 10 |
+
endpoint=endpoint, credential=AzureKeyCredential(key)
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Open the image file and begin document analysis
|
| 14 |
+
with open(image_path, "rb") as image_file:
|
| 15 |
+
poller = document_analysis_client.begin_analyze_document(
|
| 16 |
+
"prebuilt-read", document=image_file
|
| 17 |
+
)
|
| 18 |
+
result = poller.result()
|
| 19 |
+
return result.content
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print('Error occurred:', e)
|
| 22 |
+
return ""
|
| 23 |
+
|
| 24 |
+
def easy_ocr(image_path):
|
| 25 |
+
try:
|
| 26 |
+
reader = easyocr.Reader(['en','hi','bn','mr','ta','te'])
|
| 27 |
+
result = reader.readtext(image_path)
|
| 28 |
+
return result
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print('Error occurred:', e)
|
| 31 |
+
return []
|