Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OCR
def test_pipeline(image_path):
img = cv2.imread(image_path)
img = cv2.resize(img, (800, 800))
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# blurred = cv2.GaussianBlur(img_gray, (3, 3), 0)
edged = cv2.Canny(img_gray, 30, 150)
contours = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sort_contours(contours, method="left-to-right")[0]
labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'add', 'div', 'mul', 'sub']
for c in contours:
(x, y, w, h) = cv2.boundingRect(c)
if 20<=w and 30<=h:
roi = img_gray[y:y+h, x:x+w]
thresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
(th, tw) = thresh.shape
if tw > th:
thresh = imutils.resize(thresh, width=32)
if th > tw:
thresh = imutils.resize(thresh, height=32)
(th, tw) = thresh.shape
dx = int(max(0, 32 - tw)/2.0)
dy = int(max(0, 32 - th) / 2.0)
padded = cv2.copyMakeBorder(thresh, top=dy, bottom=dy, left=dx, right=dx, borderType=cv2.BORDER_CONSTANT,
value=(0, 0, 0))
padded = cv2.resize(padded, (32, 32))
padded = np.array(padded)
padded = padded/255.
padded = np.expand_dims(padded, axis=0)
padded = np.expand_dims(padded, axis=-1)
pred = model.predict(padded)
pred = np.argmax(pred, axis=1)
label = labels[pred[0]]
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.putText(img, label, (x-5, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
figure = plt.figure(figsize=(10, 10))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.axis('off')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment