ethanrom commited on
Commit
7d30f01
·
0 Parent(s):

Duplicate from ethanrom/ocr-orderid

Browse files
Files changed (12) hide show
  1. .gitattributes +34 -0
  2. 3app.py +43 -0
  3. README.md +13 -0
  4. app.py +26 -0
  5. button_click.py +44 -0
  6. button_click_alt.py +60 -0
  7. model.h5 +3 -0
  8. packages.txt +1 -0
  9. process.py +78 -0
  10. requirements.txt +4 -0
  11. wapp.py +53 -0
  12. workingapp.py +26 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
3app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import pytesseract
3
+ import streamlit as st
4
+ import numpy as np
5
+
6
+ def main():
7
+ st.set_page_config(page_title='Order ID Finder', layout='wide', page_icon='https://example.com/favicon.ico')
8
+ st.title('Order ID Finder')
9
+ st.image('https://seeklogo.com/images/S/streamlit-logo-1A3B208AE4-seeklogo.com.png', width=200)
10
+ st.write('This app helps you find the order ID from an image of customized jewellery.')
11
+
12
+ with st.sidebar:
13
+ st.write('## Input')
14
+ uploaded_file = st.file_uploader('Upload the image file (PNG or JPG)', type=['png', 'jpg'], help='Upload an image of customized jewellery')
15
+ input_file = st.file_uploader('Upload the input file (TXT)', type=['txt'], help='Upload a TXT file that contains a list of order IDs, names, and font types')
16
+
17
+ if st.button('Find Order ID') and uploaded_file and input_file:
18
+ with st.spinner('Processing image...'):
19
+ img = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
20
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
21
+ text = pytesseract.image_to_string(gray)
22
+ with input_file as file:
23
+ file_contents = file.read()
24
+ lines = file_contents.decode().splitlines()
25
+ found = False
26
+ for line in lines:
27
+ order_id, name, font = line.strip().split(',')
28
+ if name.strip() in text:
29
+ st.success(f'Order ID: {order_id}')
30
+ found = True
31
+ break
32
+ if not found:
33
+ st.error('Could not find the order ID in the image. Please try again with a different image or input file.')
34
+
35
+ st.write('')
36
+ st.write('')
37
+ st.write('')
38
+ st.image('https://www.canpackmachinery.com/wp-content/uploads/2018/03/Footer-Background-01-1.png', width=600)
39
+ st.write('Contact us: [email protected]')
40
+ st.write('Follow us on social media: @example')
41
+
42
+ if __name__ == '__main__':
43
+ main()
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Ocr Orderid
3
+ emoji: 🌍
4
+ colorFrom: pink
5
+ colorTo: gray
6
+ sdk: streamlit
7
+ sdk_version: 1.17.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: ethanrom/ocr-orderid
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ #from button_click import find_order_id
3
+ import tensorflow as tf
4
+ from button_click_alt import find_order_id
5
+
6
+ def main():
7
+ st.set_page_config(page_title='Order ID Finder', layout='wide')
8
+ st.title('OCR + Font type demo')
9
+ st.write('intro')
10
+
11
+ with st.sidebar:
12
+ st.write('## Input')
13
+ uploaded_file = st.file_uploader('Upload the image file (PNG or JPG)', type=['png', 'jpg'], help='help')
14
+ input_file = st.file_uploader('Upload the input file (TXT)', type=['txt'], help='help')
15
+
16
+ if st.button('Find Order ID') and uploaded_file and input_file:
17
+ st.write('## Output')
18
+ model = tf.keras.models.load_model('model.h5')
19
+ result = find_order_id(uploaded_file, input_file, model)
20
+ if result['status'] == 'success':
21
+ st.success(result['message'])
22
+ elif result['status'] == 'warning':
23
+ st.warning(result['message'])
24
+
25
+ if __name__ == '__main__':
26
+ main()
button_click.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import pytesseract
4
+ import tensorflow as tf
5
+ from tensorflow.keras.preprocessing.image import img_to_array, load_img
6
+
7
+ def find_order_id(uploaded_file, input_file, model):
8
+ img = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
9
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
10
+ text = pytesseract.image_to_string(gray)
11
+ with input_file as file:
12
+ file_contents = file.read().decode()
13
+ lines = file_contents.split('\n')
14
+ found = False
15
+ for line in lines:
16
+ order_id, name, font = line.strip().split(',')
17
+ if name.strip() in text:
18
+
19
+ image = load_img(uploaded_file, target_size=(64, 64))
20
+ image = img_to_array(image)
21
+ image = np.expand_dims(image, axis=0)
22
+ image = image / 255.0
23
+ prediction = model.predict(image)
24
+ font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
25
+ result = {
26
+ 'status': 'success',
27
+ 'message': f'Detected Text: {text.strip()}\n, Order ID: {order_id}, Predicted Font Type: {font_type}'
28
+ }
29
+ found = True
30
+ break
31
+
32
+ if not found:
33
+
34
+ image = load_img(uploaded_file, target_size=(64, 64))
35
+ image = img_to_array(image)
36
+ image = np.expand_dims(image, axis=0)
37
+ image = image / 255.0
38
+ prediction = model.predict(image)
39
+ font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
40
+ result = {
41
+ 'status': 'warning',
42
+ 'message': f'Detected Text: {text.strip()}\n, Could not find the Order ID, Predicted Font Type: {font_type}'
43
+ }
44
+ return result
button_click_alt.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import pytesseract
4
+ import tensorflow as tf
5
+ from tensorflow.keras.preprocessing.image import img_to_array, load_img
6
+ from process import preprocess_image
7
+
8
+ def find_order_id(uploaded_file, input_file, model):
9
+ rotated = preprocess_image(uploaded_file)
10
+ text = pytesseract.image_to_string(rotated)
11
+ with input_file as file:
12
+ file_contents = file.read().decode()
13
+ lines = file_contents.split('\n')
14
+ found = False
15
+ possible_order_ids = []
16
+ for line in lines:
17
+ order_id, name, font = line.strip().split(',')
18
+ if name.strip() in text:
19
+ image = load_img(uploaded_file, target_size=(64, 64))
20
+ image = img_to_array(image)
21
+ image = np.expand_dims(image, axis=0)
22
+ image = image / 255.0
23
+ prediction = model.predict(image)
24
+ font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
25
+ if font_type == font.strip():
26
+ result = {
27
+ 'status': 'success',
28
+ 'message': f'Detected Text: {text.strip()}\n, Order ID: {order_id}, Predicted Font Type: {font_type}'
29
+ }
30
+ found = True
31
+ break
32
+ else:
33
+ possible_order_ids.append(order_id)
34
+
35
+ if not found:
36
+ image = load_img(uploaded_file, target_size=(64, 64))
37
+ image = img_to_array(image)
38
+ image = np.expand_dims(image, axis=0)
39
+ image = image / 255.0
40
+ prediction = model.predict(image)
41
+ font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
42
+ for line in lines:
43
+ order_id, name, font = line.strip().split(',')
44
+ if font.strip() == font_type:
45
+ possible_order_ids.append(order_id)
46
+
47
+ if len(possible_order_ids) > 0:
48
+ result = {
49
+ 'status': 'warning',
50
+ 'message': f'Detected Text: {text.strip()}\n, Possible Order IDs: {",".join(possible_order_ids)}, Predicted Font Type: {font_type}'
51
+ }
52
+ else:
53
+ result = {
54
+ 'status': 'error',
55
+ 'message': f'Detected Text: {text.strip()}\n, Could not find the Order ID and possible font matches.'
56
+ }
57
+
58
+ return result
59
+
60
+
model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:148ca2bd9a8292808dd8ab5e1cfde8164fc92c3a8658ea1007cd2f0b4caacd14
3
+ size 8253400
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ tesseract-ocr-all
process.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+
5
+ def preprocess_image_simple(image_file):
6
+ img = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), 1)
7
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
8
+ return gray
9
+
10
+ def preprocess_image(image_file):
11
+ img = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), 1)
12
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13
+
14
+ # Thresholding
15
+ _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
16
+
17
+ # Image Denoising
18
+ blur = cv2.GaussianBlur(thresh, (3,3), 0)
19
+
20
+ # Image Binarization
21
+ thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
22
+
23
+ # Skew Correction
24
+ coords = np.column_stack(np.where(thresh > 0))
25
+ angle = cv2.minAreaRect(coords)[-1]
26
+ if angle < -45:
27
+ angle = -(90 + angle)
28
+ else:
29
+ angle = -angle
30
+ (h, w) = thresh.shape[:2]
31
+ center = (w // 2, h // 2)
32
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
33
+ rotated = cv2.warpAffine(thresh, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
34
+
35
+ return rotated
36
+
37
+
38
+ #not yet working
39
+ def preprocess_image_high(image_file):
40
+ img = cv2.imread(image_file)
41
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
42
+
43
+ # Adaptive thresholding
44
+ thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
45
+
46
+ # Morphological operations
47
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
48
+ closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
49
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
50
+ opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, kernel)
51
+
52
+ # Connected Component Analysis (CCA)
53
+ num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(opened)
54
+ filtered_labels = []
55
+ for i in range(1, num_labels):
56
+ # Filter out components based on their size, aspect ratio, and position
57
+ x, y, w, h, area = stats[i]
58
+ aspect_ratio = float(w) / h
59
+ if area > 100 and aspect_ratio < 5 and aspect_ratio > 0.2 and x > 10 and y > 10:
60
+ filtered_labels.append(i)
61
+ filtered = np.zeros_like(labels)
62
+ for i, label in enumerate(filtered_labels):
63
+ filtered[labels == label] = i + 1
64
+
65
+ # Skew correction
66
+ coords = np.column_stack(np.where(filtered > 0))
67
+ angle = cv2.minAreaRect(coords)[-1]
68
+ if angle < -45:
69
+ angle = -(90 + angle)
70
+ else:
71
+ angle = -angle
72
+ (h, w) = filtered.shape[:2]
73
+ center = (w // 2, h // 2)
74
+ M = cv2.getRotationMatrix2D(center, angle, 1.0)
75
+ rotated = cv2.warpAffine(filtered, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
76
+
77
+ return rotated
78
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ opencv-python-headless
2
+ pytesseract
3
+ numpy
4
+ tensorflow
wapp.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import pytesseract
3
+ import streamlit as st
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
7
+
8
+ def main():
9
+ st.set_page_config(page_title='Order ID Finder', layout='wide')
10
+ st.title('OCR + Font type demo')
11
+ st.write('uses OCR to extract text from an uploaded image of a name. The app then attempts to match the extracted text with the corresponding order ID from a uploaded text file. Also uses pre-trained TensorFlow CNN to classify the font type of the extracted text as either Pacifico or OpenSans-Light. If the app successfully matches the text with an order ID, it outputs the order ID, predicted font type, and detected text. If no match is found, it outputs the predicted font type and detected text.')
12
+
13
+ with st.sidebar:
14
+ st.write('## Input')
15
+ uploaded_file = st.file_uploader('Upload the image file (PNG or JPG)', type=['png', 'jpg'], help='help')
16
+ input_file = st.file_uploader('Upload the input file (TXT)', type=['txt'], help='help')
17
+
18
+ if st.button('Find Order ID') and uploaded_file and input_file:
19
+ st.write('## Output')
20
+ img = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
21
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
22
+ text = pytesseract.image_to_string(gray)
23
+ with input_file as file:
24
+ file_contents = file.read().decode()
25
+ lines = file_contents.split('\n')
26
+ found = False
27
+ for line in lines:
28
+ order_id, name, font = line.strip().split(',')
29
+ if name.strip() in text:
30
+
31
+ image = load_img(uploaded_file, target_size=(64, 64))
32
+ image = img_to_array(image)
33
+ image = np.expand_dims(image, axis=0)
34
+ image = image / 255.0
35
+ prediction = model.predict(image)
36
+ font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
37
+ st.success(f'Detected Text: {text.strip()}\n, Order ID: {order_id}, Predicted Font Type: {font_type}')
38
+ found = True
39
+ break
40
+
41
+ if not found:
42
+
43
+ image = load_img(uploaded_file, target_size=(64, 64))
44
+ image = img_to_array(image)
45
+ image = np.expand_dims(image, axis=0)
46
+ image = image / 255.0
47
+ prediction = model.predict(image)
48
+ font_type = 'Pacifico' if prediction[0, 0] > prediction[0, 1] else 'OpenSans-Light'
49
+ st.warning(f'Detected Text: {text.strip()}\n, Could not find the Order ID, Predicted Font Type: {font_type}')
50
+
51
+ if __name__ == '__main__':
52
+ model = tf.keras.models.load_model('model.h5')
53
+ main()
workingapp.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import pytesseract
3
+ import streamlit as st
4
+ import numpy as np
5
+
6
+ def main():
7
+ st.title('Order ID Finder')
8
+
9
+ uploaded_file = st.file_uploader('Upload an image', type=['png', 'jpg'])
10
+ input_file = st.file_uploader('Upload the input file', type=['txt'])
11
+
12
+ if uploaded_file and input_file:
13
+ img = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
14
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
15
+ text = pytesseract.image_to_string(gray)
16
+ with input_file as file:
17
+ file_contents = file.read()
18
+ lines = file_contents.decode().splitlines()
19
+ for line in lines:
20
+ order_id, name, font = line.strip().split(',')
21
+ if name.strip() in text:
22
+ st.success(f'Order ID: {order_id}')
23
+ break
24
+
25
+ if __name__ == '__main__':
26
+ main()