Skip to content

Instantly share code, notes, and snippets.

@AlexanderProd
Created March 19, 2023 08:22
Show Gist options
  • Save AlexanderProd/1f2823bcf47bd902aa48b2fa818ceedd to your computer and use it in GitHub Desktop.
Save AlexanderProd/1f2823bcf47bd902aa48b2fa818ceedd to your computer and use it in GitHub Desktop.
Uses chunks to upsample an image which reduces the required memory, especially helpful with very memory intensive models like EDSR.
import cv2
import numpy as np
import time
# Load the input image
input_image = cv2.imread('example.jpg')
# Define the size of the chunks
chunk_size = (200, 200)
# Create a DNN super-resolution object
sr = cv2.dnn_superres.DnnSuperResImpl_create()
sr.readModel('models/EDSR_x4.pb')
sr.setModel('edsr', 4)
sr.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
sr.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
start = time.time()
# Compute the new image size based on the chunk size and scale factor
new_size = (input_image.shape[1] * 4, input_image.shape[0] * 4)
# Create an empty output image with the same number of channels as the input image
output_image = np.zeros((new_size[1], new_size[0], input_image.shape[2]), dtype=np.uint8)
# Loop over the image chunks
for y in range(0, input_image.shape[0], chunk_size[1]):
for x in range(0, input_image.shape[1], chunk_size[0]):
# Extract the chunk from the input image
chunk = input_image[y:y+chunk_size[1], x:x+chunk_size[0], :]
# Upsample the chunk
upsampled_chunk = sr.upsample(chunk)
# Insert the upscaled chunk into the output image
output_image[y*4:(y+chunk_size[1])*4, x*4:(x+chunk_size[0])*4, :] = upsampled_chunk
end = time.time()
print("INFO: super resolution took {:.6f} seconds".format(end-start))
# Display the output image
cv2.imshow('Upscaled image', output_image)
cv2.imwrite('upscaled.jpg', output_image)
cv2.waitKey()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment