| 74 | |
| 75 | |
| 76 | def check_and_read(img_path): |
| 77 | if os.path.basename(img_path)[-3:].lower() == "gif": |
| 78 | gif = cv2.VideoCapture(img_path) |
| 79 | ret, frame = gif.read() |
| 80 | if not ret: |
| 81 | logger = logging.getLogger("openrec") |
| 82 | logger.info("Cannot read {}. This gif image maybe corrupted.") |
| 83 | return None, False |
| 84 | if len(frame.shape) == 2 or frame.shape[-1] == 1: |
| 85 | frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB) |
| 86 | imgvalue = frame[:, :, ::-1] |
| 87 | return imgvalue, True, False |
| 88 | elif os.path.basename(img_path)[-3:].lower() == "pdf": |
| 89 | import fitz |
| 90 | from PIL import Image |
| 91 | |
| 92 | imgs = [] |
| 93 | with fitz.open(img_path) as pdf: |
| 94 | for pg in range(0, pdf.page_count): |
| 95 | page = pdf[pg] |
| 96 | mat = fitz.Matrix(2, 2) |
| 97 | pm = page.get_pixmap(matrix=mat, alpha=False) |
| 98 | |
| 99 | # if width or height > 2000 pixels, don't enlarge the image |
| 100 | if pm.width > 2000 or pm.height > 2000: |
| 101 | pm = page.get_pixmap(matrix=fitz.Matrix(1, 1), alpha=False) |
| 102 | |
| 103 | img = Image.frombytes("RGB", [pm.width, pm.height], pm.samples) |
| 104 | img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) |
| 105 | imgs.append(img) |
| 106 | return imgs, False, True |
| 107 | return None, False, False |
| 108 | |
| 109 | |
| 110 | def load_vqa_bio_label_maps(label_map_path): |