Spaces:
Runtime error
Runtime error
Download app.py from Akshat231/denoise_space: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/spaces/Akshat231/denoise_space/resolve/main/app.py
- Command line
-
hf download hf://spaces/Akshat231/denoise_space/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Akshat231/denoise_space/resolve/main/app.py
1.83 kB
| ## THIS IS FOR DENOISING | |
| import gradio as gr | |
| from PIL import Image | |
| import tensorflow as tf | |
| import tensorflow_hub as hub | |
| import numpy as np | |
| import requests | |
| import cv2 | |
| def resize_image(image, target_dim): | |
| shape = tf.cast(tf.shape(image)[1:-1], tf.float32) | |
| short_dim = min(shape) | |
| scale = target_dim / short_dim | |
| new_shape = tf.cast(shape * scale, tf.int32) | |
| image = tf.image.resize(image, new_shape) | |
| image = tf.image.resize_with_crop_or_pad(image, target_dim, target_dim) | |
| return image | |
| def process_image(image_path, target_dim=256): | |
| input_img = cv2.cvtColor(image_path, cv2.COLOR_BGR2RGB) | |
| input_img=np.asarray(input_img,np.float32)/255.0 | |
| input_img = tf.expand_dims(input_img, axis=0) | |
| input_img = resize_image(input_img, target_dim) | |
| return input_img | |
| def infer(image_path: str, model: tf.keras.Model, input_resolution=(256, 256)): | |
| preprocessed_image = process_image(image_path, input_resolution[0]) | |
| preds = model.predict(preprocessed_image) | |
| if isinstance(preds, list): | |
| preds = preds[-1] | |
| if isinstance(preds, list): | |
| preds = preds[-1] | |
| preds = np.array(preds[0], np.float32) | |
| final_pred_image = np.array((np.clip(preds, 0.0, 1.0)).astype(np.float32)) | |
| return final_pred_image | |
| input_resolution = (256,256) | |
| def get_model(model_url: str, input_resolution: tuple) -> tf.keras.Model: | |
| inputs = tf.keras.Input((*input_resolution, 3)) | |
| hub_module = hub.KerasLayer(model_url) | |
| outputs = hub_module(inputs) | |
| return tf.keras.Model(inputs, outputs) | |
| denoise_model = get_model('./denoise_graph', input_resolution) | |
| def predict_image(image): | |
| return infer(image,denoise_model,input_resolution) | |
| image=gr.inputs.Image() | |
| irface=gr.Interface(fn=predict_image, inputs=image, outputs=image,interpretation='default') | |
| irface.launch() |