ragavsachdeva commited on
Commit
2c3862a
1 Parent(s): 492c088

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModel
3
+ from PIL import Image
4
+ import torch
5
+ import numpy as np
6
+
7
+ @st.cache_resource
8
+ def load_model():
9
+ model = AutoModel.from_pretrained("ragavsachdeva/magi", trust_remote_code=True)
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model.to(device)
12
+ return model
13
+
14
+ @st.cache_data
15
+ def read_image_as_np_array(image_path):
16
+ with open(image_path, "rb") as file:
17
+ image = Image.open(file).convert("L").convert("RGB")
18
+ image = np.array(image)
19
+ return image
20
+
21
+ @st.cache_data
22
+ def predict_detections_and_associations(
23
+ image_path,
24
+ character_detection_threshold,
25
+ panel_detection_threshold,
26
+ text_detection_threshold,
27
+ character_character_matching_threshold,
28
+ text_character_matching_threshold,
29
+ ):
30
+ image = read_image_as_np_array(image_path)
31
+ with torch.no_grad():
32
+ result = model.predict_detections_and_associations(
33
+ [image],
34
+ character_detection_threshold=character_detection_threshold,
35
+ panel_detection_threshold=panel_detection_threshold,
36
+ text_detection_threshold=text_detection_threshold,
37
+ character_character_matching_threshold=character_character_matching_threshold,
38
+ text_character_matching_threshold=text_character_matching_threshold,
39
+ )[0]
40
+ return result
41
+
42
+ @st.cache_data
43
+ def predict_ocr(
44
+ image_path,
45
+ character_detection_threshold,
46
+ panel_detection_threshold,
47
+ text_detection_threshold,
48
+ character_character_matching_threshold,
49
+ text_character_matching_threshold,
50
+ ):
51
+ if not generate_transcript:
52
+ return
53
+ image = read_image_as_np_array(image_path)
54
+ result = predict_detections_and_associations(
55
+ path_to_image,
56
+ character_detection_threshold,
57
+ panel_detection_threshold,
58
+ text_detection_threshold,
59
+ character_character_matching_threshold,
60
+ text_character_matching_threshold,
61
+ )
62
+ text_bboxes_for_all_images = [result["texts"]]
63
+ with torch.no_grad():
64
+ ocr_results = model.predict_ocr([image], text_bboxes_for_all_images)
65
+ return ocr_results
66
+
67
+ model = load_model()
68
+
69
+ path_to_image = "/scratch/shared/beegfs/rs/comics/mangas/bakuman/1.0/p_00009.png"
70
+ st.markdown("<style>.title{font-size:2em;text-align:center;color:#fff;font-family:'Comic Sans MS',cursive;text-transform:uppercase;letter-spacing:.1em;padding:.5em 0 .2em;background:0 0}.title span{background:-webkit-linear-gradient(45deg,#6495ed,#4169e1);-webkit-background-clip:text;-webkit-text-fill-color:transparent}.subheading{font-size:1.5em;text-align:center;color:#ddd;font-family:'Comic Sans MS',cursive}.affil,.authors{font-size:1em;text-align:center;color:#ddd;font-family:'Comic Sans MS',cursive}.authors{padding-top:1em}</style><div class='title-container'> <div class='title'> The <span>Ma</span>n<span>g</span>a Wh<span>i</span>sperer </div> <div class='subheading'> Automatically Generating Transcriptions for Comics </div> <div class='authors'> Ragav Sachdeva and Andrew Zisserman </div> <div class='affil'> University of Oxford </div></div>", unsafe_allow_html=True)
71
+ path_to_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
72
+
73
+ st.sidebar.markdown("**Mode**")
74
+ generate_detections_and_associations = st.sidebar.toggle("Generate detections and associations", True)
75
+ generate_transcript = st.sidebar.toggle("Generate transcript (slower)", False)
76
+ st.sidebar.markdown("**Hyperparameters**")
77
+ input_character_detection_threshold = st.sidebar.slider('Character detection threshold', 0.0, 1.0, 0.30, step=0.01)
78
+ input_panel_detection_threshold = st.sidebar.slider('Panel detection threshold', 0.0, 1.0, 0.2, step=0.01)
79
+ input_text_detection_threshold = st.sidebar.slider('Text detection threshold', 0.0, 1.0, 0.25, step=0.01)
80
+ input_character_character_matching_threshold = st.sidebar.slider('Character-character matching threshold', 0.0, 1.0, 0.7, step=0.01)
81
+ input_text_character_matching_threshold = st.sidebar.slider('Text-character matching threshold', 0.0, 1.0, 0.4, step=0.01)
82
+
83
+
84
+ if path_to_image is None:
85
+ st.stop()
86
+
87
+ image = read_image_as_np_array(path_to_image)
88
+
89
+ st.markdown("**Prediction**")
90
+ if generate_detections_and_associations or generate_transcript:
91
+ result = predict_detections_and_associations(
92
+ path_to_image,
93
+ input_character_detection_threshold,
94
+ input_panel_detection_threshold,
95
+ input_text_detection_threshold,
96
+ input_character_character_matching_threshold,
97
+ input_text_character_matching_threshold,
98
+ )
99
+
100
+ if generate_transcript:
101
+ ocr_results = predict_ocr(
102
+ path_to_image,
103
+ input_character_detection_threshold,
104
+ input_panel_detection_threshold,
105
+ input_text_detection_threshold,
106
+ input_character_character_matching_threshold,
107
+ input_text_character_matching_threshold,
108
+ )
109
+
110
+ if generate_detections_and_associations and generate_transcript:
111
+ col1, col2 = st.columns(2)
112
+ output = model.visualise_single_image_prediction(image, result)
113
+ col1.image(output)
114
+ text_bboxes_for_all_images = [result["texts"]]
115
+ ocr_results = model.predict_ocr([image], text_bboxes_for_all_images)
116
+ transcript = model.generate_transcript_for_single_image(result, ocr_results[0])
117
+ col2.text(transcript)
118
+
119
+ elif generate_detections_and_associations:
120
+ output = model.visualise_single_image_prediction(image, result)
121
+ st.image(output)
122
+
123
+ elif generate_transcript:
124
+ transcript = model.generate_transcript_for_single_image(result, ocr_results[0])
125
+ st.text(transcript)
126
+