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)
class ImageEnhancement: 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 improve_quality(self, image): if self.model is None: return image result = self.model.predict(image) result = cv2.resize(result, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_CUBIC) return result
def post(self, request, *args, **kwargs): model = RDN(weights='noise-cancel') img=request.data.get("img") img = (np.array(Image.open(io.BytesIO(img.file.read())))) processed_img_arr = model.predict(np.array(img)) encoded_img = base64.b64encode(processed_img_arr) return Response(encoded_img, status=status.HTTP_201_CREATED)
def fun3(): rdn = RDN(arch_params={'C':6, 'D':20, 'G':64, 'G0':64, 'x':2}) rdn.model.load_weights('weights/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf5') #Run prediction sr_img = rdn.predict(lr_img) Image.fromarray(sr_img) ans=Image.fromarray(sr_img) ss="out3.png" ans.save(ss)
def fun1(): rdn = RDN(arch_params={'C':6, 'D':20, 'G':64, 'G0':64, 'x':2}) rdn.model.load_weights('weights/rdn-C6-D20-G64-G064-x2_PSNR_epoch086.hdf5') #Run prediction sr_img = rdn.predict(lr_img) Image.fromarray(sr_img) ans=Image.fromarray(sr_img) ss="out1.png" ans.save(ss)
def super_resolution(img): # First we initialize the RDN (Recurral neural network model) model = RDN(weights='noise-cancel') # Model is then used to 'predict' the higher resolution image processed_img_arr = model.predict(np.array(img)) im = Image.fromarray(processed_img_arr.astype('uint8'), 'RGB') return im
class RDNDeNoise(): def __init__(self, weights='noise-cancel'): self.weights = weights def __call__(self, img): self.model = RDN(weights=self.weights) pred = self.model.predict(img) del self.model return pred, "<p><b>RDN Denoise</b></p>"
def SR(lr_img: np.array) -> np.array: # rdn = RDN(arch_params=dict(C=6, D=20, G=64, G0=64, x=2)) # rdn.model.load_weights( # '/home/sudongpo/Downloads/image-super-resolution/weights/sample_weights/rdn-C6-D20-G64-G064-x2/PSNR-driven/rdn-C6-D20-G64-G064-x2_PSNR_epoch086.hdf5') rdn = RDN(arch_params=dict(C=3, D=10, G=64, G0=64, x=2)) weights_file = os.path.join( os.path.split(sys.argv[0])[0], 'PSNR-driven/rdn-C3-D10-G64-G064-x2_PSNR_epoch134.hdf5') rdn.model.load_weights(weights_file) sr_img = rdn.predict(lr_img) return sr_img
def upscale_image(img): img = img.convert('RGB') lr_img = np.array(img) assert (lr_img.shape[2] == 3) rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights( 'image-super-resolution/weights/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf5' ) sr_img = rdn.predict(lr_img) return Image.fromarray(sr_img)
def superresolute(lr_img, scale=2): """ Takes an image with low resolution and increases it Input: PIL Image, Output: PIL Image """ assert type(lr_img) == np.ndarray rdn = RDN(weights='psnr-small') for _ in range(scale): lr_img = rdn.predict(lr_img, by_patch_of_size=50) return lr_img
class Superresolution(): def __init__(self, weights='psnr-small'): self.weights = weights def __call__(self, img): self.model = RDN(weights=self.weights) pred = self.model.predict(img) del self.model return pred, "<p><b>Superresolution</b> " + str( img.shape) + " -> " + str(pred.shape) + "</p>"
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)
class upscale_video: def __init__(self, input_filename, remove_noise, zoom, output_filename): self.input_filename = input_filename self.rdn = None # TODO add GAN support https://idealo.github.io/image-super-resolution/#gans-model if remove_noise: self.rdn = RDN(weights='noise-cancel') else: self.rdn = RDN(weights='psnr-large') self.output_filename = output_filename # create a unique dir name to save the updated frames now = datetime.now() dt_string = now.strftime("%d_%m_%Y_%H_%M_%S") self.dirname = f"upscaled_frames_{dt_string}" os.makedirs(self.dirname) self.vidcap = cv2.VideoCapture(self.input_filename) self.fps = self.vidcap.get(cv2.CAP_PROP_FPS) """ Go frame by frame and save the scaled up images into the self.dirname """ def upscale_images_from_video(self): ret, orig_img = self.vidcap.read() count = 0 while ret: sr_img = self.rdn.predict(orig_img) img_filename = f"{count}.jpg" cv2.imwrite(os.path.join(self.dirname, img_filename), sr_img) ret, orig_img = self.vidcap.read() count += 1 """ Use scaled up images and create video with no audio based off it """ def combine_img_dir_into_video(self): video_ffmpeg_script = f"ffmpeg -framerate {int(self.fps)} -i {self.dirname}/%d.jpg no_audio_{self.output_filename}" subprocess.call(video_ffmpeg_script, shell=True) """ Add audio to the super scaled video """ def extract_and_apply_audio(self): apply_audio = audio_extract(self.input_filename, self.output_filename) apply_audio.apply_audio_to_video()
def enhance(images): print('[info] initializing models...') tick = datetime.now() # m1 = RRDN(weights='gans') m2 = RDN(weights='noise-cancel') tock = datetime.now() print( f"[info] complete, time elapsed: {(tock - tick).total_seconds():.1f}s.\n" ) for image in tqdm(images): enhanced = m2.predict(image) # enhanced = m2.predict(enhanced, by_patch_of_size=500) yield enhanced
def run(): """ Funzione per effettuare il super resolution. Prende in input le immagini presenti nella cartella 'superres' e salva il risultato nella cartella ROI """ # Percorso delle immagini originali CWD_PATH = 'superres/' # Percorso di salvataggio ROI_PATH = 'ROI' PATH_TO_ROI = os.path.join(ROI_PATH) import numpy as np from PIL import Image import cv2 from glob import glob # Itero i file nella cartella imgs_paths = sorted(glob('%s/*.jpg' % CWD_PATH)) if not img_paths: raise Exception("Nessuna immagine da scalare") i = 0 for j,img_path in enumerate(imgs_paths): img = Image.open(img_path) lr_img = np.array(img) # Carica il modello Residual Dense Network from ISR.models import RDN rdn = RDN(weights='psnr-large') # Applica due volte il super resolution sr_img = rdn.predict(lr_img) sr_img = rdn.predict(sr_img) # Sfocatura superficie e conversione in scala di grigi sr_img2 = cv2.bilateralFilter(sr_img, 50, 10, 10) sr_img2 = cv2.cvtColor(sr_img2, cv2.COLOR_RGB2GRAY) cv2.imwrite(PATH_TO_ROI + '/filtered_' + str(i) + '.jpg', sr_img2) i += 1
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')
class PythonPredictor: def __init__(self, config): #self.model = RRDN(weights='gans') self.model = RDN(weights='noise-cancel') def predict(self, payload): # read image lr_img = imread(io.BytesIO(base64.b64decode(payload["base64"]))) # run model sr_img = self.model.predict(lr_img) output_image = Image.fromarray(sr_img) # denoise image #output_image.filter(ImageFilter.SMOOTH_MORE) im_file = io.BytesIO() output_image.save(im_file, format="JPEG") im_bytes = im_file.getvalue() # im_bytes: image in binary format. return base64.b64encode(im_bytes).decode("utf-8")
def download(): if request.method == 'POST': f = request.files['file'] img = Image.open(f) lr_img = np.array(img) model = RDN(weights='psnr-large') sr_img = model.predict(lr_img) out = Image.fromarray(sr_img) print("\nOutput resolution =", out.size, "\n") d = 1 while (out.size[1] / d > 400): d += 1 out.save('images/HD_' + f.filename) #Enhanced images are named HD_<filename> return render_template("download.html", name='/images/HD_' + f.filename, y=out.size[0], x=out.size[1], w=out.size[0] / d, h=out.size[1] / d)
# prereqs: # `pip install ISR` ## import sys import numpy as np from PIL import Image input_path = sys.argv[1] out_path = sys.argv[2] img = Image.open(input_path) ## img = img.convert("RGB") # remove alpha channel # https://github.com/idealo/image-super-resolution/issues/151 ## lr_img = np.array(img) ### from ISR.models import RDN # https://github.com/idealo/image-super-resolution#pre-trained-networks # https://github.com/idealo/image-super-resolution/blob/master/notebooks/ISR_Prediction_Tutorial.ipynb model = RDN(weights='psnr-small') sr_img = model.predict(lr_img, by_patch_of_size=100) img_out = Image.fromarray(sr_img) img_out.save(out_path, 'PNG')
def save_img(img: IMG, file_path: str): img.save(file_path) all_img_paths = get_all_img_paths('../image_2') outdir = 'output_isr/' os.makedirs(outdir, exist_ok=True) for i, img_path in enumerate(all_img_paths): try: org_file_name = os.path.basename(img_path)[::-1].replace('.', '__', 1)[::-1] print('processing {}[{}/{}]'.format(org_file_name, i + 1, len(all_img_paths))) img = Image.open(img_path, mode='r') img = img.convert('RGB') img = resize_lr_img(img) img = denoise(img) save_img(img, outdir + "{}_{}.png".format(org_file_name, 'org')) print(img.size) lr_img_array = np.array(img) print('predict with srresnet') sr_img_rdn = Image.fromarray(rdn.predict(lr_img_array)) save_img((sr_img_rdn), outdir + "{}_{}.png".format(org_file_name, 'srr')) print('predict with srgam') sr_img_rrdn = Image.fromarray(rrdn.predict(lr_img_array)) save_img(sr_img_rrdn, outdir + "{}_{}.png".format(org_file_name, 'srg')) save_img(combine_image_horizontally([img, sr_img_rdn, sr_img_rrdn]), outdir + '{}_com.png'.format(org_file_name)) except Exception as e: print(e)
import os import os.path as osp import numpy as np from PIL import Image from ISR.models import RDN if __name__ == '__main__': images_dir = r'E:\Projects\digital_writer\AttnGAN\code\images\2020\March\06' dirs = os.listdir(images_dir) images = [osp.join(images_dir, i, 'coco_g2.png') for i in dirs] rdn = RDN(weights='psnr-small') for img_path in images: print(f'processing {img_path}') img = Image.open(img_path) lr_img = np.array(img) sr_img = rdn.predict(lr_img) sr2_img = rdn.predict(sr_img) im_res = Image.fromarray(sr2_img) im_res.save(osp.join(osp.dirname(img_path), 'coco_g2_big_2.png'), format='png') print(f'done processing {img_path}')
f"[info] complete, time elapsed: {(tock - tick).total_seconds():.1f}s.\n" ) for image in tqdm(images): enhanced = m2.predict(image) # enhanced = m2.predict(enhanced, by_patch_of_size=500) yield enhanced if __name__ == '__main__': print('[info] initializing models...') tick = datetime.now() # m1 = RRDN(weights='gans') m1 = RDN(weights='psnr-large') m2 = RDN(weights='noise-cancel') tock = datetime.now() print( f"[info] complete, time elapsed: {(tock - tick).total_seconds():.1f}s.\n" ) index = 497 folder = f'c{index}' pattern = os.path.join('./exports', folder, '*.jpg') image_paths = glob(pattern) images = [cv2.imread(p) for p in image_paths] for i, origin in enumerate(images): e1 = m1.predict(origin) e2 = m2.predict(e1, by_patch_of_size=400) show_image(origin, e1, e2, col=2, width=1200) break
lr_img = np.array(img) from ISR.models import RDN rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights('weights/rdn-C6-D20-G64-G064-x2_PSNR_epoch086.hdf5') rdn = RDN(arch_params={'C': 3, 'D': 10, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights('weights/rdn-C3-D10-G64-G064-x2_PSNR_epoch134.hdf5') rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights( 'weights/rdn-C6-D20-G64-G064-x2/2019-06-12_16-23/rdn-C6-D20-G64-G064-x2_best-val_generator_PSNR_Y_epoch009.hdf5' ) sr_img = rdn.predict(lr_img) Image.fromarray(sr_img) img.save('data/input/test_images/compressed.jpeg', 'JPEG', dpi=[300, 300], quality=50) compressed_img = Image.open('data/input/test_images/compressed.jpeg') compressed_lr_img = np.array(compressed_img) compressed_img.show() compressed_img.resize(size=(compressed_img.size[0] * 2, compressed_img.size[1] * 2), resample=Image.BICUBIC) rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2})
import numpy as np from PIL import Image from ISR.models import RDN testImage1 = '../../sample-images/t1.jpg' testImage2 = '../../sample-images/t2.jpg' testImage3 = '../../sample-images/t3.jpg' testImage4 = '../../sample-images/t4.jpg' testImage5 = '../../sample-images/t5.jpg' testImage6 = '../../sample-images/t6.jpg' testImage7 = '../../sample-images/t7.jpg' testImage8 = '../../sample-images/t8.jpg' testImages = [ testImage1, testImage2, testImage3, testImage4, testImage5, testImage6, testImage7, testImage8 ] for testImage in testImages: print('Testing on image [' + str(testImage) + ']') inputPath = testImage outputPath = testImage.replace('.jpg', '') + '_up' + '.jpg' imagePil = Image.open(inputPath) imageNumpy = np.array(imagePil) model = RDN(weights='noise-cancel') scaledNumpy = model.predict(imageNumpy) scaledPil = Image.fromarray(scaledNumpy) scaledPil.save(outputPath)
args = vars(ap.parse_args()) IMAGE_PATH = args['image_path'][0] RESOLUTION_DOUBLER = args['resolution_doubler'][0] current_resolution = '' print('argparse arguments: %s\n' % args) print('Image --- %s' % IMAGE_PATH) print('Doubler --- %sx' % RESOLUTION_DOUBLER) for i in range(0, int(RESOLUTION_DOUBLER)): tic = time.time() current_image_path = IMAGE_PATH[:-4] + current_resolution + '.png' print('Current Image', current_image_path) img = Image.open(os.path.join(current_image_path)) lr_img = np.array(img) rdn = RDN(weights='psnr-small') sr_img = rdn.predict(lr_img, by_patch_of_size=50) img_doubled = Image.fromarray(sr_img) current_resolution = str(max(img_doubled.size)) new_image_path = IMAGE_PATH[:-4] + current_resolution + '.png' img_doubled.save(new_image_path) current_image_path = new_image_path toc = time.time() print('Elapsed Time:') print(round((toc - tic) / 60, 2))
# For single image file img_file = '/Users/SawS/Documents/final_year_thesis/sample-images/for-super-resolution/sample-9.png' weight_dir = '/Users/SawS/Documents/final_year_thesis/image-super-resolution/weights/sample_weights/rdn-C6-D20-G64' \ '-G064-x2/ArtefactCancelling/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf5' img_files = glob(os.path.join(img_dir, '*.png')) save_img_dir = '/Users/SawS/Documents/final_year_thesis/sample-images/for-super-resolution/sr-images/' # save_img_dir = '/Users/SawS/Documents/final_year_thesis/sample-images/for-super-resolution/sr-images/results/' # print(img_files) # for img_file in img_files: # img = Image.open(img_file) # lr_img = np.array(img) # lr_img = lr_img[:, :, :3] # rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) # rdn.model.load_weights(weight_dir) # img_name = 'super-resolution-' + img_file.split('/')[-1] # sr_img = rdn.predict(lr_img) # sr_img = Image.fromarray(sr_img) # sr_img.save(save_img_dir + img_name) img = Image.open(img_file) lr_img = np.array(img) lr_img = lr_img[:, :, :3] rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights(weight_dir) img_name = 'super-resolution-' + img_file.split('/')[-1] sr_img = rdn.predict(lr_img) sr_img = Image.fromarray(sr_img) sr_img.save(save_img_dir + img_name)
Bananas = [ f for f in listdir(os.path.join(in_path, 'Banana')) if isfile(join(os.path.join(in_path, 'Banana'), f)) ] Carambolas = [ f for f in listdir(os.path.join(in_path, 'Carambola')) if isfile(join(os.path.join(in_path, 'Carambola'), f)) ] for idx, Banana_path in enumerate(Bananas): Banana_path = os.path.join(in_path, 'Banana', Banana_path) print(str(idx) + " " + Banana_path) src = skimage.io.imread(Banana_path) sr_img_2x = rdn.predict(src) cv2.imwrite( os.path.join(out_path_2x, 'Banana', os.path.basename(Banana_path)), cv2.cvtColor(sr_img_2x, cv2.COLOR_BGR2RGB)) sr_img_4x = rdn.predict(sr_img_2x) cv2.imwrite( os.path.join(out_path_4x, 'Banana', os.path.basename(Banana_path)), cv2.cvtColor(sr_img_4x, cv2.COLOR_BGR2RGB)) if (idx == 0): input() for idx, Carambola_path in enumerate(Carambolas): Carambola_path = os.path.join(in_path, 'Carambola', Carambola_path)
!mkdir -p data/input/test_images !mv *.png data/input/test_images import numpy as np from PIL import Image img = Image.open('data/input/test_images/section8-image.png') img #regular upscaled image img.resize(size=(img.size[0]*4, img.size[1]*4), resample=Image.BICUBIC) #prediction 1 sr_img1 = model1.predict(np.array(img)) Image.fromarray(sr_img1) #prediction 2 sr_img3 = model3.predict(np.array(img)) Image.fromarray(sr_img3) #Final prediction; upscaled image sr_img = model.predict(np.array(img)) Image.fromarray(sr_img) #example 2 !wget https://raw.githubusercontent.com/jbhuang0604/SelfExSR/master/data/Urban100/image_SRF_2/img_001_SRF_2_LR.png
app = Flask(__name__) # 全局化 # carn = CARN_SR() cgclr = changeFace() stgan = demo2.STGAN() rdn = RDN(arch_params={'C': 6, 'D': 20, 'G': 64, 'G0': 64, 'x': 2}) rdn.model.load_weights( 'G:/Deecamp/STGAN_ZW/STGAN/ImageSuperResolution/ArtefactCancelling/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf' ) img = Image.open( 'G:/Deecamp/STGAN_ZW/STGAN/ImageSuperResolution/image/20121.jpg') lr_img = np.array(img) rdn.predict(lr_img) cgclr.getMask(lr_img, 'skin') cgstyle = changestyle() im = cv2.imread("G:/Deecamp/STGAN_ZW/STGAN/transform_folder/fj2.png") body = cgstyle.inference(im, 1) @app.route('/') def hello_world(): return "Hello" @app.route('/changeColor', methods=['POST', 'GET']) # 可能需要用两个函数来处理图片 def changeColor():
import numpy as np from os.path import expanduser from PIL import Image from ISR.models import RDN, RRDN # RDN: psnr-large, psnr-small, noise-cancel # RRDN: gans model = RDN(weights='psnr-large') img = Image.open(expanduser('/home/minhhoang/image-super-resolution/data/input/sample/test.jpg')) lr_img = np.array(img) sr_img = model.predict(lr_img,) output = Image.fromarray(sr_img) output.save(expanduser('data/test_psnr-large.png'))