Add text extraction
Browse files
main.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import io
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
import base64
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from pickle import load
|
| 6 |
from PIL import Image
|
|
@@ -12,6 +13,16 @@ app = Flask(__name__)
|
|
| 12 |
|
| 13 |
MAX_LENGTH = 38
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def extract_features(image_data, model):
|
| 16 |
try:
|
| 17 |
image = Image.open(io.BytesIO(image_data))
|
|
@@ -65,19 +76,27 @@ def generate_caption():
|
|
| 65 |
# Decode the Base64 string into binary image data
|
| 66 |
image_data = base64.b64decode(base64_image_data)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
return jsonify({'caption': caption}), 200
|
| 81 |
except Exception as e:
|
| 82 |
return jsonify({'error': str(e)}), 500
|
| 83 |
|
|
|
|
| 1 |
import io
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
import base64
|
| 4 |
+
import pytesseract
|
| 5 |
import numpy as np
|
| 6 |
from pickle import load
|
| 7 |
from PIL import Image
|
|
|
|
| 13 |
|
| 14 |
MAX_LENGTH = 38
|
| 15 |
|
| 16 |
+
def format_tesseract_output(output_text):
|
| 17 |
+
formatted_text = ""
|
| 18 |
+
lines = output_text.strip().split("\n")
|
| 19 |
+
for line in lines:
|
| 20 |
+
line = line.strip()
|
| 21 |
+
if line:
|
| 22 |
+
formatted_text += line + "\n"
|
| 23 |
+
return formatted_text
|
| 24 |
+
|
| 25 |
+
|
| 26 |
def extract_features(image_data, model):
|
| 27 |
try:
|
| 28 |
image = Image.open(io.BytesIO(image_data))
|
|
|
|
| 76 |
# Decode the Base64 string into binary image data
|
| 77 |
image_data = base64.b64decode(base64_image_data)
|
| 78 |
|
| 79 |
+
# Convert the image data to a PIL image object
|
| 80 |
+
pil_image = Image.open(io.BytesIO(img_path))
|
| 81 |
+
|
| 82 |
+
extracted_text = pytesseract.image_to_string(pil_image, lang="eng+chi_sim+msa")
|
| 83 |
+
hasText = bool(extracted_text.strip())
|
| 84 |
+
|
| 85 |
+
if hasText:
|
| 86 |
+
result = format_tesseract_output(extracted_text)
|
| 87 |
+
else:
|
| 88 |
+
tokenizer = load(open("tokenizer.p","rb"))
|
| 89 |
+
model = load_model('model_9.keras')
|
| 90 |
|
| 91 |
+
xception_model = Xception(include_top=False, pooling="avg")
|
| 92 |
+
photo = extract_features(image_data, xception_model)
|
| 93 |
|
| 94 |
+
if photo is None:
|
| 95 |
+
return jsonify({'error': 'Failed to extract features from the image'}), 400
|
| 96 |
|
| 97 |
+
result = generate_desc(model, tokenizer, photo, MAX_LENGTH)
|
| 98 |
|
| 99 |
+
return jsonify({'hasText': hasText, 'result': result}), 200
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
return jsonify({'error': str(e)}), 500
|
| 102 |
|