File size: 1,816 Bytes
7d30f01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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