Skip to content

Commit

Permalink
get commands working
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Mar 16, 2024
1 parent 07a5e2b commit cf6227d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
50 changes: 40 additions & 10 deletions app/tasks/mit.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ def _preprocess_mit(image_path: str, target_lang: str):
_run_mit_translate(t["text"], target_lang=target_lang) for t in ocred
]
quads = [
MitTextQuad(
pts=t["pts"],
raw_text=t["text"],
translated=translated_texts[i],
)
{
"pts": t["pts"],
"raw_text": t["text"],
"translated": translated_texts[i],
}
for i, t in enumerate(ocred)
]
return MitPreprocessedImage(
image_path=image_path,
target_lang=target_lang,
text_quads=quads,
)
return {
"image_path": image_path,
"target_lang": target_lang,
"text_quads": quads,
}


@dataclass(frozen=True)
Expand All @@ -114,13 +114,43 @@ class MitTextQuad:
raw_text: str
translated: str

def to_dict(self) -> dict[str, Any]:
return {
"pts": self.pts,
"raw_text": self.raw_text,
"translated": self.translated,
}

@classmethod
def from_dict(cls, d: dict[str, Any]) -> "MitTextQuad":
return cls(
pts=d["pts"],
raw_text=d["raw_text"],
translated=d["translated"],
)


@dataclass(frozen=True)
class MitPreprocessedImage:
image_path: str
target_lang: str
text_quads: list[MitTextQuad]

def to_dict(self) -> dict[str, Any]:
return {
"image_path": self.image_path,
"target_lang": self.target_lang,
"text_quads": [t.to_dict() for t in self.text_quads],
}

@classmethod
def from_dict(cls, d: dict[str, Any]) -> "MitPreprocessedImage":
return cls(
image_path=d["image_path"],
target_lang=d["target_lang"],
text_quads=[MitTextQuad.from_dict(t) for t in d["text_quads"]],
)


# export tasks with a better type
mit_detect_text: Task = _mit_detect_text # type: ignore
Expand Down
34 changes: 27 additions & 7 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re

import click

Expand Down Expand Up @@ -85,20 +86,39 @@ def local(action):
)


@click.command("poc-mit")
def dummy_call_mit():
from app.tasks.mit import preprocess_mit
@click.command("mit_file")
@click.option("--file", help="path to image file")
def mit_preprocess_file(file: str):
from app.tasks.mit import preprocess_mit, MitPreprocessedImage

sample_img = "/var/lib/moeflow-storage/bj001-112.png"
proprocessed = preprocess_mit.delay(file, "CHT")
proprocessed_result: dict = proprocessed.get()

proprocessed = preprocess_mit.delay(sample_img, "CHT")
print('proprocessed', proprocessed.get())
print("proprocessed", proprocessed_result)
print("proprocessed", MitPreprocessedImage.from_dict(proprocessed_result))


@click.command("mit_dir")
@click.option("--dir", help="absolute path to a dir containing image files")
def mit_preprocess_dir(dir: str):
from app.tasks.mit import preprocess_mit, MitPreprocessedImage

for file in os.listdir(dir):
if not re.match(r".*\.(jpg|png|jpeg)$", file):
continue
full_path = os.path.join(dir, file)
proprocessed = preprocess_mit.delay(full_path, "CHT")
proprocessed_result: dict = proprocessed.get()

print("proprocessed", proprocessed_result)
print("proprocessed", MitPreprocessedImage.from_dict(proprocessed_result))


main.add_command(run)
main.add_command(local)
main.add_command(docs)
main.add_command(dummy_call_mit)
main.add_command(mit_preprocess_file)
main.add_command(mit_preprocess_dir)


if __name__ == "__main__":
Expand Down

0 comments on commit cf6227d

Please sign in to comment.