为什么我的代码只能正确识别 4 个数字中的 3 个?这里我在数字中做了一个轮廓然后在这里我尝试正确识别数字import cv2import numpy as npimport matplotlib.pypl...
为什么我的代码只能正确识别 4 个数字中的 3 个?
我在这里用数字做了一个概述
在这里我尝试正确识别数字
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)
我尝试使用张量流并训练模型来解决问题,但我无法解决这唯一的问题。实际上,我不知道如何正确训练模型,所以需要帮助,请,谢谢