Views
No views yet
1from transformers import AutoModelForCausalLM, AutoProcessor
2from PIL import Image
3import numpy as np
4import torch
5
6def load_image(path):
7 with open(path, "rb") as file:
8 image = Image.open(file).convert("L").convert("RGB")
9 image = np.array(image)
10
11 return image
12
13images = ["01.jpg", "02.jpg"]
14images = [load_image(image) for image in images]
15
16# All panels from images, not provided by model
17panels = splitImagesToPanels(images)
18
19# The generated captions for each panels, not provided by model
20captions = generateCaptionsFromPanels(panels)
21
22model = AutoModelForCausalLM.from_pretrained('mrfish233/magiv3', torch_dtype=torch.float16, trust_remote_code=True).cuda().eval()
23processor = AutoProcessor.from_pretrained('mrfish233/magiv3', trust_remote_code=True)
24
25with torch.no_grad():
26 # detections from
27 detections = model.predict_detections_and_associations(images, processor)
28
29 # OCR for each page
30 ocr_results = model.predict_ocr(images, processor)
31
32 # get character grounding with captions provided
33 grounding = model.predict_character_grounding(panels, captions, processor)