guangyangmusic commited on
Commit
e337419
·
1 Parent(s): 37754dc

feat: add examples and image cropping method

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.png filter=lfs diff=lfs merge=lfs -text
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import spaces # Must be before any CUDA/torch imports
2
  import gradio as gr
3
 
@@ -5,6 +7,14 @@ import abc_utils
5
  import config
6
  from inference import inference
7
 
 
 
 
 
 
 
 
 
8
  with gr.Blocks(theme=gr.themes.Soft(), title="LEGATO OMR Demo") as demo:
9
  gr.Markdown("""
10
  # 🎼 LEGATO: Large-scale End-to-end Generalizable Approach to Typeset OMR
@@ -33,6 +43,12 @@ with gr.Blocks(theme=gr.themes.Soft(), title="LEGATO OMR Demo") as demo:
33
 
34
  gr.Markdown("### ✨ Try it")
35
  inp = gr.Image(type="pil", label="📤 Upload score image")
 
 
 
 
 
 
36
  with gr.Row():
37
  out = gr.Textbox(label="📝 ABC transcription", lines=10, buttons=["copy"])
38
  with gr.Accordion("🎵 Rendered ABC notation", open=True):
 
1
+ import os
2
+
3
  import spaces # Must be before any CUDA/torch imports
4
  import gradio as gr
5
 
 
7
  import config
8
  from inference import inference
9
 
10
+ _EXAMPLE_EXTENSIONS = (".png", ".jpg", ".jpeg", ".webp")
11
+ _examples_dir = os.path.join(config.APP_DIR, "examples")
12
+ _example_paths = []
13
+ if os.path.isdir(_examples_dir):
14
+ for name in sorted(os.listdir(_examples_dir)):
15
+ if name.lower().endswith(_EXAMPLE_EXTENSIONS):
16
+ _example_paths.append([os.path.join(_examples_dir, name)])
17
+
18
  with gr.Blocks(theme=gr.themes.Soft(), title="LEGATO OMR Demo") as demo:
19
  gr.Markdown("""
20
  # 🎼 LEGATO: Large-scale End-to-end Generalizable Approach to Typeset OMR
 
43
 
44
  gr.Markdown("### ✨ Try it")
45
  inp = gr.Image(type="pil", label="📤 Upload score image")
46
+ if _example_paths:
47
+ gr.Examples(
48
+ examples=_example_paths,
49
+ inputs=[inp],
50
+ label="Example scores",
51
+ )
52
  with gr.Row():
53
  out = gr.Textbox(label="📝 ABC transcription", lines=10, buttons=["copy"])
54
  with gr.Accordion("🎵 Rendered ABC notation", open=True):
examples/.gitkeep ADDED
File without changes
examples/Example1.png ADDED

Git LFS Details

  • SHA256: bbfe68a324d16143a4a320bf296a88a2ae925dad9d2157205736c7c5db6f3e16
  • Pointer size: 130 Bytes
  • Size of remote file: 93.7 kB
examples/Example2.png ADDED

Git LFS Details

  • SHA256: 85d6b7a0e762f8c32900a139b22bce1743bacd1f457b3ba160851dfd4e8c5f5c
  • Pointer size: 131 Bytes
  • Size of remote file: 117 kB
examples/Example3.png ADDED

Git LFS Details

  • SHA256: 6fd3db6678caa824a6f7549c4e738f1c27f741a4d78a20cd170eab9f51f746ae
  • Pointer size: 131 Bytes
  • Size of remote file: 269 kB
examples/Example4.png ADDED

Git LFS Details

  • SHA256: c7cecb54aacdba280c7b3699592a678586518691f82bac1fc6385ec3d172857d
  • Pointer size: 132 Bytes
  • Size of remote file: 1.23 MB
examples/Example5.png ADDED

Git LFS Details

  • SHA256: 4fac59f7638177316474fb2afb36bb60aa5e71819bc1ab70afbb2f828f069978
  • Pointer size: 131 Bytes
  • Size of remote file: 256 kB
image_utils.py CHANGED
@@ -5,12 +5,12 @@ from config import LETTER_ASPECT
5
 
6
  def pad_to_portrait_letter(pil_image: Image.Image) -> Image.Image:
7
  """If aspect ratio is wider than letter, pad at the bottom to match letter aspect."""
8
- w, h = pil_image.size
9
- if w / h < LETTER_ASPECT:
10
- return pil_image
11
- new_h = int(round(w / LETTER_ASPECT))
12
- canvas = Image.new("RGB", (w, new_h), (255, 255, 255))
13
  if pil_image.mode != "RGB":
14
  pil_image = pil_image.convert("RGB")
 
 
 
 
 
15
  canvas.paste(pil_image, (0, 0))
16
  return canvas
 
5
 
6
  def pad_to_portrait_letter(pil_image: Image.Image) -> Image.Image:
7
  """If aspect ratio is wider than letter, pad at the bottom to match letter aspect."""
 
 
 
 
 
8
  if pil_image.mode != "RGB":
9
  pil_image = pil_image.convert("RGB")
10
+ w, h = pil_image.size
11
+ pil_image = pil_image.resize((1050, 1050*h//w))
12
+ if pil_image.height >= 1485:
13
+ return pil_image
14
+ canvas = Image.new("RGB", (1050, 1485), (255, 255, 255))
15
  canvas.paste(pil_image, (0, 0))
16
  return canvas