Why my code just identify 3 of the 4 numbers correctly?
Here I make an outline in the numbers
And here I try to identify correctly the numbers
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
def segment_digits(image):
_, thresh = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
digits = []
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
if h > 20 and w > 10:
digit = image[y:y+h, x:x+w]
digit = cv2.resize(digit, (28, 28))
digits.append((x, digit))
digits = sorted(digits, key=lambda d: d[0])
return [d[1] for d in digits]
def train_knn_model():
(x_train, y_train), (_, _) = mnist.load_data()
x_train = x_train.reshape((x_train.shape[0], 28 * 28)).astype(np.float32)
y_train = y_train.astype(np.float32)
knn = cv2.ml.KNearest_create()
knn.train(x_train, cv2.ml.ROW_SAMPLE, y_train)
return knn
def predict_digit(model, digit):
sample = digit.reshape((1, 784)).astype(np.float32)
_, result, _, _ = model.findNearest(sample, k=1)
return int(result[0][0])
image_path = 'digits_image.png'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if image is None:
print("Erro ao carregar a imagem.")
exit()
digits = segment_digits(image)
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title("Imagem Original")
_, thresh = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contoured_image = cv2.drawContours(image.copy(), contours, -1, (255, 0, 0), 2)
plt.subplot(1, 2, 2)
plt.imshow(contoured_image, cmap='gray')
plt.title("Imagem com Contornos")
plt.show()
if len(digits) != 4:
print(f"Não foram encontrados 4 dígitos na imagem. Foram encontrados {len(digits)} dígitos.")
exit()
knn_model = train_knn_model()
predicted_digits = [predict_digit(knn_model, digit) for digit in digits]
for i, digit in enumerate(digits):
plt.subplot(1, 4, i + 1)
plt.imshow(digit, cmap='gray')
plt.title(predicted_digits[i])
plt.axis('off')
plt.show()
print("Predicted digits:", predicted_digits)
I try use tensor flow, and train a model to solve the problem, but I’m not able to solve this only problem. Actualy, a dunno how to train correctly a model, so a need help, pls, and thx