coutant commited on
Commit
e7fc64a
1 Parent(s): 17832a2

first release detecting signature

Browse files
Files changed (5) hide show
  1. .gitignore +2 -0
  2. app.py +67 -0
  3. data/photologo-1-1.jpg +0 -0
  4. data/times-square.jpg +0 -0
  5. requirements.txt +10 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .idea
2
+ output
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import PIL.Image
2
+ import gradio as gr
3
+
4
+ import numpy as np
5
+ from craft_text_detector import Craft
6
+
7
+ craft = Craft(output_dir='output', crop_type="box", cuda=True, export_extra=True)
8
+
9
+ dw=0.3
10
+ dh=0.25
11
+ def is_nw(box):
12
+ """
13
+ A box happen to be a 4-pixel list in order
14
+ 1 -- 2
15
+ 4 -- 3
16
+ """
17
+ return box[2][0]<=dw and box[2][1]<= dh
18
+
19
+ def is_ne(box):
20
+ return box[3][0]>=1-dw and box[3][1]<= dh
21
+
22
+ def is_se(box):
23
+ return box[0][0]>=1-dw and box[0][1]>= 1-dh
24
+
25
+ def is_sw(box):
26
+ return box[1][0]<=dw and box[1][1]>= 1-dh
27
+
28
+ def is_corner(box)->bool:
29
+ """ @:returns true if the box is located in any corner """
30
+ return is_nw(box) or is_ne(box) or is_se(box) or is_sw(box)
31
+
32
+ dhhf=0.2 # dh for header and footer
33
+ def is_footer(box)->bool:
34
+ """ true if for the 2 first points, y>0.8 """
35
+ return box[0][1]>=1-dhhf and box[1][1]>=1-dhhf
36
+
37
+ def is_header(box)->bool:
38
+ """ true if for the 2 last points, y<0.2 """
39
+ return box[2][1]<=dhhf and box[3][1]<=dhhf
40
+
41
+ def is_signature(prediction_result) -> bool:
42
+ """ true if any of the boxes is at any corner """
43
+ for box in prediction_result['boxes_as_ratios']:
44
+ if is_corner(box) or is_header(box) or is_footer(box):
45
+ return True
46
+ return False
47
+
48
+ def detect(image: PIL.Image.Image):
49
+ result = craft.detect_text( np.asarray(image))
50
+ return result['boxes'], is_signature(result)
51
+
52
+ def process(image:PIL.Image.Image):
53
+ if image is None:
54
+ return None,0
55
+ boxes,signed = detect( image)
56
+ annotated = PIL.Image.open('output/image_text_detection.png') # image with boxes displayed
57
+ return annotated, len(boxes), signed
58
+
59
+ gr.Interface(
60
+ fn = process,
61
+ inputs = [ gr.Image(type="pil", label="Input") ],
62
+ outputs = [ gr.Image(type="pil", label="Output"), gr.Label(label="nb of text detections"), gr.Label(label="Has signature") ],
63
+ title="Detect signature in image",
64
+ description="Is the photo or image watermarked by a signature?",
65
+ examples=[['data/photologo-1-1.jpg'], ['data/times-square.jpg']],
66
+ allow_flagging="never"
67
+ ).launch(debug=True, enable_queue=True)
data/photologo-1-1.jpg ADDED
data/times-square.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ Pillow
3
+ opencv-python
4
+ numpy
5
+ PyYAML
6
+ seaborn
7
+ pandas
8
+ matplotlib
9
+ scipy
10
+ psutil