Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.8 kB
1
Indexable
Never
#%%
#%%
import openslide
import glob
import os
import matplotlib.pyplot as plt
import numpy as np

import easyocr
#%%
imgDir = '/project/shared/kapur_rajaram_phi/Staging/From_Dinesh_20240703_part4'
svsPaths = glob.glob(os.path.join(imgDir, '*.svs'))

# Initialize EasyOCR reader for English
reader = easyocr.Reader(['en'], gpu=False)

#%%
for svsPath in svsPaths:
    slide = openslide.open_slide(svsPath)

    label_img = slide.associated_images.get('label')

    if label_img:
        # Convert the label image to a format suitable for OCR (numpy array)
        label_img_np = np.array(label_img)

        for angle in [0, 90, 180, 270]:
            # Rotate the image by the specified angle
            rotated_img = label_img.rotate(angle, expand=True)
            rotated_img_np = np.array(rotated_img)

            # Use EasyOCR to extract text
            extracted_text = reader.readtext(rotated_img_np, detail=0)  # detail=0 returns only the text

            print(f"Extracted Text from {os.path.basename(svsPath)} at {angle} degrees:")
            print("\n".join(extracted_text))  # Print each line of extracted text
            print("-" * 50)

            # Display the rotated image
            plt.imshow(rotated_img_np)
            plt.title(f"Label Image (Rotated {angle} degrees) from {os.path.basename(svsPath)}")
            plt.show()

    # Get the number of levels in the slide (resolution levels)
    nLevels = slide.level_count
    # Read the lowest resolution image for display
    lowres_img = np.array(slide.read_region((0, 0), nLevels-1, slide.level_dimensions[nLevels-1]))[:, :, :3]

    # Display the low-resolution image
    plt.imshow(lowres_img)
    plt.title(f"Low-Resolution Image from {os.path.basename(svsPath)}")
    plt.show()
Leave a Comment