-
Notifications
You must be signed in to change notification settings - Fork 1
/
mazePuzzleSolver.py
140 lines (101 loc) · 4.37 KB
/
mazePuzzleSolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import streamlit as st
import os
import cv2
import numpy as np
import time
from PIL import Image
def solve(ImgCV, kSize = 21):
sleepTime = 0.5
img = ImgCV.copy()
# meankSize, minkSize, modekSize = findKernel(img)
# print(meankSize, minkSize, modekSize)
# # kSize = 21
ret, binaryImage = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY_INV)
imageLocation.image(binaryImage, use_column_width=True, caption="Binary Image")
time.sleep(sleepTime)
print(" While binary ",binaryImage.dtype)
contours, hierarchy = cv2.findContours(binaryImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
path = np.zeros(binaryImage.shape, np.uint8)
cv2.drawContours(path, contours, 0, (255,255,255), cv2.FILLED)
imageLocation.image(path, use_column_width=True, caption='After contour')
time.sleep(sleepTime)
kernel = np.ones((kSize,kSize),np.uint8)
dilated = cv2.dilate(path,kernel,iterations = 1)
imageLocation.image(dilated, use_column_width=True, caption="Dilated")
time.sleep(sleepTime)
kernel = np.ones((int(kSize/2),int(kSize/2)),np.uint8)
erosion = cv2.erode(dilated,kernel,iterations = 1)
imageLocation.image(erosion, use_column_width=True, caption="Eroded")
time.sleep(sleepTime)
diff = cv2.absdiff(erosion, dilated)
img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
img[diff==255] = (0,255,0)
imageLocation.image(img, use_column_width=True, caption="Solved Puzzle")
def insert_github_logo():
st.markdown(
"<br>"
'<div style="text-align: center;">'
'<a target="_blank" rel="noopener noreferrer" href="https://github.com/iamsashank09"> '
'<img src="https://image.flaticon.com/icons/png/128/1051/1051326.png" width=64>'
" </img>"
"</a> "
"<br>"
'<h> Built by Sashank Kakaraparty </h>'
"</div>",
unsafe_allow_html=True,
)
def hideMenuandFooter():
hide_menu_footer_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_footer_style, unsafe_allow_html=True)
def applyStyleCSS():
with open("style.css") as f:
st.markdown('<style>{}</style>'.format(f.read()), unsafe_allow_html=True)
# Background HTML scripts using Markdown
hideMenuandFooter()
applyStyleCSS()
# Path to folder containing images
folder = 'mazes/'
uploadOwn = False
imageDict = {}
for i in os.listdir(folder):
imageDict[str(i[:-4])] = "".join((folder, i))
st.markdown("<h1 style='text-align: center; color: black;'>Maze Puzzle Solver</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center; color: black;'>Solving Maze Puzzles from <mark>Images</mark> using Morphological Operations - Computer Vision.</p>", unsafe_allow_html=True)
uploadType = st.radio("Type of Maze: ", [ "Try out available Maze Images", "Upload Custom Maze image to Solve"])
if uploadType == "Upload Custom Maze image to Solve" :
uploadOwn = True
else:
uploadOwn = False
if not uploadOwn:
st.markdown("<h3 style='text-align: center; color: black;'>Select a maze image from the dropdown and click on solve:</h3>", unsafe_allow_html=True)
key = st.selectbox("Picture choices", list(imageDict.keys()), 0)
filenameDropDown = imageDict[key]
ImgCV = cv2.imread(filenameDropDown, 0)
fileName = filenameDropDown
if uploadOwn:
st.markdown(
'<a target="_blank" rel="noopener noreferrer" href="http://mazegenerator.net/Default.aspx">'
'<p>Generate and Download custom mazes from MazeGenerator.net in PNG format ONLY. [RECTANGULAR MAZES]</p>'
'</a>',
unsafe_allow_html=True)
filenameUpload = st.file_uploader("Upload Maze Image [PNG FORMAT]", type="png")
if filenameUpload is not None:
ImgPIL = Image.open(filenameUpload)
ImgCV = cv2.cvtColor(np.uint8(np.array(ImgPIL)), cv2.COLOR_BGR2GRAY)
fileName = filenameUpload
else:
ImgCV = None
btn = st.button("Solve the maze!")
# kSize = st.sidebar.slider(label = "Kernel Size", value = 21)
imageLocation = st.empty()
if ImgCV is not None:
imageLocation.image(ImgCV, use_column_width=True, caption="Original Maze")
if btn:
if ImgCV is not None:
solve(ImgCV)
insert_github_logo()