Alex Strick van Linschoten commited on
Commit
d89098d
1 Parent(s): 54f6682
Files changed (1) hide show
  1. app.py +43 -6
app.py CHANGED
@@ -1,14 +1,30 @@
 
 
 
 
1
  import gradio as gr
 
2
  import skimage
3
  from fastai.learner import load_learner
4
  from fastai.vision.all import *
5
- from huggingface_hub import hf_hub_download
6
- import fitz
7
- import tempfile
8
- import os
9
  from fpdf import FPDF
 
 
 
10
  from PIL import Image as PILImage
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  learn = load_learner(
13
  hf_hub_download("strickvl/redaction-classifier-fastai", "model.pkl")
14
  )
@@ -51,16 +67,37 @@ def predict(pdf, confidence, generate_file):
51
  [i for i in os.listdir(tmp_dir) if i.endswith("png")]
52
  )
53
  for image in imagelist:
 
 
 
 
 
 
 
54
  with PILImage.open(os.path.join(tmp_dir, image)) as img:
55
  size = img.size
56
  if size[0] > size[1]:
57
  pdf.add_page("L")
58
  else:
59
  pdf.add_page("P")
60
- # pdf.image(os.path.join(tmp_dir, image), w=190, h=280)
61
- pdf.image(os.path.join(tmp_dir, image))
 
 
 
 
 
 
 
 
 
 
 
 
62
  pdf.output(report, "F")
 
63
  text_output = f"A total of {len(redacted_pages)} pages were redacted. \n\n The redacted page numbers were: {', '.join(redacted_pages)}."
 
64
  if generate_file:
65
  return text_output, images, report
66
  else:
 
1
+ import os
2
+ import tempfile
3
+
4
+ import fitz
5
  import gradio as gr
6
+ import PIL
7
  import skimage
8
  from fastai.learner import load_learner
9
  from fastai.vision.all import *
 
 
 
 
10
  from fpdf import FPDF
11
+ from huggingface_hub import hf_hub_download
12
+ from icevision.all import *
13
+ from icevision.models.checkpoint import *
14
  from PIL import Image as PILImage
15
 
16
+ checkpoint_path = "./2022-01-15-vfnet-post-self-train.pth"
17
+ checkpoint_and_model = model_from_checkpoint(checkpoint_path)
18
+ model = checkpoint_and_model["model"]
19
+ model_type = checkpoint_and_model["model_type"]
20
+ class_map = checkpoint_and_model["class_map"]
21
+
22
+ img_size = checkpoint_and_model["img_size"]
23
+ valid_tfms = tfms.A.Adapter(
24
+ [*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()]
25
+ )
26
+
27
+
28
  learn = load_learner(
29
  hf_hub_download("strickvl/redaction-classifier-fastai", "model.pkl")
30
  )
 
67
  [i for i in os.listdir(tmp_dir) if i.endswith("png")]
68
  )
69
  for image in imagelist:
70
+ # with PILImage.open(os.path.join(tmp_dir, image)) as img:
71
+ # size = img.size
72
+ # if size[0] > size[1]:
73
+ # pdf.add_page("L")
74
+ # else:
75
+ # pdf.add_page("P")
76
+ # pdf.image(os.path.join(tmp_dir, image))
77
  with PILImage.open(os.path.join(tmp_dir, image)) as img:
78
  size = img.size
79
  if size[0] > size[1]:
80
  pdf.add_page("L")
81
  else:
82
  pdf.add_page("P")
83
+ pred_dict = model_type.end2end_detect(
84
+ img,
85
+ valid_tfms,
86
+ model,
87
+ class_map=class_map,
88
+ detection_threshold=0.7,
89
+ display_label=True,
90
+ display_bbox=True,
91
+ return_img=True,
92
+ font_size=16,
93
+ label_color="#FF59D6",
94
+ )
95
+ pred_dict["img"].save(os.path.join(tmp_dir, f"pred-{image}"))
96
+ pdf.image(os.path.join(tmp_dir, f"pred-{image}"))
97
  pdf.output(report, "F")
98
+
99
  text_output = f"A total of {len(redacted_pages)} pages were redacted. \n\n The redacted page numbers were: {', '.join(redacted_pages)}."
100
+
101
  if generate_file:
102
  return text_output, images, report
103
  else: