def placeSudokuDigits(img_PT): global img #we start looking at the middle of the cell as this is where the sudoku digit should be at img_PT = cv2.resize( img_PT, (252, 252)) #had to reshape the image size to fit the model shape img_color = cv2.resize(img, (252, 252)) cells = getCellPositions(img_PT) n = 9 cr = [cells[i:i + n] for i in range(0, len(cells), n)] #cr meaning cells reshaped digits = extractSudokuDigits(img_PT) solve(digits) #have to look for a way not use this inside this function for i in range(len(cr)): for j in range(len(cr[i])): pos = detectEmptyCell(cr[i][j], img_PT) digit_text = digits[i][j] if pos == []: cv2.putText(img_color, str(digit_text), ((cr[i][j][0] + 8), (cr[i][j][2] + 19)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2, cv2.LINE_AA) else: continue cv2.imshow('puzzle', img_color) cv2.waitKey()
def placeSudokuDigitsLive(img_PT): #we start looking at the middle of the cell as this is where the sudoku digit should be at img_PT = cv2.resize(img_PT,(252,252)) #had to reshape the image size to fit the model shape img_color = cv2.resize(frame,(252,252)) #img is got from the grayscale cells = getCellPositions(img_PT) n = 9 cr = [cells[i:i+n] for i in range(0, len(cells), n)] #cr meaning cells reshaped digits = extractSudokuDigits(img_PT) solve(digits) for i in range(len(cr)): for j in range(len(cr[i])): pos = detectEmptyCell(cr[i][j],img_PT) digit_text = digits[i][j] if pos == []: cv2.putText(img_color, str(digit_text), ((cr[i][j][0]+8),(cr[i][j][2]+19)),cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 0, 255), 2, cv2.LINE_AA) else: continue
def manually_solve(puzzle): """Compilation of techniques to run until puzzle is complete""" added_on = True count = 0 puzzle = puzzle.tolist() while added_on: orig_puzzle = np.copy(np_puzzle(puzzle)) puzzle = sole_candidate(puzzle) puzzle = unique_candidate(puzzle) puzzle = naked_subset(puzzle) puzzle = naked_singles(puzzle) puzzle = final_sweep(puzzle) if np.array_equal(orig_puzzle, np_puzzle(puzzle)): added_on = False count += 1 #applies backtracking algorithm to unsolved puzzles unsolved = np_puzzle(puzzle) if (not (check_completion(unsolved) == 0)): solve(unsolved, len(puzzle)) return unsolved #returns solved puzzle return np_puzzle(puzzle)
def gen_schedule(data): try: # Preprocess the data professors, courses, groups, config = generate_ds(data) # Initialize the closure function schedule_session = solve(professors, courses, groups, config) # Try to solve and return answer solution = schedule_session(0, 0, groups[0]['hour_range'][0]) except Exception as err: print('Something has failed inside the scheduling algorithm') # print(err) # traceback.print_exc() return None try: if solution: return format_solution(courses, groups) else: return None except Exception as err: print('Solution found, but reformatting failed') return None
y = int((i * side) + (side / 2)) + correction cv2.putText(check, str(sudoku[i][j]), (x, y), cv2.FONT_HERSHEY_DUPLEX, 1.5, (0, 0, 0), 4) print() cv2.imshow('check', check) cv2.waitKey(0) goto(2) if line == 2: ans = input( 'Todos os digitos da imagem estão corretos? (Y para sim / N para não)' ) if ans in ['y', 'Y', 'sim', 'Sim', 'SIM']: solved = np.copy(sudoku) if (backtracking.solve(solved)): for m in range(9): for n in range(9): if sudoku[m][n] == 0: xx = int((n * sidex) + (sidex / 2)) - correctionx yy = int((m * sidey) + (sidey / 2)) + correctiony cv2.putText(warp, str(solved[m][n]), (xx, yy), cv2.FONT_HERSHEY_DUPLEX, 1.5, (255, 255, 0), 3) final = preprocessing.print_final(dst, corners, warp, img0) cv2.imshow('final', final) cv2.waitKey(0) break else: print("This game has no solution!") break
#Applying Probabilistic Hough Transform on the Binary Image minLineLength = 100 maxLineGap = 60 lines = cv2.HoughLinesP(thresh_inv,1,np.pi/180,100,minLineLength=100,maxLineGap=10) for l in lines: x1,y1,x2,y2 = l[0] cv2.line(board_segment,(x1,y1),(x2,y2),(0,255,0),2, cv2.LINE_AA) if flag: #using neural network model to detect the digits in the image #new_model = load_model('keras_digit_model.h5') a = extractSudokuDigits(thresh_inv) #solving with backtracking solve(a) #putting back the solved digits on spaces that are empty placeSudokuDigitsLive(thresh_inv) #this function won't have the plt.imshow() and also, #the colored image would be img flag = False #overlaying the board segment of the image on the frame x_offset, y_offset = (poly_approx[0][0].tolist()[0]),(poly_approx[0][0].tolist()[1]) x_end, y_end = (x_offset+board_segment.shape[1]), (y_offset+board_segment.shape[0]) frame[y_offset:y_end,x_offset:x_end] = board_segment # Display the resulting frame cv2.imshow('frame',frame)
def back_time(puzzle): start = time.time() solve(puzzle, len(puzzle)) end = time.time() return (end - start)