Skip to main content

PrefetchReader

A threaded image prefetch reader for efficient batch processing.
from realesrgan.utils import PrefetchReader

reader = PrefetchReader(img_list, num_prefetch_queue=3)
for img in reader:
    # Process image
    pass

Parameters

img_list
list[str]
required
A list of image file paths to be read
num_prefetch_queue
int
required
Number of images to prefetch in the queue

Methods

run()

Starts the prefetch thread. Reads images from the list and puts them in the queue.

next()

Returns the next prefetched image from the queue. Returns: numpy.ndarray - The next image in BGR format Raises: StopIteration when no more images are available

iter()

Returns the iterator object (self).

Usage Example

import glob
from realesrgan.utils import PrefetchReader

# Get list of image paths
img_paths = sorted(glob.glob('inputs/*.png'))

# Create prefetch reader with queue size of 3
reader = PrefetchReader(img_paths, num_prefetch_queue=3)
reader.start()  # Start the prefetch thread

# Iterate through prefetched images
for idx, img in enumerate(reader):
    print(f'Processing image {idx}: {img.shape}')
    # Your processing logic here
The PrefetchReader uses threading to read images asynchronously, which can significantly improve performance when processing large batches of images by overlapping I/O and computation.

IOConsumer

A threaded consumer for asynchronous image writing operations.
from realesrgan.utils import IOConsumer
import queue

write_queue = queue.Queue()
consumer = IOConsumer(opt, write_queue, qid=0)
consumer.start()

Parameters

opt
object
required
Options object containing configuration parameters
que
queue.Queue
required
Queue object for receiving write tasks
qid
int
required
Worker ID for the consumer thread

Methods

run()

Runs the consumer loop. Continuously reads messages from the queue and writes images to disk. Message format:
{
    'output': numpy.ndarray,  # Image to write
    'save_path': str          # Destination path
}
To stop the consumer, send the string 'quit' to the queue.

Usage Example

import queue
import cv2
from realesrgan.utils import IOConsumer

# Create write queue and consumer
write_queue = queue.Queue(maxsize=10)
consumer = IOConsumer(opt, write_queue, qid=0)
consumer.start()

# Process images and queue for async writing
for img_path in image_paths:
    img = cv2.imread(img_path)
    output = process_image(img)  # Your processing
    
    # Queue the write operation
    write_queue.put({
        'output': output,
        'save_path': f'results/{img_path}'
    })

# Signal completion
write_queue.put('quit')
consumer.join()
The IOConsumer allows you to offload disk I/O operations to a separate thread, preventing write operations from blocking your main processing pipeline. This is especially useful when processing large batches of high-resolution images.

Constants

ROOT_DIR

The root directory of the Real-ESRGAN package.
from realesrgan.utils import ROOT_DIR

model_path = os.path.join(ROOT_DIR, 'weights', 'model.pth')
Type: str Value: The directory containing the Real-ESRGAN package (parent directory of realesrgan/)

Build docs developers (and LLMs) love