Spaces:
Sleeping
Sleeping
add check
Browse files
app.py
CHANGED
|
@@ -24,49 +24,93 @@ def get_crop(img, pos_x, pos_y, crop_size=0.1):
|
|
| 24 |
@interactive(seed=(45, [0, 100], "Puzzle seed"))
|
| 25 |
def generate_random_puzzle(seed: int = 45, context: dict = {}):
|
| 26 |
np.random.seed(seed)
|
| 27 |
-
pos_x, pos_y
|
| 28 |
-
context["
|
| 29 |
context["puzzle_flip_mirror"] = np.random.choice([True, False], 2)
|
| 30 |
|
| 31 |
|
| 32 |
-
def create_puzzle(img, context: dict = {}):
|
| 33 |
out = img.copy()
|
| 34 |
-
x_gt, y_gt
|
| 35 |
flip_gt, mirror_gt = context["puzzle_flip_mirror"]
|
| 36 |
cs_x, cs_y = get_crop(img, x_gt, y_gt)
|
| 37 |
crop = img[cs_y[0]:cs_y[1], cs_x[0]:cs_x[1], ...]
|
| 38 |
-
out[cs_y[0]:cs_y[1], cs_x[0]:cs_x[1]] = intensity*
|
| 39 |
crop = flip_image(crop, flip=flip_gt, mirror=mirror_gt)
|
| 40 |
return out, crop
|
| 41 |
|
| 42 |
|
| 43 |
@interactive(
|
| 44 |
-
flip=(
|
| 45 |
-
mirror=(
|
| 46 |
)
|
| 47 |
-
def flip_mirror_piece(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return flip_image(piece.copy(), flip=flip, mirror=mirror)
|
| 49 |
|
| 50 |
|
| 51 |
@interactive(
|
| 52 |
-
pos_x=(0.5, [0.1, 0.9], "Position X"),
|
| 53 |
-
pos_y=(0.5, [0.1, 0.9], "Position Y"),
|
| 54 |
)
|
| 55 |
-
def place_puzzle(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
out = puzzle.copy()
|
|
|
|
| 57 |
cp_x, cp_y = get_crop(img, pos_x, pos_y)
|
| 58 |
out[cp_y[0]:cp_y[1], cp_x[0]:cp_x[1]] = piece
|
| 59 |
return out
|
| 60 |
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# pipeline definition
|
| 63 |
# -------------------
|
|
|
|
|
|
|
| 64 |
def captcha_pipe(inp):
|
| 65 |
generate_random_puzzle()
|
| 66 |
puzzle, puzzle_piece = create_puzzle(inp)
|
| 67 |
puzzle_piece = flip_mirror_piece(puzzle_piece)
|
| 68 |
puzzle = place_puzzle(puzzle, puzzle_piece)
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
|
|
|
| 24 |
@interactive(seed=(45, [0, 100], "Puzzle seed"))
|
| 25 |
def generate_random_puzzle(seed: int = 45, context: dict = {}):
|
| 26 |
np.random.seed(seed)
|
| 27 |
+
pos_x, pos_y = np.random.uniform(0.2, 0.8, 2)
|
| 28 |
+
context["puzzle_pos"] = (pos_x, pos_y)
|
| 29 |
context["puzzle_flip_mirror"] = np.random.choice([True, False], 2)
|
| 30 |
|
| 31 |
|
| 32 |
+
def create_puzzle(img, intensity: float = 0.4, context: dict = {}):
|
| 33 |
out = img.copy()
|
| 34 |
+
x_gt, y_gt = context["puzzle_pos"]
|
| 35 |
flip_gt, mirror_gt = context["puzzle_flip_mirror"]
|
| 36 |
cs_x, cs_y = get_crop(img, x_gt, y_gt)
|
| 37 |
crop = img[cs_y[0]:cs_y[1], cs_x[0]:cs_x[1], ...]
|
| 38 |
+
out[cs_y[0]:cs_y[1], cs_x[0]:cs_x[1]] = intensity*crop
|
| 39 |
crop = flip_image(crop, flip=flip_gt, mirror=mirror_gt)
|
| 40 |
return out, crop
|
| 41 |
|
| 42 |
|
| 43 |
@interactive(
|
| 44 |
+
flip=(False, "Flip Image"),
|
| 45 |
+
mirror=(False, "Mirror Image"),
|
| 46 |
)
|
| 47 |
+
def flip_mirror_piece(
|
| 48 |
+
piece: np.ndarray,
|
| 49 |
+
flip: bool = False,
|
| 50 |
+
mirror: bool = False,
|
| 51 |
+
context: dict = {}
|
| 52 |
+
) -> np.ndarray:
|
| 53 |
+
context["user_flip_mirror"] = (flip, mirror)
|
| 54 |
return flip_image(piece.copy(), flip=flip, mirror=mirror)
|
| 55 |
|
| 56 |
|
| 57 |
@interactive(
|
| 58 |
+
pos_x=(0.5, [0.1, 0.9, 0.005], "Position X", ["left", "right"]),
|
| 59 |
+
pos_y=(0.5, [0.1, 0.9, 0.005], "Position Y", ["up", "down"]),
|
| 60 |
)
|
| 61 |
+
def place_puzzle(
|
| 62 |
+
puzzle: np.ndarray,
|
| 63 |
+
piece: np.ndarray,
|
| 64 |
+
pos_x: float = 0.5,
|
| 65 |
+
pos_y: float = 0.5,
|
| 66 |
+
context: dict = {}
|
| 67 |
+
) -> np.ndarray:
|
| 68 |
out = puzzle.copy()
|
| 69 |
+
context["user_pos"] = (pos_x, pos_y)
|
| 70 |
cp_x, cp_y = get_crop(img, pos_x, pos_y)
|
| 71 |
out[cp_y[0]:cp_y[1], cp_x[0]:cp_x[1]] = piece
|
| 72 |
return out
|
| 73 |
|
| 74 |
|
| 75 |
+
TOLERANCES = {"low": 0.01, "medium": 0.02, "high": 0.05}
|
| 76 |
+
TOLERANCE_LEVELS = list(TOLERANCES.keys())
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@interactive(
|
| 80 |
+
tolerance=(TOLERANCE_LEVELS[0], TOLERANCE_LEVELS, "Tolerance")
|
| 81 |
+
)
|
| 82 |
+
def check_puzzle(tolerance: str = "low", context: dict = {}):
|
| 83 |
+
x_gt, y_gt = context["puzzle_pos"]
|
| 84 |
+
flip_gt, mirror_gt = context["puzzle_flip_mirror"]
|
| 85 |
+
x, y = context["user_pos"]
|
| 86 |
+
flip, mirror = context["user_flip_mirror"]
|
| 87 |
+
check_pos = np.allclose([x_gt, y_gt], [x, y],
|
| 88 |
+
atol=TOLERANCES.get(tolerance, 0.01))
|
| 89 |
+
check_flip_mirror = (flip_gt == flip) and (mirror_gt == mirror)
|
| 90 |
+
success = check_pos and check_flip_mirror
|
| 91 |
+
context["success"] = success
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def show_feedback(context: dict = {}):
|
| 95 |
+
success = context["success"]
|
| 96 |
+
flat_array = np.ones((256, 256, 3))
|
| 97 |
+
if success:
|
| 98 |
+
return flat_array*np.array([0., 1., 0.])[None, None, :]
|
| 99 |
+
else:
|
| 100 |
+
return flat_array*np.array([1., 0., 0.])[None, None, :]
|
| 101 |
+
|
| 102 |
# pipeline definition
|
| 103 |
# -------------------
|
| 104 |
+
|
| 105 |
+
|
| 106 |
def captcha_pipe(inp):
|
| 107 |
generate_random_puzzle()
|
| 108 |
puzzle, puzzle_piece = create_puzzle(inp)
|
| 109 |
puzzle_piece = flip_mirror_piece(puzzle_piece)
|
| 110 |
puzzle = place_puzzle(puzzle, puzzle_piece)
|
| 111 |
+
check_puzzle()
|
| 112 |
+
validity = show_feedback()
|
| 113 |
+
return [puzzle_piece, puzzle, validity]
|
| 114 |
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|