ocr-orderid3 / button_click.py
ethanrom's picture
Duplicate from ethanrom/ocr-orderid
7d30f01
import cv2
import numpy as np
import pytesseract
import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array, load_img
def find_order_id(uploaded_file, input_file, model):
img = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
text = pytesseract.image_to_string(gray)
with input_file as file:
file_contents = file.read().decode()
lines = file_contents.split('\n')
found = False
for line in lines:
order_id, name, font = line.strip().split(',')
if name.strip() in text:
image = load_img(uploaded_file, target_size=(64, 64))
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = image / 255.0
prediction = model.predict(image)
font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
result = {
'status': 'success',
'message': f'Detected Text: {text.strip()}\n, Order ID: {order_id}, Predicted Font Type: {font_type}'
}
found = True
break
if not found:
image = load_img(uploaded_file, target_size=(64, 64))
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = image / 255.0
prediction = model.predict(image)
font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
result = {
'status': 'warning',
'message': f'Detected Text: {text.strip()}\n, Could not find the Order ID, Predicted Font Type: {font_type}'
}
return result