Upload 4 files
Browse files- 1.jpg +3 -0
- 1.png +3 -0
- manga-whisperer.py +35 -0
- requirements.txt +8 -0
1.jpg
ADDED
Git LFS Details
|
1.png
ADDED
Git LFS Details
|
manga-whisperer.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModel
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
|
7 |
+
images = [
|
8 |
+
"1.png",
|
9 |
+
"1.jpg",
|
10 |
+
]
|
11 |
+
|
12 |
+
|
13 |
+
def read_image_as_np_array(image_path):
|
14 |
+
with open(image_path, "rb") as file:
|
15 |
+
image = Image.open(file).convert("L").convert("RGB")
|
16 |
+
image = np.array(image)
|
17 |
+
return image
|
18 |
+
|
19 |
+
|
20 |
+
images = [read_image_as_np_array(image) for image in images]
|
21 |
+
|
22 |
+
model = AutoModel.from_pretrained(
|
23 |
+
"ragavsachdeva/magi", trust_remote_code=True).cuda()
|
24 |
+
# model = AutoModel.from_pretrained(
|
25 |
+
# "./magi", trust_remote_code=True).cuda()
|
26 |
+
with torch.no_grad():
|
27 |
+
results = model.predict_detections_and_associations(images)
|
28 |
+
text_bboxes_for_all_images = [x["texts"] for x in results]
|
29 |
+
ocr_results = model.predict_ocr(images, text_bboxes_for_all_images)
|
30 |
+
|
31 |
+
for i in range(len(images)):
|
32 |
+
model.visualise_single_image_prediction(
|
33 |
+
images[i], results[i], filename=f"image_{i}.png")
|
34 |
+
model.generate_transcript_for_single_image(
|
35 |
+
results[i], ocr_results[i], filename=f"transcript_{i}.txt")
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
einops
|
3 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
4 |
+
torch
|
5 |
+
shapely
|
6 |
+
timm
|
7 |
+
scipy
|
8 |
+
tokenizers
|