Spaces:
Sleeping
Sleeping
Update ocr_functions.py
Browse files- ocr_functions.py +11 -17
ocr_functions.py
CHANGED
|
@@ -15,10 +15,12 @@ def textract_ocr(image, box):
|
|
| 15 |
img_bytes = io.BytesIO()
|
| 16 |
cropped_image.save(img_bytes, format='PNG')
|
| 17 |
img_bytes = img_bytes.getvalue()
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
response = client.detect_document_text(Document={'Bytes': img_bytes})
|
| 23 |
blocks = response['Blocks']
|
| 24 |
texttract = ""
|
|
@@ -26,38 +28,30 @@ def textract_ocr(image, box):
|
|
| 26 |
for block in blocks:
|
| 27 |
if(block['BlockType'] == 'LINE'):
|
| 28 |
line_confidence[block['Text']] = block['Confidence']
|
| 29 |
-
texttract+= block['Text']+"\n"
|
| 30 |
-
|
| 31 |
-
return texttract
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
def paddle_ocr(image,box):
|
| 36 |
x1, y1, x2, y2 = box
|
| 37 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 38 |
cropped_image = np.array(cropped_image)
|
| 39 |
ocr = PaddleOCR(use_angle_cls=False, lang='latin')
|
| 40 |
result = ocr.ocr(cropped_image, cls=False)
|
| 41 |
-
text= ""
|
| 42 |
-
if result
|
| 43 |
result.sort(key=lambda x: (x[0][0][1], x[0][0][0]))
|
| 44 |
text = [x[1][0] for x in result[0]]
|
| 45 |
return "\n".join(text)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
def tesseract_ocr(image, box):
|
| 50 |
target_dpi = 300
|
| 51 |
x1, y1, x2, y2 = box
|
| 52 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 53 |
cropped_image = cropped_image.convert("L")
|
| 54 |
-
|
| 55 |
current_dpi = cropped_image.info['dpi'][0] if 'dpi' in image.info else None
|
| 56 |
-
|
| 57 |
if current_dpi:
|
| 58 |
scale_factor = target_dpi / current_dpi
|
| 59 |
else:
|
| 60 |
-
|
| 61 |
scale_factor = 1.0
|
| 62 |
binarized_image = cropped_image.filter(ImageFilter.MedianFilter())
|
| 63 |
binarized_image = binarized_image.point(lambda p: p > 180 and 255)
|
|
|
|
| 15 |
img_bytes = io.BytesIO()
|
| 16 |
cropped_image.save(img_bytes, format='PNG')
|
| 17 |
img_bytes = img_bytes.getvalue()
|
| 18 |
+
|
| 19 |
+
client = boto3.client('textract',
|
| 20 |
+
region_name='eu-west-3',
|
| 21 |
+
aws_access_key_id=os.getenv("aws_access_key_id"),
|
| 22 |
+
aws_secret_access_key=os.getenv('aws_secret_access_key'))
|
| 23 |
+
|
| 24 |
response = client.detect_document_text(Document={'Bytes': img_bytes})
|
| 25 |
blocks = response['Blocks']
|
| 26 |
texttract = ""
|
|
|
|
| 28 |
for block in blocks:
|
| 29 |
if(block['BlockType'] == 'LINE'):
|
| 30 |
line_confidence[block['Text']] = block['Confidence']
|
| 31 |
+
texttract += block['Text'] + "\n"
|
| 32 |
+
return texttract
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
def paddle_ocr(image, box):
|
|
|
|
| 35 |
x1, y1, x2, y2 = box
|
| 36 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 37 |
cropped_image = np.array(cropped_image)
|
| 38 |
ocr = PaddleOCR(use_angle_cls=False, lang='latin')
|
| 39 |
result = ocr.ocr(cropped_image, cls=False)
|
| 40 |
+
text = ""
|
| 41 |
+
if result[0] is not None:
|
| 42 |
result.sort(key=lambda x: (x[0][0][1], x[0][0][0]))
|
| 43 |
text = [x[1][0] for x in result[0]]
|
| 44 |
return "\n".join(text)
|
| 45 |
|
|
|
|
|
|
|
| 46 |
def tesseract_ocr(image, box):
|
| 47 |
target_dpi = 300
|
| 48 |
x1, y1, x2, y2 = box
|
| 49 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 50 |
cropped_image = cropped_image.convert("L")
|
|
|
|
| 51 |
current_dpi = cropped_image.info['dpi'][0] if 'dpi' in image.info else None
|
|
|
|
| 52 |
if current_dpi:
|
| 53 |
scale_factor = target_dpi / current_dpi
|
| 54 |
else:
|
|
|
|
| 55 |
scale_factor = 1.0
|
| 56 |
binarized_image = cropped_image.filter(ImageFilter.MedianFilter())
|
| 57 |
binarized_image = binarized_image.point(lambda p: p > 180 and 255)
|