def predict_sr(filename): if request.method == 'POST': file_path = os.path.join(APP_ROOT, 'static/output') if not os.path.isdir(file_path): os.mkdir(file_path) img = Image.open(os.path.join('static/images/', filename)) #resize LR img.resize(size=(img.size[0] * 4, img.size[1] * 4), resample=Image.BICUBIC) #prediction lr_img = np.array(img) rrdn = RRDN(arch_params={ 'C': 4, 'D': 3, 'G': 32, 'G0': 32, 'x': 4, 'T': 10 }) rrdn.model.load_weights( 'weights/rrdn-C4-D3-G32-G032-T10-x4_epoch299.hdf5') sr_img = rrdn.predict(lr_img) Image.fromarray(sr_img) #save image sr = Image.fromarray(sr_img) sr.save(os.path.join(file_path, filename)) return render_template("result.html", prediction=filename)
def supresol(): inputName = request.form.to_dict()['fileName'] outputName = 'SR_' + inputName.split('.')[0] + '.jpg' # 원래 사진 파일 s3에서 다운로드 downloadFileFromS3('inputImage/superResolution/' + inputName, outputName) # super resolution 하기 # chanel 4 ->3 lr_img = Image.open(outputName).convert("RGB") lr_img_np = np.array(lr_img) print("...load RRDN-GAN...") sr_model = RRDN(weights='gans') sr_img_gan = sr_model.predict(lr_img_np) sr_img_gan = Image.fromarray(sr_img_gan) sr_img_gan.save(outputName) # 후처리된 사진 파일 s3에 업로드 (+서버에선 파일 지움) uploadFileToS3(outputName, 'outputImage/superResolution/' + outputName) os.remove(outputName) # 파일 다운로드 s3 링크 받아오기 url = getUrlFromS3('outputImage/superResolution/' + outputName) # 클라이언트로 링크 전송 return url
def image_super_resolution(file_in_path, file_out_path): rdn = RDN(weights='noise-cancel') rrdn = RRDN(weights='gans') img = imread(file_in_path) lr_img = np.array(img) if lr_img.shape[0] >= 360 or lr_img.shape[1] >= 640: sr_img = rdn.predict(lr_img) else: sr_img = rrdn.predict(lr_img) imsave(file_out_path, sr_img)
def image_deblur(path, filename): img = Image.open(path) model = RRDN(weights='gans') # model = RDN(weights='psnr-small') # model = RDN(weights='psnr-large') # model = RDN(weights='noise-cancel') img.resize(size=(img.size[0] * 4, img.size[1] * 4), resample=Image.BICUBIC) sr_img = model.predict(np.array(img), by_patch_of_size=None, padding_size=2) new = Image.fromarray(sr_img) #output_stream = open(app.config['DOWNLOAD_FOLDER'] + filename, 'wb') tf.keras.backend.clear_session() #output.write(output_stream) new.save(DOWNLOAD_FOLDER + filename, 'JPEG')
def index(): if request.method == 'POST': if request.form['submit'] == 'Go back': return redirect('/') elif request.form['submit'] == 'Download': return redirect('/display/' + final_filename) else: lr_img = np.array(img) model = 'noise-cancel' # You can try various models : # RDN: psnr-large, psnr-small, noise-cancel # RRDN: gans if (model == 'noise-cancel'): rdn = RDN(weights='noise-cancel') elif (model == 'psnr-large'): rdn = RDN(weights='psnr-large') elif (model == 'psnr-small'): rdn = RDN(weights='psnr-small') elif (model == 'gans'): rdn = RRDN(weights='gans') sr_img = rdn.predict(lr_img, by_patch_of_size=patch_size) final_im = Image.fromarray(sr_img) final_im.save(os.path.join(app.config['UPLOAD_FOLDER'], final_filename)) full_filename = os.path.join(app.config['UPLOAD_FOLDER'], final_filename) return render_template('index.html', filename=final_filename)
def setup(opts): print("Checkpoint: ", opts["checkpoint"]) if "C4" in opts['checkpoint']: rdn = RRDN(arch_params={'C':4, 'D':3, 'G':64, 'G0':64, 'T':10, 'x':4}, patch_size=40) elif "C6" in opts["checkpoint"]: rdn = RDN(arch_params={'C':6, 'D':20, 'G':64, 'G0':64, 'x':2}) else: rdn = RDN(arch_params={'C':3, 'D':10, 'G':64, 'G0':64, 'x':2}) rdn.model.load_weights(opts['checkpoint']) return rdn
def prepare_isrgan(weights='psnr-small'): # Currently 4 models are available: - RDN: psnr-large, psnr-small, noise-cancel - RRDN: gans if 'psnr' in weights: from ISR.models import RDN rdn = RDN(weights=weights) elif 'gans' == weights: from ISR.models import RRDN rdn = RRDN(weights=weights) #rdn = RDN(weights='psnr-large') return rdn
def unblur(self, img = None, out = False): if(img == None): img = self.img_blur if(self.log): print('GANS work') model = RRDN(weights='gans') sr_img = model.predict(np.array(img)) if(self.log): print('Scale work') scale_percent = 25 width = int(sr_img.shape[1] * scale_percent / 100) height = int(sr_img.shape[0] * scale_percent / 100) sr_img = cv2.resize(sr_img, (width, height), interpolation = cv2.INTER_NEAREST) self.img_unblur = sr_img if(out): return sr_img
def main(): args = parse_args() input_file = "/input/" + args.input output_file = "/input/" + args.output model = RRDN(weights="gans") if any(t in input_file for t in [".mov", ".mp4", ".mkv"]): run_video(input_file, output_file, model) elif input_file.endswith(".exr"): run_exr(input_file, output_file, model) else: run_image(input_file, output_file, model)
def __init__(self, method): self.method = method print( 'Initialising Super Resolution model of type : {}'.format(method)) if method == None: print( 'No Super Resolution models initalised for method : {}'.format( method)) self.model = None elif method == 'gans': self.model = RRDN(weights='gans') elif method == 'psnr-small': self.model = RDN(weights='psnr-small') elif method == 'psnr-large': self.model = RDN(weights='psnr-large') else: raise Exception( 'Method :{} not valid for ImageEnhancement'.format(method))
def repl_test(): import numpy as np import matplotlib.pyplot as plt from PIL import Image from ISR.models import RDN from ISR.models import RRDN rdn = RDN(weights='psnr-large') rrdn = RRDN(weights='gans') img = Image.open('/home/sean/data/AVSpeech/TUyDankfTsY/TUyDankfTsY_1/63.jpg') lr_img = np.array(img) sr_img = rdn.predict(lr_img, by_patch_of_size=50) test = Image.fromarray(sr_img) f, axarr = plt.subplots(1,2) axarr[0].imshow(img) axarr[1].imshow(test) # plt.show() plt.savefig('foo.png', dpi = 300) test.save('foo.png')
import numpy as np from PIL import Image from ISR.models import RDN, RRDN from util import getRandomStr import os IMAGE_PATH = os.path.dirname(os.path.realpath(__file__)) + "/images" model = RRDN(weights="gans") model.model._make_predict_function() def predict(images): img = Image.open(images) img = img.convert('RGB') lr_img = np.array(img) if (lr_img.shape[0] > 1280 or lr_img.shape[1] > 1280): raise ValueError('Invalid Image Size: width or height < 1280') sr_img = model.predict(lr_img) output = Image.fromarray(sr_img) output_file_path = IMAGE_PATH + getRandomStr(15) + '.jpg' output.save(output_file_path) return output_file_path
help="Input file pattern") parser.add_argument('-out', dest="OUTPUT_FILE", default="output/large_images/%s.png", help="Output file pattern") parser.add_argument('-overwrite', dest="OVERWRITE", action="store_true", help="Overwrite existing images?") a = parser.parse_args() files = getFilenames(a.INPUT_FILE) makeDirectories(a.OUTPUT_FILE) # model = RDN(weights='noise-cancel') model = RRDN(weights='gans') # model = RDN(weights='psnr-small') # model = RDN(weights='psnr-large') for fn in files: basename = getBasename(fn) filenameOut = a.OUTPUT_FILE % basename if not a.OVERWRITE and os.path.isfile(filenameOut): print(f'Already created {filenameOut}') continue img = Image.open(fn) sr_img = model.predict(np.array(img), by_patch_of_size=50) newImg = Image.fromarray(sr_img)
#processing frames with ascending order name i.e. frame0, frame1, frame2 etc. #print(files) for filename in files: #sorted_images = sorted(filename, key=natural_sort_key) if filename.endswith(".jpg"): img = Image.open(directory + filename) lr_img = np.array(img) #model = RDN(weights='psnr-small') model = RRDN(weights='gans') sr_img = model.predict(lr_img) sr_img = Image.fromarray(sr_img) sr_img.save('Upscaled/' + filename) print(filename)
import re import os import imutils import glob from PIL import Image # Create two folders named as "LR_images" and "SR_images" # Put all the low resolution images in LR_images folder and then use GANS to # convert it into High Resolution # Put all converted images into SR_images folder #import GANs model from ISR.models import RRDN #load the pretrained weights - (Trained on DIV2K dataset) model = RRDN(weights='gans') files = [img for img in glob.glob("LR_images/*.jpg")] d=1 for im in files: img = Image.open(im) sr_img = model.predict(np.array(img)) Image.fromarray(sr_img).save("SR_images/%d.jpg"%d) d+=1 # Regular expression and validation of aadhar no. def regex(text): uid = set() newlist = [] for xx in text.split('\n'): newlist.append(xx)
choices=range(0, 1024), help= "Set patch size (default 50px) to hold in memory for magnification. Patching helps to avoid out-of-memory errors but for very high-performing systems you may set this to zero and attempt to process the whole image at once." ) args = parser.parse_args() # Handle user-specified input file inputfile = str(args.inputFile) file_root, file_ext = os.path.splitext(inputfile) print("Input file is {}".format(inputfile)) img = Image.open(inputfile) lr_img = np.array(img) # Handle user-specified model if args.model == 1: model = RRDN(weights='gans') tagout = '_gan' elif args.model == 2: model = RDN(weights='psnr-large') tagout = '_psnr-large' elif args.model == 3: model = RDN(weights='psnr-small') tagout = '_psnr-small' elif args.model == 4: model = RDN(weights='noise-cancel') tagout = '_denoise' else: print("Couldn't resolve model choice to valid value") # Enlarge the image using ISR print("Enlarging {} using {} now\n".format(args.inputFile, tagout))
#Unzip and putting data into dictionary !mkdir div2k !unzip -q DIV2K_valid_LR_bicubic_X2.zip -d div2k !unzip -q DIV2K_train_LR_bicubic_X2.zip -d div2k !unzip -q DIV2K_train_HR.zip -d div2k !unzip -q DIV2K_valid_HR.zip -d div2k #loading the model from ISR.models import RRDN, RDN from ISR.models import Discriminator from ISR.models import Cut_VGG19 model1 = RDN(weights='noise-cancel') model = RRDN(weights='gans') model2 = RDN(weights='psnr-small') model3 = RDN(weights='psnr-large') """ rdn = RDN(arch_params={'C': 5, 'D':16, 'G':48, 'G0':52, 'x':3}) rdn.model.load_weights('PATH/TO/WEIGHTS') """ #setting the perameters lr_train_patch_size = 40 layers_to_extract = [5, 9] scale = 2 hr_train_patch_size = lr_train_patch_size * scale
import numpy as np from PIL import Image from ISR.models import RRDN image_name = "biosphere7_mollweide.1505.tif" img = Image.open('images_test/' + image_name) lr_img = np.array(img) rrdn = RRDN(weights="gans") sr_img = rrdn.predict(lr_img) sr_img_cleaned = Image.fromarray(sr_img) sr_img_cleaned.save("test_output/" + image_name + "_upscaled_RRDN.tif") bicubic_img = img.resize(size=(img.size[0] * 2, img.size[1] * 2), resample=Image.BICUBIC) bicubic_img.save("test_output/" + image_name + "_bicubic.tif") """ To predict on large images and avoid memory allocation errors, use the by_patch_of_size option for the predict method, for instance sr_img = model.predict(image, by_patch_of_size=50) """
from config import video_dir, checkpoint_path, image_size import numpy as np import math import sys import cv2 import glob import shutil import os from ISR.models import RRDN rrdn = RRDN(weights='gans') #rdn = RDN(weights='psnr-small') movie_name = sys.argv[1] fps = int(sys.argv[2]) # frames per second smoothing = float(sys.argv[3]) # number of seconds between images duration = int(sys.argv[4]) # video name videotype = ".mp4" video_name = movie_name + "_ISR" video_path = video_dir + video_name + "_sm" + str(smoothing) + "_dur" + str( duration) + "_fps" + str(fps) + videotype #print(video_path) #create temporary folder to store images temp_dir = "temp_interpImgs"
import sys sys.path.append('N:\\code\\super_resolution\\a-PyTorch-Tutorial-to-Super-Resolution') import os import cv2 as cv from glob import glob from typing import List, Tuple from ISR.models import RDN from ISR.models import RRDN import torch from utils import ImageTransforms device = torch.device("cpu") rdn = RDN(weights='psnr-large') rrdn = RRDN(weights='gans') srgan_checkpoint = "../checkpoint_srgan.pth.tar" srresnet_checkpoint = "../checkpoint_srresnet.pth.tar" # Load model, either the SRResNet or the SRGAN srresnet = torch.load(srresnet_checkpoint)['model'].to(device) srresnet.eval() srgan_generator = torch.load(srgan_checkpoint)['generator'].to(device) srgan_generator.eval() import numpy as np imagenet_mean = torch.FloatTensor([0.485, 0.456, 0.406]).unsqueeze(1).unsqueeze(2) imagenet_std = torch.FloatTensor([0.229, 0.224, 0.225]).unsqueeze(1).unsqueeze(2) from PIL import Image
## https://github.com/idealo/image-super-resolution import os import numpy as np from PIL import Image from ISR.models import RDN from ISR.models import RRDN #rdn = RDN(weights='psnr-large') #rdn_sm = RDN(weights='psnr-small') rdn_nr = RDN(weights='noise-cancel') rrdn = RRDN(weights='gans') START = 34001 END = 35000 #shortened #i0010427.png - i0010511.png #i0011357.png - i0011406.png #i0013204.png - i0013262.png #i0013309.png - i0013707.png #i0013810.png - i0014096.png #i0015870.png - i0016231.png #i0018560.png - i0018651.png #i0020861.png - i0020904.png #i0022620.png - i0022714.png #i0024923.png - i0025433.png #i0028315.png - i0028383.png #i0029318.png - i0029391.png #i0032648.png - i0032962.png #i0033099.png - i0033296.png #i0033414.png - i0033613.png
import sys import os from os.path import join import numpy as np import glob from PIL import Image from ISR.models import RDN, RRDN model_list = [ # RDN(weights='psnr-small'), # RDN(weights='psnr-large'), # RDN(weights='noise-cancel'), '', '', '', RRDN(weights='gans'), ] dataset_list = [ join('data', 'input', 'chest_xray', 'test', 'NORMAL'), join('data', 'input', 'chest_xray', 'test', 'PNEUMONIA'), join('data', 'input', 'chest_xray', 'train', 'NORMAL'), join('data', 'input', 'chest_xray', 'train', 'PNEUMONIA'), join('data', 'input', 'chest_xray', 'val', 'NORMAL'), join('data', 'input', 'chest_xray', 'val', 'PNEUMONIA'), ] result_dir = 'results/chest_xray' resize_list = [ 256,
import numpy as np from PIL import Image img = Image.open('data/DIV2K_train_LR_x8/0009x8.png') lr_img = np.array(img) from ISR.models import RRDN rrdn = RRDN(arch_params={'C': 4, 'D': 3, 'G': 64, 'G0': 64, 'T': 10, 'x': 2}) rrdn.model.load_weights('rrdn_90_500_16_valPSNR_Y.hdf5') sr_img = rrdn.predict(lr_img) tmp = Image.fromarray(sr_img) tmp.save("test2.jpg")
import numpy as np from PIL import Image from ISR.models import RDN, RRDN # Import the image img = Image.open('input.png') # Load the GAN model that will perform a 4x resize model = RRDN(weights='gans') # Convert to numpy image npimg = np.array(img) row,col,ch= npimg.shape # Add some noise mean = 0 var = 0.1 sigma = var**0.5 gauss = np.random.normal(mean,sigma,(row,col,ch))*24 # Reshape and clip the pixel values gauss = gauss.reshape(row,col,ch) noisy = np.clip(npimg + gauss, 0, 255).astype('uint8') noisy_image = Image.fromarray(noisy) # Do the resize big_np = model.predict(np.array(noisy_image)) # Save the image big_img = Image.fromarray(big_np) big_img.save("big.jpg")
def predict(img): lr_img = np.array(img) model = RRDN(weights='gans') sr_img = model.predict(np.array(lr_img)) return (Image.fromarray(sr_img))
create_directory(upload_directory) model_directory = os.environ.get("WEIGHTS_PATH", '/src/weights/') create_directory(model_directory) rdn = RDN(weights='noise-cancel', arch_params={ 'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 1 }) rrdn = RRDN(weights="gans", arch_params={ 'C': 4, 'D': 3, 'G': 32, 'G0': 32, 'x': 1, 'T': 10 }) if check_cuda: print("- cuda: ", tf.test.is_built_with_cuda()) if check_gpu: print("- gpu: ", tf.test.is_gpu_available()) app.run(host=host, port=port, threaded=False, debug=debug)
K.tensorflow_backend._get_available_gpus() configuration = tf.ConfigProto(device_count={'GPU': 1, 'CPU': 56}) sess = tf.Session(config=configuration) K.set_session(sess) lr_train_patch_size = 40 layers_to_extract = [5, 9] scale = 8 hr_train_patch_size = lr_train_patch_size * scale rrdn = RRDN(arch_params={ 'C': 4, 'D': 3, 'G': 64, 'G0': 64, 'T': 10, 'x': scale }, patch_size=lr_train_patch_size) f_ext = Cut_VGG19(patch_size=hr_train_patch_size, layers_to_extract=layers_to_extract) discr = Discriminator(patch_size=hr_train_patch_size, kernel_size=3) ################################################################################################################### #run = wandb.init(project='superres') run = wandb.init(project='respick') config = run.config config.num_epochs = 50
# load all model # model yolo print('[LOADING] Detect model') YOLO_NET = cv2.dnn.readNet(cfg.YOLOV4.YOLO_MODEL_PATH, cfg.YOLOV4.YOLO_CFG_PATH) print('[LOADING SUCESS] Detect model') # model text detct print('[LOADING] Text detecttion model') CRAFT_MODEL = load_model_Craft(CRAFT_CONFIG, NET_CRAFT) print('[LOADING SUCESS] Text detection model') # model regconition print('[LOADING] Text regconition model') DEEPTEXT_MODEL, DEEPTEXT_CONVERTER = load_model_Deeptext(DEEPTEXT_CONFIG) print('[LOADING SUCESS] Text regconition model') print('[LOADING] Super resolution model') super_resolution_model = RRDN(weights='gans') print('[LOADING SUCESS] Super resolution model') def text_recog(cfg, opt, image_path, model, converter): text = 'None' if cfg.PIPELINE.DEEPTEXT: list_image_path = [image_path] for img in list_image_path: text = Deeptext_predict(img, opt, model, converter) elif cfg.PIPELINE.MORAN: text = MORAN_predict(cfg.PIPELINE.MORAN_MODEL_PATH, image_path, MORAN) return text def text_detect_CRAFT(img,