Returns the next prefetched image from the queue.Returns:numpy.ndarray - The next image in BGR formatRaises:StopIteration when no more images are available
import globfrom realesrgan.utils import PrefetchReader# Get list of image pathsimg_paths = sorted(glob.glob('inputs/*.png'))# Create prefetch reader with queue size of 3reader = PrefetchReader(img_paths, num_prefetch_queue=3)reader.start() # Start the prefetch thread# Iterate through prefetched imagesfor 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.
import queueimport cv2from realesrgan.utils import IOConsumer# Create write queue and consumerwrite_queue = queue.Queue(maxsize=10)consumer = IOConsumer(opt, write_queue, qid=0)consumer.start()# Process images and queue for async writingfor 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 completionwrite_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.