import tensorflow as tf import os import pathlib import time import datetime from matplotlib import pyplot as plt import numpy as np from cv2 import cv2 import math from tensorflow import keras from tensorflow.keras.models import * from tensorflow.keras.layers import * from tensorflow.keras.optimizers import * def color_imread(path): img = cv2.imread(path) img = cv2.cvtColor(img , cv2.COLOR_BGR2RGB) img = (img/127.5) - 1 img = img.astype(np.float32) return img def gray_imread(path): img = cv2.imread(path) img = cv2.cvtColor(img ,cv2.COLOR_BGR2GRAY) img = img.astype(np.float32) return img def reshape(gray_img): gray_img = np.asarray(gray_img) gray_img = gray_img.reshape(256,256,1) return gray_img array_Gen_loss=[] def histogram_graphic(img): hist,bins = np.histogram(img.flatten(),256,[0,256]) cdf = hist.cumsum() cdf_normalized = cdf * float(hist.max()) / cdf.max() plt.plot(cdf_normalized, color = 'b') plt.hist(img.flatten(),256,[0,256], color = 'r') plt.xlim([0, 230]) plt.legend(('cdf','histogram'), loc = 'upper left') plt.show() def preprocessing(path): img = cv2.imread(path) img = np.asarray(img).reshape(256,256,3) #print(img.shape) #cv2.imshow(img) #cv2.imwrite("/content/drive/MyDrive/ColabNotebooks/enhance/Before_hist_equalizer.png",img) #Işık ayarı hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #hsv formatında gerekiyor hue, sat, val = cv2.split(hsv) mid = 0.5 mean = np.mean(val) gamma = math.log(mid*255)/math.log(mean) #print("Gamma:",gamma) #Çıkan gamma değerine göre ters işlem uygulayacak def image_colorfulness(image): # split the image into its respective RGB components (B, G, R) = cv2.split(image.astype("float")) # compute rg = R - G rg = np.absolute(R - G) # compute yb = 0.5 * (R + G) - B yb = np.absolute(0.5 * (R + G) - B) # compute the mean and standard deviation of both `rg` and `yb` (rbMean, rbStd) = (np.mean(rg), np.std(rg)) (ybMean, ybStd) = (np.mean(yb), np.std(yb)) # combine the mean and standard deviations stdRoot = np.sqrt((rbStd ** 2) + (ybStd ** 2)) meanRoot = np.sqrt((rbMean ** 2) + (ybMean ** 2)) # derive the "colorfulness" metric and return it return stdRoot + (0.3 * meanRoot) # sınırı 24 from PIL import Image, ImageEnhance def add_saturation(path): clr = cv2.imread(path) value = image_colorfulness(clr) print(value) img = Image.open(path) enhanced_obj = ImageEnhance.Color(img) if value<30 : #renk doygunluğu iyi durumda çıkanları da bir miktar arttırmak için sınırı 30 yapıyoruz enhanced_obj.enhance((30-value)*0.1 + 0.75).save("enhance/deneme_sat.jpg") #add_saturation("/content/drive/MyDrive/ColabNotebooks/enhance/cikti2.jpeg") def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0): """Return a sharpened version of the image, using an unsharp mask.""" blurred = cv2.GaussianBlur(image, kernel_size, sigma) sharpened = float(amount + 1) * image - float(amount) * blurred sharpened = np.maximum(sharpened, np.zeros(sharpened.shape)) sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape)) sharpened = sharpened.round().astype(np.uint8) if threshold > 0: low_contrast_mask = np.absolute(image - blurred) < threshold np.copyto(sharpened, image, where=low_contrast_mask) return sharpened def example(image,name): sharpened_image = unsharp_mask(image) cv2.imwrite(name, sharpened_image) def ssim_psnr(pre,target): ssim_res = ssim(pre,target) psnr_res = psnr(pre,target) ssim_results.append(ssim_res) psnr_results.append(ssim_results) def alexnet(pretrained_weights = None,input_size = (256,256,3)): model = Sequential() model.add(Conv2D(input_shape=input_size, filters= 512, kernel_size =(11,11) ,strides=(4,4), activation = keras.layers.LeakyReLU(alpha=0.01))) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2,2))) model.add(Conv2D(filters= 256, kernel_size =(5,5) ,strides=(2,2), activation = keras.layers.LeakyReLU(alpha=0.01) , padding='same')) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2,2))) model.add(Conv2D(filters= 128, kernel_size =(3,3) , activation = keras.layers.LeakyReLU(alpha=0.01) , padding='same')) model.add(Conv2D(filters= 32, kernel_size =(3,3) , activation = keras.layers.LeakyReLU(alpha=0.01) , padding='same')) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2,2))) model.add(Flatten()) model.add(Dense(4096 , activation = keras.layers.LeakyReLU(alpha=0.01))) model.add(Dropout(0.3)) model.add(Dense(4096 , activation = keras.layers.LeakyReLU(alpha=0.01))) model.add(Dropout(0.5)) model.add(Dense(256 , activation = keras.layers.LeakyReLU(alpha=0.01))) model.add(Dropout(0.3)) model.add(Dense(2 , activation='softmax')) return model def result(Input,Choice,Step): if Choice=="Place-Coloring": model = alexnet() model.load_weights('indoor_outdoor.h5') image = cv2.cvtColor(Input,cv2.COLOR_BGR2RGB) image = np.array(image).reshape(-1,256,256,3) pred = model.predict(image) result = np.argmax(pred, axis=1) if int(result[0]) == 1: if Step == 1.0: pre_trained = tf.keras.models.load_model("indoor_1.h5") if Step == 2.0: pre_trained = tf.keras.models.load_model("indoor_2.h5") if Step == 3.0: pre_trained = tf.keras.models.load_model("indoor_3.h5") size0 = Input.shape[0] size1 = Input.shape[1] start = Input Input = cv2.resize(Input, (256,256), interpolation = cv2.INTER_AREA) Input = cv2.cvtColor(Input , cv2.COLOR_BGR2GRAY) Input = np.array(Input).reshape(1,256,256,1) prediction = pre_trained(Input,training=True) Input = prediction[0] Input = (Input+1)*127.5 Input = np.uint8(Input) Input = cv2.resize(Input, (size1,size0), interpolation = cv2.INTER_AREA) finish = Input mse = np.mean((start - finish) ** 2) MAX = np.iinfo(start.dtype).max if mse == 0: Psnr = 100 else: Psnr = 20 * math.log10(MAX / math.sqrt(mse)) return Input,Psnr if int(result[0]) == 0: if Step == 1.0: pre_trained = tf.keras.models.load_model("outdoor_1.h5") if Step == 2.0: pre_trained = tf.keras.models.load_model("outdoor_2.h5") if Step == 3.0: pre_trained = tf.keras.models.load_model("outdoor_3.h5") size0 = Input.shape[0] size1 = Input.shape[1] start = Input Input = cv2.resize(Input, (256,256), interpolation = cv2.INTER_AREA) Input = cv2.cvtColor(Input , cv2.COLOR_BGR2GRAY) Input = np.array(Input).reshape(1,256,256,1) prediction = pre_trained(Input,training=True) Input = prediction[0] Input = (Input+1)*127.5 Input = np.uint8(Input) Input = cv2.resize(Input, (size1,size0), interpolation = cv2.INTER_AREA) finish = Input mse = np.mean((start - finish) ** 2) MAX = np.iinfo(start.dtype).max if mse == 0: Psnr = 100 else: Psnr = 20 * math.log10(MAX / math.sqrt(mse)) return Input,Psnr if Choice=="Face-Coloring": if Step == 1.0: pre_trained = tf.keras.models.load_model("face_1.h5") if Step == 2.0: pre_trained = tf.keras.models.load_model("face_2.h5") if Step == 3.0: pre_trained = tf.keras.models.load_model("face_3.h5") size0 = Input.shape[0] size1 = Input.shape[1] start = Input Input = cv2.resize(Input, (256,256), interpolation = cv2.INTER_AREA) Input = cv2.cvtColor(Input , cv2.COLOR_BGR2GRAY) Input = np.array(Input).reshape(1,256,256,1) prediction = pre_trained(Input,training=True) Input = prediction[0] Input = (Input+1)*127.5 Input = np.uint8(Input) Input = cv2.resize(Input, (size1,size0), interpolation = cv2.INTER_AREA) finish = Input mse = np.mean((start - finish) ** 2) MAX = np.iinfo(start.dtype).max if mse == 0: Psnr = 100 else: Psnr = 20 * math.log10(MAX / math.sqrt(mse)) return Input,Psnr if Choice =="Enhancement": if Step == 1.0 or Step == 2.0 or Step == 3.0: pre_trained2 = tf.keras.models.load_model("generatorLR-HR_300.h5") size0 = Input.shape[0] size1 = Input.shape[1] Input = cv2.resize(Input, (256,256), interpolation = cv2.INTER_AREA) Input = cv2.cvtColor(Input ,cv2.COLOR_BGR2RGB) Input = (Input/127.5) - 1 Input = Input.astype(np.float32) Input = np.array(Input).reshape(1,256,256,3) prediction = pre_trained2(Input,training=True) Input = prediction[0] Input = (Input+1)*127.5 Input = np.uint8(Input) Input = cv2.resize(Input, (size1,size0), interpolation = cv2.INTER_AREA) Input = cv2.cvtColor(Input ,cv2.COLOR_BGR2RGB) Psnr = 50 return Input, Psnr #lst = cv2.imread('/content/drive/MyDrive/ColabNotebooks/enhance/low-sat.jpg') #r = result(lst) #cv2.imshow(r) import gradio as gr iface = gr.Interface(fn=result, inputs=[gr.inputs.Image(type="numpy",image_mode="RGB"),gr.inputs.Radio(["Place-Coloring","Enhancement", "Face-Coloring","Repair"]),gr.inputs.Slider(minimum=1.0,maximum=3.0,default=3.0,step=1.0)], outputs=[gr.outputs.Image( type="auto", label="Output"),gr.outputs.Textbox(type="number",label="Psnr Between Input and Output")],theme="grass", live=True ,css=""" body {background-color: rgba(127,191,63,0.48)} """,title="Colorization and Enhancement of Old Images",article=""" Article""",examples=[["indoor.png","Indoor-Coloring",3.0],["indoor_10468.png","Indoor-Coloring",3.0],["outdoor_46.png","Outdoor-Coloring",3.0],["outdoor_1755.png","Outdoor-Coloring",3.0]]) iface.launch(debug="True",show_tips="True",inbrowser=True)