Niv Sardi commited on
Commit
90c5fac
1 Parent(s): d4ab848

rework imtool

Browse files

Signed-off-by: Niv Sardi <xaiki@evilgiggle.com>

Files changed (1) hide show
  1. crawler/imtool.py +37 -20
crawler/imtool.py CHANGED
@@ -48,8 +48,8 @@ def read_bounding_boxes(filename):
48
  boxes.append(BoundingBox(x,y,w,h))
49
  return boxes
50
 
51
- def floor_point(a, b):
52
- return (math.floor(a), math.floor(b))
53
 
54
  def cut_img(im, s, e):
55
  x = s[0]
@@ -75,30 +75,38 @@ def crop(fn, logos):
75
 
76
  (h, w, c) = im.shape
77
  (tx, ty)= (
78
- math.floor(w/(TILE_SIZE*TILE_OVERLAP)),
79
- math.floor(h/(TILE_SIZE*TILE_OVERLAP))
80
  )
81
 
82
- print('shape', basename, tx, ty, h, w, logos)
83
  for x in range(tx):
84
  for y in range(ty):
85
  color = (0,x*(255/tx),y*(255/ty))
86
 
87
- fx = math.floor(x*(w - TILE_SIZE)/(tx))
88
- fy = math.floor(y*(h - TILE_SIZE)/(ty))
 
 
 
 
 
89
 
90
- start = (fx, fy)
91
- end = (fx + TILE_SIZE, fy + TILE_SIZE)
92
 
93
- #im = cv2.rectangle(im, start, end, color, 10)
 
94
  li = []
95
  for l in logos:
96
  def intersect():
97
- six = l.x - fx
98
- siy = l.y - fy
99
  eix = six + l.w
100
  eiy = siy + l.h
101
 
 
 
102
  if six < 0:
103
  if six + l.w < 0:
104
  return None
@@ -107,14 +115,14 @@ def crop(fn, logos):
107
  if siy + l.h < 0:
108
  return None
109
  siy = 0
110
- if eix > TILE_SIZE:
111
- if eix - l.w > TILE_SIZE:
112
  return None
113
- eix = TILE_SIZE
114
- if eiy > TILE_SIZE:
115
- if eiy - l.h > TILE_SIZE:
116
  return None
117
- eiy = TILE_SIZE
118
 
119
  return BoundingBox(six, siy, eix - six, eiy - siy)
120
 
@@ -123,20 +131,29 @@ def crop(fn, logos):
123
  li.append(p)
124
 
125
  c = (255, 0, 0)
126
- nim = im[fy:fy+TILE_SIZE, fx:fx+TILE_SIZE]
127
- img_name =f"{img_out}/{basename}-x{x}y{y}.png"
 
 
128
  txt_name =f"{txt_out}/{basename}-x{x}y{y}.txt"
129
 
130
  cv2.imwrite(img_name, nim)
131
  if len(li):
132
  with open(txt_name, 'w') as f:
133
  for p in li:
 
 
 
 
 
 
134
  cx = p.w/2 + p.x
135
  cy = p.h/2 + p.y
136
 
137
  a = f"{basename} {cx/TILE_SIZE} {cy/TILE_SIZE} {p.w/TILE_SIZE} {p.h/TILE_SIZE}"
138
  f.write(a)
139
  print(a)
 
140
 
141
  if __name__ == '__main__':
142
  with os.scandir('./data/') as it:
 
48
  boxes.append(BoundingBox(x,y,w,h))
49
  return boxes
50
 
51
+ def floor_point(x, y):
52
+ return (math.floor(x), math.floor(y))
53
 
54
  def cut_img(im, s, e):
55
  x = s[0]
 
75
 
76
  (h, w, c) = im.shape
77
  (tx, ty)= (
78
+ math.ceil(w/(TILE_SIZE*TILE_OVERLAP)),
79
+ math.ceil(h/(TILE_SIZE*TILE_OVERLAP))
80
  )
81
 
82
+ print('shape', basename, tx, ty, w, h, logos)
83
  for x in range(tx):
84
  for y in range(ty):
85
  color = (0,x*(255/tx),y*(255/ty))
86
 
87
+ (tw, th) = (min(w, TILE_SIZE), min(h, TILE_SIZE))
88
+ f = BoundingBox(
89
+ (w - tw)*x/(tx),
90
+ (h - th)*y/(ty),
91
+ tw,
92
+ th
93
+ )
94
 
95
+ start = floor_point(f.x, f.y)
96
+ end = floor_point(f.x + f.w, f.y + f.h)
97
 
98
+ print(x, y, start, end, logos)
99
+ im = cv2.rectangle(im, start, end, color, 10)
100
  li = []
101
  for l in logos:
102
  def intersect():
103
+ six = l.x - f.x
104
+ siy = l.y - f.y
105
  eix = six + l.w
106
  eiy = siy + l.h
107
 
108
+ print('intersect', (six, siy), (eix, eiy), f, l)
109
+
110
  if six < 0:
111
  if six + l.w < 0:
112
  return None
 
115
  if siy + l.h < 0:
116
  return None
117
  siy = 0
118
+ if eix > tw:
119
+ if eix - l.w > tw:
120
  return None
121
+ eix = tw
122
+ if eiy > th:
123
+ if eiy - l.h > th:
124
  return None
125
+ eiy = th
126
 
127
  return BoundingBox(six, siy, eix - six, eiy - siy)
128
 
 
131
  li.append(p)
132
 
133
  c = (255, 0, 0)
134
+
135
+ print(start, end)
136
+ nim = im[start[1]:end[1], start[0]:end[0]]
137
+ img_name =f"{img_out}/{basename}-x{x}y{y}.jpg"
138
  txt_name =f"{txt_out}/{basename}-x{x}y{y}.txt"
139
 
140
  cv2.imwrite(img_name, nim)
141
  if len(li):
142
  with open(txt_name, 'w') as f:
143
  for p in li:
144
+ print(p)
145
+ im = cv2.rectangle(im,
146
+ floor_point(p.x, p.y),
147
+ floor_point(p.x + p.w, p.y + p.h),
148
+ c,
149
+ 5)
150
  cx = p.w/2 + p.x
151
  cy = p.h/2 + p.y
152
 
153
  a = f"{basename} {cx/TILE_SIZE} {cy/TILE_SIZE} {p.w/TILE_SIZE} {p.h/TILE_SIZE}"
154
  f.write(a)
155
  print(a)
156
+ cv2.imwrite(f'{basename}.debug.png', im)
157
 
158
  if __name__ == '__main__':
159
  with os.scandir('./data/') as it: