MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / calculate_square

Function calculate_square

anyword_data.py:42–76  ·  view source on GitHub ↗
(full_image, polygon)

Source from the content-addressed store, hash-verified

40
41
42def calculate_square(full_image, polygon):
43 gray = cv2.cvtColor(full_image, cv2.COLOR_RGB2GRAY)
44 # Find non-white areas (i.e., pixel values less than 255)
45 coords = cv2.findNonZero(255 - gray)
46 x_l, y_t, w, h = cv2.boundingRect(coords)
47 x_r = x_l + w
48 y_b = y_t + h
49
50 x0, y0, x1, y1 = int(min([x[0] for x in polygon])), int(min([x[1] for x in polygon])), int(max([x[0] for x in polygon])), int(max([x[1] for x in polygon]))
51 x0 = max(x_l, min(x0, x_r))
52 y0 = max(y_t, min(y0, y_b))
53 x1 = max(x_l, min(x1, x_r))
54 y1 = max(y_t, min(y1, y_b))
55
56 width = x1 - x0
57 height = y1 - y0
58 L = max(width, height)
59
60 if w < L:
61 sx0, sx1 = x_l, x_r
62 else:
63 sx0_min = max(x_l, x1 - L)
64 sx0_max = min(x_r - L, x0)
65 sx0 = random.randint(sx0_min, sx0_max+1)
66 sx1 = sx0 + L
67
68 if h < L:
69 sy0, sy1 = y_t, y_b
70 else:
71 sy0_min = max(y_t, y1 - L)
72 sy0_max = min(y_b - L, y0)
73 sy0 = random.randint(sy0_min, sy0_max+1)
74 sy1 = sy0 + L
75
76 return [sx0, sy0, sx1, sy1]
77
78
79class AnyWordDataset(Dataset):

Callers 1

__getitem__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected