File size: 2,116 Bytes
e7fc64a
 
3d3c04e
e7fc64a
 
 
3d3c04e
e7fc64a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d3c04e
e7fc64a
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import PIL.Image
import gradio as gr
import torch
import numpy as np
from craft_text_detector import Craft

craft = Craft(output_dir='output', crop_type="box", cuda=torch.cuda.is_available(), export_extra=True)

dw=0.3
dh=0.25
def is_nw(box):
    """
    A box happen to be a 4-pixel list in order
    1 -- 2
    4 -- 3
    """
    return box[2][0]<=dw and box[2][1]<= dh

def is_ne(box):
    return box[3][0]>=1-dw and box[3][1]<= dh

def is_se(box):
    return box[0][0]>=1-dw and box[0][1]>= 1-dh

def is_sw(box):
    return box[1][0]<=dw and box[1][1]>= 1-dh

def is_corner(box)->bool:
    """ @:returns true if the box is located in any corner """
    return is_nw(box) or is_ne(box) or is_se(box) or is_sw(box)

dhhf=0.2 # dh for header and footer
def is_footer(box)->bool:
    """ true if for the 2 first points, y>0.8 """
    return box[0][1]>=1-dhhf and box[1][1]>=1-dhhf

def is_header(box)->bool:
    """ true if for the 2 last points, y<0.2 """
    return box[2][1]<=dhhf and box[3][1]<=dhhf

def is_signature(prediction_result) -> bool:
    """ true if any of the boxes is at any corner """
    for box in prediction_result['boxes_as_ratios']:
        if is_corner(box) or is_header(box) or is_footer(box):
            return True
    return False

def detect(image: PIL.Image.Image):
    result = craft.detect_text( np.asarray(image))
    return result['boxes'], is_signature(result)

def process(image:PIL.Image.Image):
    if image is None:
        return None,0,False
    boxes,signed = detect( image)
    annotated = PIL.Image.open('output/image_text_detection.png') # image with boxes displayed
    return annotated, len(boxes), signed

gr.Interface(
    fn = process,
    inputs = [ gr.Image(type="pil", label="Input") ],
    outputs = [ gr.Image(type="pil", label="Output"), gr.Label(label="nb of text detections"), gr.Label(label="Has signature") ],
    title="Detect signature in image",
    description="Is the photo or image watermarked by a signature?",
    examples=[['data/photologo-1-1.jpg'], ['data/times-square.jpg']],
    allow_flagging="never"
).launch(debug=True, enable_queue=True)