MattyMroz commited on
Commit
8d12d79
1 Parent(s): 4ef9317

Update manga-whisperer.py

Browse files
Files changed (1) hide show
  1. manga-whisperer.py +35 -35
manga-whisperer.py CHANGED
@@ -1,35 +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")
 
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
+ "test/1.png",
9
+ "test/2.png",
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")