我正在制作一个移动应用程序,在其中的第一部分,用户将不得不拍摄一个sudoku网格的照片,计算机将扫描和阅读它,使用我受过训练的TensorFlow模型。
我对TensorFlow模型有一个很大的问题,它似乎不太擅长它的工作,我认为这不是模型的错,而是被发送的张量没有以数字为中心。
显然,我不期望100%的准确性,但特别是数字打印在[网格,我希望更好,因为大约20%的数字似乎是错误的。
img=<base64 string represeting 313*320 image of grid>
[This is the base64 image at the top](https://i.stack.imgur.com/OrPis.jpg)
import cv2
import json
import numpy as np
import tensorflow as tf
import base64
import matplotlib.pyplot as plt
model = tf.keras.models.load_model("newmodel")
data = base64.b64decode(img)
np_data = np.fromstring(data, np.uint8)
img = cv2.imdecode(np_data, cv2.IMREAD_GRAYSCALE)
height, width = img.shape
img = cv2.resize(img, (width, width))
height, width = img.shape
print(height,width)
nums = []
for y in range(9):
row = []
for x in range(9):
left = round(x*(width)/9+3)
top = round(y*(height)/9+3)
right = round((x+1)*(width)/9-3)
bottom = round((y+1)*(height)/9-3)
image = img[top:bottom, left:right]
image = np.array(image)
image = cv2.resize(image, (28,28))
image = 255-image
#Checking for empty cells
numofBlack = 0
for r in image:
for item in r:
if item > 127:
numofBlack += 1
if numofBlack < 50:
row.append(0)
else:
pred = model.predict(image.reshape(1,28, 28, 1))
row.append(int(pred.argmax()))
nums.append(row)
print(nums)
上面图像中的网格返回:
[8,3,0,0,7,0,0,0,0,0,8,0,0,1,9,5,0,0,0,0,9,8,0,0,0,0,6,0,8,0,0,0,0,8,4,0,0,8,0,3,0,0,7,7,0,0,0,0,0,0,0,6,0,0,0,0,6,0,8,0,0,0,0,2,8,0,0,0,0,4,1,9,0,0,5,0,0,0,0,8,0,0,7,6]
把一两个数字弄错了--这对我来说没什么问题,因为我可以在图像检测之后进行手动检查,我知道模型不会完美,但从数字不是8中读取的8的数字是可疑的,我觉得它可能来自于稍微偏离中心的数字。
因此,问题是:我是否可以使用python中的库来检测单元格中的数字,而不是用圆形(x*(宽度)/9+3)和丑陋的数学方法来检测单元格中的数字-- OR是我面临的问题,也是解决方案,完全不同?
发布于 2022-11-20 22:02:07
https://stackoverflow.com/questions/74512039
复制相似问题