Spaces:
Sleeping
Sleeping
import streamlit as st | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
import os, random | |
import cv2 | |
from tensorflow.keras.models import load_model | |
from PIL import Image | |
def main(): | |
st.title('Sudoku Solver') | |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
# Importing puzzle to be solved | |
puzzle = cv2.imread(uploaded_file) | |
# Resizing puzzle to be solved | |
puzzle = cv2.resize(puzzle, (450,450)) | |
# Preprocessing Puzzle | |
su_puzzle = preprocess(puzzle) | |
# Finding the outline of the sudoku puzzle in the image | |
su_contour_1= su_puzzle.copy() | |
su_contour_2= sudoku_a.copy() | |
su_contour, hierarchy = cv2.findContours(su_puzzle,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) | |
cv2.drawContours(su_contour_1, su_contour,-1,(0,255,0),3) | |
black_img = np.zeros((450,450,3), np.uint8) | |
su_biggest, su_maxArea = main_outline(su_contour) | |
if su_biggest.size != 0: | |
su_biggest = reframe(su_biggest) | |
cv2.drawContours(su_contour_2,su_biggest,-1, (0,255,0),10) | |
su_pts1 = np.float32(su_biggest) | |
su_pts2 = np.float32([[0,0],[450,0],[0,450],[450,450]]) | |
su_matrix = cv2.getPerspectiveTransform(su_pts1,su_pts2) | |
su_imagewrap = cv2.warpPerspective(puzzle,su_matrix,(450,450)) | |
su_imagewrap =cv2.cvtColor(su_imagewrap, cv2.COLOR_BGR2GRAY) | |
sudoku_cell = splitcells(su_imagewrap) | |
sudoku_cell_croped= CropCell(sudoku_cell) | |
grid = read_cells(sudoku_cell_croped, model) | |
grid = np.asarray(grid) | |
grid = np.reshape(grid,(9,9)) | |
solve(grid) | |
# Display the solution or appropriate message | |
if solve(grid): | |
st.header("Sudoku Solved:") | |
st.write(Solved(grid)) | |
else: | |
st.write("No solution found.") | |
if __name__ == '__main__': | |
main() | |