├── frame.jpg ├── __pycache__ ├── grid.cpython-36.pyc └── pathsolver.cpython-36.pyc ├── grid.py ├── finder.py ├── pathsolver.py └── README.md /frame.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/satinder147/Maze-Solving-using-A-star-algorithm/HEAD/frame.jpg -------------------------------------------------------------------------------- /__pycache__/grid.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/satinder147/Maze-Solving-using-A-star-algorithm/HEAD/__pycache__/grid.cpython-36.pyc -------------------------------------------------------------------------------- /__pycache__/pathsolver.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/satinder147/Maze-Solving-using-A-star-algorithm/HEAD/__pycache__/pathsolver.cpython-36.pyc -------------------------------------------------------------------------------- /grid.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | class gridmaker: 5 | def __init__(self,s): 6 | self.s=s 7 | self.img=cv2.imread("frame.jpg",0) 8 | self.h,self.w=self.img.shape 9 | self.grid=np.zeros(shape=(26,32)) 10 | def iswhite(self,a,b,block): 11 | h,w=block.shape 12 | count=0 13 | for i in range(b,b+20): 14 | for j in range(a,a+20): 15 | if(i<480 and j<640): 16 | if(block[i][j]>0): 17 | count=count+1 18 | if(count>225): 19 | return True 20 | return False 21 | 22 | 23 | def returnGrid(self): 24 | for i in range(0,self.w,self.s): 25 | for j in range(0,self.h,self.s): 26 | if(self.iswhite(i,j,self.img)): 27 | self.grid[int(j/self.s)][int(i/self.s)]=1 28 | #cv2.rectangle(frame,(i,j),(i+self.s,j+self.s),(255,0,0),-1) 29 | #else: 30 | #cv2.rectangle(frame,(i,j),(i+self.s,j+self.s),(255,0,0),1) 31 | 32 | return self.grid 33 | -------------------------------------------------------------------------------- /finder.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from grid import gridmaker 4 | import matplotlib.pyplot as plt 5 | from pathsolver import solver 6 | obj=gridmaker(20) 7 | solve=solver() 8 | 9 | 10 | def line(route): 11 | xc=[] 12 | yc=[] 13 | for i in (range(0,len(route))): 14 | x=route[i][0] 15 | y=route[i][1] 16 | xc.append(x) 17 | yc.append(y) 18 | return xc,yc 19 | 20 | 21 | cap=cv2.VideoCapture(1) 22 | while 1: 23 | ret,frame=cap.read() 24 | frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) 25 | th,frame=cv2.threshold(frame,100,255,cv2.THRESH_BINARY) 26 | frame=cv2.bitwise_not(frame) 27 | frame=cv2.dilate(frame,None,iterations=5) 28 | cv2.imshow("frame",frame) 29 | if(cv2.waitKey(1) & 0XFF==ord('q')): 30 | cv2.imwrite("frame.jpg",frame) 31 | break 32 | cap.release() 33 | cv2.destroyAllWindows() 34 | 35 | def main(): 36 | global obj,solve 37 | fig,ax=plt.subplots() 38 | grid=obj.returnGrid() 39 | ax.imshow(grid,cmap=plt.cm.Spectral) 40 | plt.show() 41 | print("enter start point") 42 | s1=int(input()) 43 | s2=int(input()) 44 | start=(s1,s2) 45 | print("enter end point") 46 | s1=int(input()) 47 | s2=int(input()) 48 | end=(s1,s2) 49 | route=solve.astar(start,end,grid) 50 | if(route==False): 51 | print("No path") 52 | return 0 53 | route+=[start] 54 | route=route[::-1] 55 | 56 | xc,yc=line(route) 57 | fig,ax=plt.subplots() 58 | ax.imshow(grid,cmap=plt.cm.Spectral) 59 | ax.plot(yc,xc,color="black") 60 | ax.scatter(start[1],start[0]) 61 | ax.scatter(end[1],end[0]) 62 | plt.show() 63 | if(__name__=="__main__"): 64 | main() 65 | -------------------------------------------------------------------------------- /pathsolver.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import heapq 4 | 5 | class solver: 6 | def __init__(self): 7 | pass 8 | 9 | def hue(self,a,b): 10 | return np.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2) 11 | 12 | def astar(self,start,end,grid): 13 | neighbours=[(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)] 14 | heap=[] 15 | closed_list=set() 16 | gscore={start:0} 17 | fscore={start:self.hue(start,end)} 18 | parent={} 19 | heapq.heappush(heap,(fscore[start],start)) 20 | while heap: 21 | current=heapq.heappop(heap)[1] 22 | closed_list.add(current) 23 | if(current==end): 24 | data=[] 25 | while current in parent: 26 | data.append(current) 27 | current=parent[current] 28 | return data 29 | 30 | for i,j in neighbours: 31 | neighbour=current[0]+i,current[1]+j 32 | tgscore=gscore[current]+self.hue(current,neighbour) 33 | if 0<=neighbour[0]=gscore.get(neighbour,0): 42 | continue 43 | if tgscore 15 |
16 | ## 2) Detect the maze using image thresholding 17 | ![Screenshot from 2019-03-22 02-23-11](https://user-images.githubusercontent.com/24778913/54785762-07e2af00-4c4d-11e9-8472-08152d4930b1.png) 18 | ## 3) Convert the image into a smaller grid (because the image size is very big) 19 | ![Screenshot from 2019-03-22 02-28-13](https://user-images.githubusercontent.com/24778913/54787244-22b72280-4c51-11e9-83b4-04e8021a5ee3.png) 20 | ## 4) Take the start and the end point from the user 21 | ![Screenshot from 2019-03-22 02-28-53](https://user-images.githubusercontent.com/24778913/54785826-33659980-4c4d-11e9-9c9f-c7e87fa48fb7.png) 22 | ## 5) Solve the grid using A-star algorithm 23 | I have provided some sources from where I studied the algorithm in the references section.
24 | ![Screenshot from 2019-03-22 02-29-04](https://user-images.githubusercontent.com/24778913/54785841-3f515b80-4c4d-11e9-87f1-57d7b7badad7.png) 25 | 26 | # Things to improve 27 | 1) The process of converting a high resolution image into a grid is not accurate and is also not very efficient.
28 | 2) Plotting the result on the live camera feed.
29 | 30 | 31 | # References 32 | 1)Computerphile
33 | 2)CodingTrain
34 | 3)Medium
35 | 4)raywenderlich
36 | 5)Analytics link 37 | --------------------------------------------------------------------------------