import cv2 import numpy as np import scipy as sp import scipy.sparse.linalg import gradio as gr def get_image(img): return cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype('double') / 255.0 def neighbours(i, j, max_i, max_j): pairs = [] for n in [-1, 1]: if 0 <= i+n <= max_i: pairs.append((i+n, j)) if 0 <= j+n <= max_j: pairs.append((i, j+n)) return pairs def poisson_sharpening(img, alpha): img_h, img_w = img.shape[:2] img_s = img.copy() im2var = np.arange(img_h * img_w).reshape(img_h, img_w) A = sp.sparse.lil_matrix((img_h*img_w*4*2, img_h*img_w)) b = np.zeros(img_h*img_w*4*2) e = 0 for y in range(img_h): for x in range(img_w): A[e, im2var[y][x]] = 1 b[e] = img_s[y][x] e += 1 for n_y, n_x in neighbours(y, x, img_h-1, img_w-1): A[e, im2var[y][x]] = 1 A[e, im2var[n_y][n_x]] = -1 b[e] = alpha * (img_s[y][x] - img_s[n_y][n_x]) e += 1 A = sp.sparse.csr_matrix(A) v = sp.sparse.linalg.lsqr(A, b)[0] return np.clip(v.reshape(img_h, img_w), 0, 1) def sharpen_image(input_img, alpha): img = get_image(input_img) sharpen_img = np.zeros(img.shape) for b in range(3): sharpen_img[:,:,b] = poisson_sharpening(img[:,:,b], alpha) return (sharpen_img * 255).astype(np.uint8) # Create examples list using the images from the original code examples = [ ["img1.jpg", 9.0], ["img2.PNG", 7.0], ] # Create the Gradio interface iface = gr.Interface( fn=sharpen_image, inputs=[ gr.Image(label="Input Image", type="numpy"), gr.Slider(minimum=1.0, maximum=15.0, step=0.01, value=9.0, label="Sharpening Strength (alpha)") ], outputs=gr.Image(label="Sharpened Image"), title="Poisson Image Sharpening", description="Upload an image or choose from the examples, then adjust the sharpening strength to enhance edges and details.", theme='bethecloud/storj_theme', examples=examples, cache_examples=True ) iface.launch()