Skip to content

Commit

Permalink
significant speed improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
MulongXie committed Oct 30, 2020
1 parent 7651187 commit 8a6e88c
Show file tree
Hide file tree
Showing 14 changed files with 1,992 additions and 718 deletions.
112 changes: 19 additions & 93 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1,270 changes: 970 additions & 300 deletions data/output/compo.json

Large diffs are not rendered by default.

1,270 changes: 970 additions & 300 deletions data/output/ip/9.json

Large diffs are not rendered by default.

Binary file modified data/output/ip/result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified data/output/result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified detect_compo/lib_ip/__pycache__/block_division.cpython-35.pyc
Binary file not shown.
Binary file modified detect_compo/lib_ip/__pycache__/ip_detection.cpython-35.pyc
Binary file not shown.
Binary file modified detect_compo/lib_ip/__pycache__/ip_preprocessing.cpython-35.pyc
Binary file not shown.
16 changes: 7 additions & 9 deletions detect_compo/lib_ip/block_division.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,15 @@ def block_division(grey, org, grad_thresh,

# flood fill algorithm to get background (layout block)
mask_copy = mask.copy()
cv2.floodFill(grey, mask, (y,x), None, grad_thresh, grad_thresh, cv2.FLOODFILL_MASK_ONLY)
ff = cv2.floodFill(grey, mask, (y, x), None, grad_thresh, grad_thresh, cv2.FLOODFILL_MASK_ONLY)
# ignore small regions
if ff[0] < 500: continue
mask_copy = mask - mask_copy
region = np.nonzero(mask_copy[1:-1, 1:-1])
region = list(zip(region[0], region[1]))
region = np.reshape(cv2.findNonZero(mask_copy[1:-1, 1:-1]), (-1, 2))
region = [(p[1], p[0]) for p in region]

# ignore small regions
if len(region) < 500:
continue
block = Block(region, grey.shape)

draw.draw_region(region, broad_all)
# draw.draw_region(region, broad_all)
# if block.height < 40 and block.width < 40:
# continue
if block.height < 30:
Expand All @@ -100,7 +98,7 @@ def block_division(grey, org, grad_thresh,
# if block.height/row < min_block_height_ratio:
# continue
blocks.append(block)
draw.draw_region(region, broad)
# draw.draw_region(region, broad)
if show:
cv2.imshow('flood-fill all', broad_all)
cv2.imshow('block', broad)
Expand Down
10 changes: 4 additions & 6 deletions detect_compo/lib_ip/ip_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,13 @@ def component_detection(binary, min_obj_area,
# region = util.boundary_bfs_connected_area(binary, i, j, mask)

mask_copy = mask.copy()
cv2.floodFill(binary, mask, (j, i), None, 0, 0, cv2.FLOODFILL_MASK_ONLY)
ff = cv2.floodFill(binary, mask, (j, i), None, 0, 0, cv2.FLOODFILL_MASK_ONLY)
if ff[0] < min_obj_area: continue
mask_copy = mask - mask_copy
region = np.nonzero(mask_copy[1:-1, 1:-1])
region = list(zip(region[0], region[1]))
region = np.reshape(cv2.findNonZero(mask_copy[1:-1, 1:-1]), (-1, 2))
region = [(p[1], p[0]) for p in region]

# filter out some compos
# ignore small area
if len(region) < min_obj_area:
continue
component = Component(region, binary.shape)
# calculate the boundary of the connected area
# ignore small area
Expand Down
13 changes: 5 additions & 8 deletions detect_compo/lib_ip/ip_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ def resize_by_height(org):
def gray_to_gradient(img):
if len(img.shape) == 3:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
row, column = img.shape[0], img.shape[1]
img_f = np.copy(img)
img_f = img_f.astype("float")

gradient = np.zeros((row, column))
for x in range(row - 1):
for y in range(column - 1):
gx = abs(img_f[x + 1, y] - img_f[x, y])
gy = abs(img_f[x, y + 1] - img_f[x, y])
gradient[x, y] = gx + gy
gradient = gradient.astype("uint8")
kernel_h = np.array([[0,0,0], [0,-1.,1.], [0,0,0]])
kernel_v = np.array([[0,0,0], [0,-1.,0], [0,1.,0]])
dst1 = abs(cv2.filter2D(img_f, -1, kernel_h))
dst2 = abs(cv2.filter2D(img_f, -1, kernel_v))
gradient = (dst1 + dst2).astype('uint8')
return gradient


Expand Down
5 changes: 4 additions & 1 deletion log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
- Synchronized with Webapp.

7/10/2020
- Extract parameters as configurable
- Extract parameters as configurable

30/10/2020
- Speed optimization (500% boost)
2 changes: 1 addition & 1 deletion run_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def resize_height_by_longest_edge(img_path, resize_length=800):
classifier['Elements'] = CNN('Elements')
# classifier['Noise'] = CNN('Noise')
ip.compo_detection(input_path_img, output_root, key_params,
classifier=classifier, resize_by_height=resized_height, show=True)
classifier=classifier, resize_by_height=resized_height, show=False)

if is_merge:
import merge
Expand Down
12 changes: 12 additions & 0 deletions speed-improvement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Optimization:
1. ip_preprocessing.py / gray_to_gradient : 0.5s -> 0.02s

2. ip_draw.py / draw_bounding_box : if not show and write_path is None: return : 0.005s -> 0s

3. ip_detection.py / component_detection : if ff[0] < min_obj_area: continue : 2.5s -> 0.3s

4. ip_detection.py / component_detection : cv2.findNonZero : 0.65s -> 0.33s

5. block_division.py / block_division : if ff[0] < 500 : continue: 1.97s -> 1s

6. block_division.py / block_division : Turn off draw : 1s -> 0.65s

0 comments on commit 8a6e88c

Please sign in to comment.