Borg93 commited on
Commit
0b8de4d
·
1 Parent(s): fc13392

Update visualizer.py

Browse files
Files changed (1) hide show
  1. app/tabs/visualizer.py +46 -46
app/tabs/visualizer.py CHANGED
@@ -1,75 +1,75 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
- from htrflow.volume.volume import Collection
 
 
 
 
5
  from htrflow.utils.draw import draw_polygons
6
  from htrflow.utils import imgproc
7
- import time
8
  from htrflow.results import Segment
9
 
10
 
11
- def load_visualize_state_from_submit(col: Collection, progress):
12
  results = []
13
 
14
- time.sleep(1)
15
-
16
  total_steps = len(col.pages)
17
 
18
  for page_idx, page_node in enumerate(col):
 
19
  page_image = page_node.image.copy()
20
 
21
- progress((page_idx + 1) / total_steps, desc=f"Running Visualizer")
22
-
23
- lines = list(page_node.traverse(lambda node: node.is_line()))
24
-
25
- recog_conf_values = {
26
- i: list(zip(tr.texts, tr.scores)) if (tr := ln.text_result) else []
27
- for i, ln in enumerate(lines)
28
- }
29
-
30
- recog_df = pd.DataFrame(
31
- [
32
- {"Transcription": text, "Confidence Score": f"{score:.4f}"}
33
- for values in recog_conf_values.values()
34
- for text, score in values
35
- ]
36
- )
37
 
38
  line_polygons = []
39
  line_crops = []
40
- for ln in lines:
41
- seg: Segment = ln.data.get("segment")
42
- if not seg:
43
- continue
44
 
45
- polygon = (
46
- seg.polygon.move(page_node.coord) if page_node.coord else seg.polygon
47
- )
48
- bbox = seg.bbox.move(page_node.coord) if page_node.coord else seg.bbox
49
 
50
- cropped_line_img = imgproc.crop(page_image, bbox)
51
- cropped_line_img = np.clip(cropped_line_img, 0, 255).astype(np.uint8)
52
- line_crops.append(cropped_line_img)
53
-
54
- if polygon is not None:
55
- line_polygons.append(polygon)
56
 
57
- annotated_image = draw_polygons(page_image, line_polygons)
 
 
 
 
 
 
 
 
 
 
 
 
58
  annotated_page_node = np.clip(annotated_image, 0, 255).astype(np.uint8)
59
 
60
- results.append(
61
- {
62
- "page_image": page_node,
63
- "annotated_page_node": annotated_page_node,
64
- "line_crops": line_crops,
65
- "recog_conf_values": recog_df,
66
- }
67
- )
68
 
69
  return results
70
 
71
- #TODO missing to able to click on left gallery and return respecitive mask.... i.e check if sselection is inside the polygons.. to the gallery on the right
72
-
 
 
 
 
 
 
 
73
 
74
  with gr.Blocks() as visualizer:
75
  with gr.Column(variant="panel"):
 
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
+ import time
5
+ from collections import defaultdict
6
+ from typing import List, Dict, Any
7
+
8
+ from htrflow.volume.volume import Collection, ImageNode, PageNode
9
  from htrflow.utils.draw import draw_polygons
10
  from htrflow.utils import imgproc
 
11
  from htrflow.results import Segment
12
 
13
 
14
+ def load_visualize_state_from_submit2_serial(col: Collection, progress):
15
  results = []
16
 
 
 
17
  total_steps = len(col.pages)
18
 
19
  for page_idx, page_node in enumerate(col):
20
+ page_node.to_original_size()
21
  page_image = page_node.image.copy()
22
 
23
+ progress((page_idx + 1) / total_steps, desc="Running Visualizer")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  line_polygons = []
26
  line_crops = []
27
+ recog_conf_values = {}
 
 
 
28
 
29
+ for i, node in enumerate(page_node.traverse(filter=lambda n: n.is_line())):
30
+ if node.polygon:
31
+ line_polygons.append(node.polygon)
 
32
 
33
+ try:
34
+ cropped_line_img = imgproc.crop(page_image, node.bbox)
35
+ cropped_line_img = np.clip(cropped_line_img, 0, 255).astype(np.uint8)
36
+ line_crops.append(cropped_line_img)
37
+ except Exception:
38
+ continue
39
 
40
+ if node.text_result:
41
+ recog_conf_values[i] = list(zip(
42
+ node.text_result.texts,
43
+ node.text_result.scores
44
+ ))
45
+
46
+ annotated_image = draw_polygons(
47
+ image=page_image,
48
+ polygons=line_polygons,
49
+ color=Colors.BLUE,
50
+ thickness=3,
51
+ alpha=0.2
52
+ )
53
  annotated_page_node = np.clip(annotated_image, 0, 255).astype(np.uint8)
54
 
55
+ results.append({
56
+ "page_image": page_node,
57
+ "annotated_page_node": annotated_page_node,
58
+ "line_crops": line_crops,
59
+ "recog_conf_values": _convert_conf_values_to_df(recog_conf_values),
60
+ })
 
 
61
 
62
  return results
63
 
64
+ def _convert_conf_values_to_df(conf_values: Dict[int, List[tuple[str, float]]]) -> pd.DataFrame:
65
+ """Convert recognition confidence values to a pandas DataFrame."""
66
+ return pd.DataFrame(
67
+ [
68
+ {"Transcription": text, "Confidence Score": f"{score:.4f}"}
69
+ for values in conf_values.values()
70
+ for text, score in values
71
+ ]
72
+ )
73
 
74
  with gr.Blocks() as visualizer:
75
  with gr.Column(variant="panel"):