-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
146 lines (106 loc) · 3.15 KB
/
main.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
141
142
143
144
145
146
import gym
import pix_sample_arena
import time
import pybullet as p
import pybullet_data
import cv2
import os
import numpy as np
import math
from collections import defaultdict
from numpy import inf
env = gym.make("pix_sample_arena-v0")
time.sleep(3)
img = env.camera_feed()
img = img[100:412,100:412]
img = cv2.resize(img, (360,360))
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
colorBGR = np.array([[227,227,227],[0,227,0],[0,227,227],[0,0,145],[211,114,211],[227,227,0]]) #0.White , 1.Green , 2.Yellow ,3.Red,4.Pink (Patient) ,4.White
mat = np.zeros((6,6)) # 5.Blue
for i in range(6):
mask=cv2.inRange(img, colorBGR[i], colorBGR[i])
Gmask = cv2.bitwise_and(img, img, mask = mask)
cv2.imshow("test", Gmask)
contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
# print('{} {}'.format(cx, cy))
x = math.ceil(cx/60) - 1
y = math.ceil(cy/60) - 1
if(cv2.contourArea(cnt)>2000.0):
if(i==5):
mat[y][x] = 1
else :
mat[y][x] = i+1
if(i==4):
for cnt in contours:
print(cv2.contourArea(cnt))
print(mat)
print(len(contours))
graph = defaultdict(dict)
costs = {}
for i in range(6):
for j in range(6):
if j<5:
if mat[i][j+1] > 0:
n=str(i)+ str(j)
m=str(i)+ str(j+1)
item = {m : mat[i][j+1]}
graph[n].update(item)
if j>0:
if mat[i][j-1] > 0:
n=str(i)+ str(j)
m=str(i)+ str(j-1)
item = {m : mat[i][j-1]}
graph[n].update(item)
if i<5:
if mat[i+1][j] > 0:
n=str(i)+ str(j)
m=str(i+1)+ str(j)
item = {m : mat[i+1][j]}
graph[n].update(item)
if i>0:
if mat[i-1][j] > 0:
n=str(i)+ str(j)
m=str(i-1)+ str(j)
item = {m : mat[i-1][j]}
graph[n].update(item)
print(graph)
source = '00'
target = '54'
for nodes in graph:
costs[nodes] = inf
costs[source] = 0
print(costs)
parents = {}
def search(source, target, graph, costs, parents):
nextNode = source
while nextNode != target:
for neighbor in graph[nextNode]:
if graph[nextNode][neighbor] + costs[nextNode] < costs[neighbor]:
costs[neighbor] = graph[nextNode][neighbor] + costs[nextNode]
parents[neighbor] = nextNode
del graph[neighbor][nextNode]
del costs[nextNode]
nextNode = min(costs, key=costs.get)
return parents
result = search(source, target, graph, costs, parents)
def backpedal(source, target, searchResult):
node = target
backpath = [target]
path = []
while node != source:
backpath.append(searchResult[node])
node = searchResult[node]
for i in range(len(backpath)):
path.append(backpath[-i - 1])
return path
print('parent dictionary={}'.format(result))
path = backpedal(source, target, result)
print('shortest path={}'.format(path))
shortest_path = []
for i in range(len(path)):
shortest_path.append([int(path[i][0]),int(path[i][1])])
print(shortest_path)