Spaces:
Sleeping
Sleeping
initiate captcha example
Browse files- README.md +2 -2
- app.py +86 -0
- requirements.txt +1 -0
- sample.jpg +0 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
-
title: Interactive Pipe Captcha
|
3 |
-
emoji:
|
4 |
colorFrom: indigo
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
+
title: Interactive Pipe Captcha Demo
|
3 |
+
emoji: π
|
4 |
colorFrom: indigo
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from interactive_pipe import interactive_pipeline, interactive, Image
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
# Helper functions
|
5 |
+
# ----------------
|
6 |
+
|
7 |
+
|
8 |
+
def flip_image(img, flip=True, mirror=True):
|
9 |
+
img = img[::-1] if flip else img
|
10 |
+
img = img[:, ::-1] if mirror else img
|
11 |
+
return img
|
12 |
+
|
13 |
+
|
14 |
+
def get_crop(img, pos_x, pos_y, crop_size=0.1):
|
15 |
+
c_size = int(crop_size * img.shape[0])
|
16 |
+
crop_x = (int(pos_x * img.shape[1]), int((pos_x) * img.shape[1]) + c_size)
|
17 |
+
crop_y = (int(pos_y * img.shape[0]), int((pos_y) * img.shape[0]) + c_size)
|
18 |
+
return crop_x, crop_y
|
19 |
+
|
20 |
+
# Processing blocks
|
21 |
+
# -----------------
|
22 |
+
|
23 |
+
|
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, intensity = np.random.uniform(0.2, 0.8, 3)
|
28 |
+
context["puzzle"] = (pos_x, pos_y, intensity)
|
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, intensity = context["puzzle"]
|
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*0.4*crop
|
39 |
+
crop = flip_image(crop, flip=flip_gt, mirror=mirror_gt)
|
40 |
+
return out, crop
|
41 |
+
|
42 |
+
|
43 |
+
@interactive(
|
44 |
+
flip=(True, "Flip Image"),
|
45 |
+
mirror=(True, "Mirror Image"),
|
46 |
+
)
|
47 |
+
def flip_mirror_piece(piece: np.ndarray, flip=True, mirror=True):
|
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(puzzle, piece, pos_x: float = 0.5, pos_y: float = 0.5):
|
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 |
+
return [puzzle_piece, puzzle]
|
70 |
+
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
import argparse
|
74 |
+
parser = argparse.ArgumentParser()
|
75 |
+
parser.add_argument("-b", "--backend", default="gradio",
|
76 |
+
choices=["gradio", "qt"], type=str)
|
77 |
+
args = parser.parse_args()
|
78 |
+
markdown_description = "# Code to build this app on gradio \n"
|
79 |
+
markdown_description += "```python\n"+open(__file__, 'r').read()+"```"
|
80 |
+
img = Image.load_image("sample.jpg")
|
81 |
+
captcha_pipe_interactive = interactive_pipeline(
|
82 |
+
gui=args.backend,
|
83 |
+
cache=True,
|
84 |
+
markdown_description=markdown_description
|
85 |
+
)(captcha_pipe)
|
86 |
+
captcha_pipe_interactive(img)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
interactive-pipe>=0.8.3
|
sample.jpg
ADDED
![]() |