kuko6 commited on
Commit
d18605c
·
1 Parent(s): a8ca0bd

Speed up Gradio image previews

Browse files
Files changed (2) hide show
  1. app.py +17 -3
  2. data/dirty_example.png +3 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import zipfile
3
  from pathlib import Path
4
  from uuid import uuid4
5
 
 
6
  import gradio as gr
7
 
8
  from cleaning import (
@@ -13,8 +14,9 @@ from cleaning import (
13
  )
14
 
15
  _BATCH_OUTPUTS = tempfile.TemporaryDirectory(prefix="ihc-cleaner-")
16
- _EXAMPLE_IMAGES = [["data/dirty.png"], ["data/dirty.tiff"]]
17
  _IMAGE_FILE_TYPES = ["image", ".tif", ".tiff"]
 
18
 
19
 
20
  def _uploaded_path(file_path: str | Path | None) -> Path:
@@ -24,11 +26,23 @@ def _uploaded_path(file_path: str | Path | None) -> Path:
24
  return Path(file_path)
25
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def preview_image(file_path: str | Path | None):
28
  if not file_path:
29
  return None, None, None
30
 
31
- return read_image_rgb(file_path), None, None
32
 
33
 
34
  def clean_uploaded_image(file_path: str | Path | None):
@@ -41,7 +55,7 @@ def clean_uploaded_image(file_path: str | Path | None):
41
  cleaned_path = output_dir / cleaned_image_name(image_path)
42
  write_image_rgb(cleaned_path, cleaned_rgb)
43
 
44
- return image_rgb, cleaned_rgb, str(cleaned_path)
45
 
46
 
47
  def clean_directory(image_paths: list[str] | None) -> tuple[str, str]:
 
3
  from pathlib import Path
4
  from uuid import uuid4
5
 
6
+ import cv2
7
  import gradio as gr
8
 
9
  from cleaning import (
 
14
  )
15
 
16
  _BATCH_OUTPUTS = tempfile.TemporaryDirectory(prefix="ihc-cleaner-")
17
+ _EXAMPLE_IMAGES = [["data/dirty_example.png"]]
18
  _IMAGE_FILE_TYPES = ["image", ".tif", ".tiff"]
19
+ _PREVIEW_MAX_DIMENSION = 1200
20
 
21
 
22
  def _uploaded_path(file_path: str | Path | None) -> Path:
 
26
  return Path(file_path)
27
 
28
 
29
+ def _preview_image(image):
30
+ height, width = image.shape[:2]
31
+ largest_dimension = max(height, width)
32
+
33
+ if largest_dimension <= _PREVIEW_MAX_DIMENSION:
34
+ return image
35
+
36
+ scale = _PREVIEW_MAX_DIMENSION / largest_dimension
37
+ preview_size = (round(width * scale), round(height * scale))
38
+ return cv2.resize(image, preview_size, interpolation=cv2.INTER_AREA)
39
+
40
+
41
  def preview_image(file_path: str | Path | None):
42
  if not file_path:
43
  return None, None, None
44
 
45
+ return _preview_image(read_image_rgb(file_path)), None, None
46
 
47
 
48
  def clean_uploaded_image(file_path: str | Path | None):
 
55
  cleaned_path = output_dir / cleaned_image_name(image_path)
56
  write_image_rgb(cleaned_path, cleaned_rgb)
57
 
58
+ return _preview_image(image_rgb), _preview_image(cleaned_rgb), str(cleaned_path)
59
 
60
 
61
  def clean_directory(image_paths: list[str] | None) -> tuple[str, str]:
data/dirty_example.png ADDED

Git LFS Details

  • SHA256: cd515bb23aa26435f50141a0fadcba14051bc9615116cce92be2e216d43b3a36
  • Pointer size: 132 Bytes
  • Size of remote file: 1.69 MB