-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageSeparator.py
188 lines (161 loc) · 6.78 KB
/
imageSeparator.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import json
import os
from fnmatch import fnmatch
import numpy as np
import cv2
import math
path = "/home/n1m4/Desktop/tasks/OpenCV"
pathArray = []
counter = 2
isBackOn = False
subDirArray = []
counterNextDir = 0
def coord(x,y):
"Convert world coordinates to pixel coordinates."
return int(450+170*x), int(300-170*y)
# getting the name of dirs
current_directory = path
for dir in os.listdir(current_directory):
if os.path.isdir(os.path.join(current_directory, dir)):
subDirArray.append("/" + dir)
def getData(root, shouldShowField):
global subDirArray
print(subDirArray)
for path, subdirs ,files in os.walk(root):
for name in files:
if fnmatch(name, "*.json"):
pathJson = os.path.join(path, name)
jsonSize = os.path.getsize(pathJson)
if jsonSize > 0 and jsonSize < 300:
fh = open(pathJson)
data = json.load(fh)
detected = data["Ball"]["detected"]
with open(pathJson) as json_file:
data = json.load(json_file)
x_corBall = int(data["Ball"]["x"])
y_corBall = int(data["Ball"]["y"])
radius = int(data["Ball"]["r"])
x_corRobotPose = float(data["RobotPose"]["x"])
y_corRobotPose = float(data["RobotPose"]["y"])
angleRobotPose = float(data["RobotPose"]["angle"])
jpgFiles = (jsonToJPG(os.path.join(path, name)))
pathArray.append(jpgFiles)
if isBackOn is False:
showImage(jpgFiles,destPath, x_corBall, y_corBall, radius, detected)
if shouldShowField is True:
drawField(x_corRobotPose,y_corRobotPose,angleRobotPose)
# # DEBUG
# print(path + name + " exists")
# print(x_corBall, y_corBall, radius)
# print(x_corRobotPose, y_corRobotPose, angleRobotPose)
# print(jsonToJPG(os.path.join(path, name)))
# jsonSize = os.path.getsize(pathJson)
# print("Size of json is : " + str(jsonSize))
# print("Size of image is : " + str(FileSize))
# fileExistsBool = os.path.isfile(jsonToJPG(os.path.join(path, name)))
# FileSize = os.path.getsize(jsonToJPG(os.path.join(path, name)))
# # DEBUG
def drawField(x_corRobotPose,y_corRobotPose,angleRobotPose):
greenField = np.full((600, 900, 3), 127, np.uint8)
greenField[:] = (5, 144, 51)
width = 900
height = 600
center_coordinates = (900 - (width/2),600 - (height/2))
color = (0, 0, 0)
thickness = 2
cv2.circle(greenField, center_coordinates, (150/2), color, thickness)
cv2.line(greenField, (450,0), (450,600), color, thickness)
x_corRobotPose = math.sin(x_corRobotPose)
y_corRobotPose = math.cos(y_corRobotPose)
angleInDegree = math.cos(math.radians(angleRobotPose))
angleXSin = math.sin(angleRobotPose)
angleXCos = math.cos(angleRobotPose)
cv2.line(greenField, coord(x_corRobotPose,y_corRobotPose), coord(angleXCos,angleXSin), color, thickness)
cv2.circle(greenField, coord(angleXCos,angleXSin), 10, color, -1)
cv2.rectangle(greenField,(0,0),(900,600),(0,0,0),15)
cv2.imshow('Field', greenField)
cv2.waitKey(0)
cv2.destroyWindow('Field')
def jsonToJPG(path):
path = path.strip('.json')
path = path + ".jpg"
return path
def writeToFile(path):
file1 = open(destPath,"a")
print("ADDDDDDDD")
file1.write(path + "\n")
file1.close()
def showImage(path, destPath, x_cor, y_cor, radius, ballDetected):
global counter
global current_directory
global counterNextDir
global isBackOn
cv2.namedWindow(path,cv2.WINDOW_AUTOSIZE)
img = cv2.imread(path)
if ballDetected=="1":
center_coordinates = (x_cor*2, y_cor*2)
color = (0, 0, 255)
thickness = 2
cv2.circle(img, center_coordinates, radius*2, color, thickness)
cv2.imshow(path,img)
key = cv2.waitKey(0)
if key == 27:
cv2.destroyAllWindows() # It's escape button to exit
exit()
if key == 102 or key == 70: # It's F button to save
print(path + " added succressfully")
writeToFile(path)
cv2.destroyAllWindows()
if key == 106 or key == 74: # It's J button to skip
print(path + " dismissed")
cv2.destroyAllWindows()
if key == 39 or key == 83: # It's Right Arrow button to next dir
try:
print(counter)
cv2.destroyAllWindows()
print(current_directory + subDirArray[counterNextDir] + " NEXT DIR")
counterNextDir += 1
cv2.destroyAllWindows()
getData(current_directory + subDirArray[counterNextDir], shouldShowFieldState)
except IndexError:
counterNextDir = 0
if key == 37 or key == 81: # It's Left Arrow button to previus dir
try:
print(counter)
cv2.destroyAllWindows()
print(current_directory + subDirArray[counterNextDir] + " PREVIUS DIR")
counterNextDir -= 1
cv2.destroyAllWindows()
getData(current_directory + subDirArray[counterNextDir], shouldShowFieldState)
except IndexError:
counterNextDir = 0
print(counterNextDir)
pass
if key == 98 or key == 66: # It's B button to back
isBackOn = True
counter = 2
while isBackOn:
lastIndex = pathArray[-(counter)]
cv2.namedWindow('back '+lastIndex,cv2.WINDOW_AUTOSIZE)
img = cv2.imread(lastIndex)
cv2.imshow('back '+lastIndex,img)
key = cv2.waitKey(0)
counter += 1
if key == 27:
cv2.destroyAllWindows() # It's escape button to exit
exit()
if key == 102 or key == 70: # It's F button to save
print(path + " added succressfully")
writeToFile(lastIndex)
cv2.destroyWindow('back'+lastIndex)
print(lastIndex)
if key == 106 or key == 74: # It's J button to skip
print(path + " dismissed")
isBackOn = False
cv2.destroyAllWindows()
# print("Enter the directory: ")
# p = raw_input()
destPath = (path + "/file.txt")
print("PATH FOR SAVING IS " + path)
shouldShowFieldState = False
getData(path, shouldShowFieldState)