coutant commited on
Commit
61be565
1 Parent(s): b371285

check signature with craft_hw_ocr as an option to craft_text_detection

Browse files
Files changed (2) hide show
  1. app.py +33 -29
  2. data/photologo-3.jpg +0 -0
app.py CHANGED
@@ -3,7 +3,30 @@ import gradio as gr
3
  import torch
4
  import numpy as np
5
 
6
- dw=0.3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  dh=0.25
8
  def is_nw(box):
9
  """
@@ -35,41 +58,22 @@ def is_header(box)->bool:
35
  """ true if for the 2 last points, y<0.2 """
36
  return box[2][1]<=dhhf and box[3][1]<=dhhf
37
 
38
- def is_signature(prediction_result) -> bool:
39
- """ true if any of the boxes is at any corner """
40
- for box in prediction_result['boxes_as_ratios']:
 
 
 
41
  if is_corner(box) or is_header(box) or is_footer(box):
42
  return True
43
  return False
44
 
45
- def detect_with_craft_text_detector(image: PIL.Image.Image):
46
- from craft_text_detector import Craft
47
- craft = Craft(output_dir='output', crop_type="box", cuda=torch.cuda.is_available(), export_extra=True)
48
- result = craft.detect_text( np.asarray(image))
49
- annotated = PIL.Image.open('output/image_text_detection.png') # image with boxes displayed
50
- return annotated, result['boxes'], is_signature(result)
51
-
52
- def detect_with_craft_hw_ocr(image: PIL.Image.Image):
53
- image = np.asarray(image)
54
- from craft_hw_ocr import OCR
55
- ocr = OCR.load_models()
56
- image, results = OCR.detection(image, ocr[2])
57
- bboxes, _ = OCR.recoginition(image, results, ocr[0], ocr[1])
58
- annotated = OCR.visualize(image, results)
59
- return annotated, bboxes, False
60
-
61
- def process(image:PIL.Image.Image, lib:str):
62
- if image is None:
63
- return None,0,False
64
- annotated, boxes, signed = detect_with_craft_text_detector(image) if lib=='craft_text_detector' else detect_with_craft_hw_ocr( image)
65
- return annotated, len(boxes), signed
66
-
67
  gr.Interface(
68
  fn = process,
69
- inputs = [ gr.Image(type="pil", label="Input"), gr.inputs.Radio(label='Model', choices=["craft_text_detector", "craft_hw_ocr"], default='craft_text_detector') ],
70
- outputs = [ gr.Image(type="pil", label="Output"), gr.Label(label="nb of text detections"), gr.Label(label="Has signature") ],
71
  title="Detect signature in image",
72
  description="Is the photo or image watermarked by a signature?",
73
- examples=[['data/photologo-1-1.jpg'], ['data/times-square.jpg']],
74
  allow_flagging="never"
75
  ).launch(debug=True, enable_queue=True)
 
3
  import torch
4
  import numpy as np
5
 
6
+ def detect_with_craft_text_detector(image: np.ndarray):
7
+ from craft_text_detector import Craft
8
+ craft = Craft(output_dir='output', crop_type="box", cuda=torch.cuda.is_available(), export_extra=True)
9
+ result = craft.detect_text( image)
10
+ annotated = PIL.Image.open('output/image_text_detection.png') # image with boxes displayed
11
+ return annotated, result['boxes'], is_signature(result['boxes_as_ratios'])
12
+
13
+ def detect_with_craft_hw_ocr(image: np.ndarray):
14
+ from craft_hw_ocr import OCR
15
+ ocr = OCR.load_models()
16
+ image, results = OCR.detection(image, ocr[2])
17
+ bboxes, _ = OCR.recoginition(image, results, ocr[0], ocr[1])
18
+ h,w,_=np.shape(image) # third dimension is color channel
19
+ annotated = OCR.visualize(image, results)
20
+ m=(np.asarray([w,h]))[np.newaxis,np.newaxis,:]
21
+ return annotated, bboxes, is_signature(bboxes/m)
22
+
23
+ def process(image:np.ndarray, lib:str):
24
+ if image is None:
25
+ return None,'',''
26
+ annotated, boxes, signed = detect_with_craft_text_detector(image) if lib=='craft_text_detector' else detect_with_craft_hw_ocr( image)
27
+ return annotated, len(boxes), signed
28
+
29
+ dw=0.3 # width ratio
30
  dh=0.25
31
  def is_nw(box):
32
  """
 
58
  """ true if for the 2 last points, y<0.2 """
59
  return box[2][1]<=dhhf and box[3][1]<=dhhf
60
 
61
+ # def is_signature(prediction_result) -> bool:
62
+ def is_signature(boxes) -> bool:
63
+ """ true if any of the boxes is at any corner, or header or footer """
64
+ for box in boxes:
65
+ if box[1][0]-box[0][0]<0.05: # not large enough
66
+ continue
67
  if is_corner(box) or is_header(box) or is_footer(box):
68
  return True
69
  return False
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  gr.Interface(
72
  fn = process,
73
+ inputs = [ gr.Image(label="Input"), gr.inputs.Radio(label='Model', choices=["craft_text_detector", "craft_hw_ocr"], default='craft_text_detector') ],
74
+ outputs = [ gr.Image(label="Output"), gr.Label(label="nb of text detections"), gr.Label(label="Has signature") ],
75
  title="Detect signature in image",
76
  description="Is the photo or image watermarked by a signature?",
77
+ examples=[['data/photologo-1-1.jpg'], ['data/times-square.jpg'], ['data/photologo-3.jpg']],
78
  allow_flagging="never"
79
  ).launch(debug=True, enable_queue=True)
data/photologo-3.jpg ADDED