I need help adapting this script for inputting the numbers from a picture of sudoku and insert the starting numbers into a console script, it does not work good for recognizing ones and sevens???
Example of sudoku solver
In my version I want to make the board is all zeros and when you take a picture it will add the numbers than solve, this combines the two programs (sudoku solver) & (ocr text recognition)
```
board=[
[5,8,4,1,0,0,0,0,0],
[0,0,6,8,0,0,5,1,0],
[0,0,0,0,5,4,7,0,6],
[0,5,3,0,1,0,0,6,7],
[0,0,0,0,2,0,0,0,0],
[4,6,0,0,9,0,8,3,0],
[7,0,8,5,4,0,0,0,0],
[0,2,9,0,0,3,4,0,0],
[0,0,0,0,0,1,3,7,9]
]
def solve(bo):
find = find_empty(bo)
if not find:
return True
else:
row,col = find
for i in range(1,10):
if valid(bo,i,(row,col)):
bo[row][col] = i
if solve(bo):
return True
bo[row][col] = 0
return False
def valid(bo,num,pos):
#check row
for i in range(len(bo[0])):
if bo[pos[0]][i] == num and pos[1] != i:
return False
#check column
for i in range(len(bo[0])):
if bo[i][pos[1]] == num and pos[0] != i:
return False
#check quadrant
box_x = pos[1] // 3
box_y = pos[0] // 3
for i in range(box_y * 3, box_y * 3 + 3):
for j in range(box_x * 3, box_x * 3 + 3):
if bo[i][j] == num and (i,j) != pos:
return False
return True
def print_board(bo):
for i in range(len(bo)):
if i % 3 == 0 and i != 0:
print('------+-------+------')
for j in range(len(bo[0])):
if j % 3 == 0 and j != 0:
print('|',end=' ')
if j == 8:
print(bo[i][j])
else:
print(str(bo[i][j])+ ' ', end='')
def find_empty(bo):
for i in range(len(bo)):
for j in range(len(bo[0])):
if bo[i][j] == 0:
return (i,j) # row, col
return None
print_board(board)
solve(board)
print('=====================')
print_board(board)
language_preference = ['fi','en','se']
import photos, ui, dialogs
import io
from objc_util import *
load_framework('Vision')
VNRecognizeTextRequest = ObjCClass('VNRecognizeTextRequest')
VNImageRequestHandler = ObjCClass('VNImageRequestHandler')
def pil2ui(pil_image):
buffer = io.BytesIO()
pil_image.save(buffer, format='PNG')
return ui.Image.from_data(buffer.getvalue())
selection = dialogs.alert('Get pic', button1='Camera', button2='Photos')
ui_image = None
if selection == 1:
pil_image = photos.capture_image()
if pil_image is not None:
ui_image = pil2ui(pil_image)
elif selection == 2:
ui_image = photos.pick_asset().get_ui_image()
if ui_image is not None:
print('Recognizing...\n')
req = VNRecognizeTextRequest.alloc().init().autorelease()
req.setRecognitionLanguages_(language_preference)
handler = VNImageRequestHandler.alloc().initWithData_options_(ui_image.to_png(), None).autorelease()
success = handler.performRequests_error_([req], None)
if success:
for result in req.results():
print(result.text())
else:
print('Problem recognizing anything') ```