Download manga-whisperer.py from MattyMroz/test: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/datasets/MattyMroz/test/resolve/8d12d79021a615a0102741487ee3e49df9640dc7/manga-whisperer.py
- Command line
-
hf download hf://datasets/MattyMroz/test@8d12d79021a615a0102741487ee3e49df9640dc7/manga-whisperer.py
-
curl -L -o manga-whisperer.py https://huggingface.co/datasets/MattyMroz/test/resolve/8d12d79021a615a0102741487ee3e49df9640dc7/manga-whisperer.py
1.05 kB
| from transformers import AutoModel | |
| import numpy as np | |
| from PIL import Image | |
| import torch | |
| import os | |
| images = [ | |
| "test/1.png", | |
| "test/2.png", | |
| ] | |
| def read_image_as_np_array(image_path): | |
| with open(image_path, "rb") as file: | |
| image = Image.open(file).convert("L").convert("RGB") | |
| image = np.array(image) | |
| return image | |
| images = [read_image_as_np_array(image) for image in images] | |
| model = AutoModel.from_pretrained( | |
| "ragavsachdeva/magi", trust_remote_code=True).cuda() | |
| # model = AutoModel.from_pretrained( | |
| # "./magi", trust_remote_code=True).cuda() | |
| with torch.no_grad(): | |
| results = model.predict_detections_and_associations(images) | |
| text_bboxes_for_all_images = [x["texts"] for x in results] | |
| ocr_results = model.predict_ocr(images, text_bboxes_for_all_images) | |
| for i in range(len(images)): | |
| model.visualise_single_image_prediction( | |
| images[i], results[i], filename=f"image_{i}.png") | |
| model.generate_transcript_for_single_image( | |
| results[i], ocr_results[i], filename=f"transcript_{i}.txt") | |