-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathpredict.py
55 lines (38 loc) · 1.91 KB
/
predict.py
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
46
47
48
49
50
51
52
import cv2
import numpy as np
from tensorflow.keras.models import load_model # type: ignore
import streamlit as st
labels = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '+', 11: '/', 12: '*', 13: '-'}
model = load_model('../Models/cnn_model.h5')
def predict(image_path):
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
_, binary_image = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
bounding_boxes = [cv2.boundingRect(contour) for contour in contours]
sorted_indices = sorted(range(len(bounding_boxes)), key=lambda i: bounding_boxes[i][0])
sorted_contours = [contours[i] for i in sorted_indices]
rois = []
padding = 10
for contour in sorted_contours:
x, y, w, h = cv2.boundingRect(contour)
x_start = max(0, x - padding)
y_start = max(0, y - padding)
x_end = min(image.shape[1], x + w + padding)
y_end = min(image.shape[0], y + h + padding)
roi = image[y_start:y_end, x_start:x_end]
roi = cv2.resize(roi, (32, 32))
rois.append(roi)
rois = np.array(rois)
rois = rois / 255.0
rois = np.expand_dims(rois, axis=-1)
predictions = model.predict(rois)
predicted_labels = np.argmax(predictions, axis=1)
image_color = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(sorted_contours):
x, y, w, h = cv2.boundingRect(contour)
label = labels[predicted_labels[i]]
cv2.rectangle(image_color, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(image_color, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
st.image(cv2.cvtColor(image_color, cv2.COLOR_BGR2RGB))
equation = ''.join(labels[predicted_labels[i]] for i in range(len(predicted_labels)))
return equation.replace('*','x'), eval(equation)