File size: 7,701 Bytes
0e2fe1b
 
 
 
 
 
 
6fc9e96
0e2fe1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3be0020
a5297b6
04e8f54
5bf17c8
3be0020
5bf17c8
3be0020
5bf17c8
49bd8dc
3be0020
04e8f54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7848c2f
5bf17c8
7848c2f
5bf17c8
7848c2f
5bf17c8
7848c2f
 
04e8f54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
079d739
e8da5f2
 
 
 
 
eb8dafe
32b9d53
 
e8da5f2
 
 
78b7608
e8da5f2
3c58be5
eb8dafe
e8da5f2
 
0e2fe1b
f35256b
079d739
0e2fe1b
 
 
 
 
 
f0d28c5
2e98884
f35256b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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 



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 result(Input,Choice,Step):
      
    if Choice=="Indoor-Coloring":
      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 Choice=="Outdoor-Coloring":
    
      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 =="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(["Indoor-Coloring","Outdoor-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=""" <a href="https://docs.google.com/document/d/19k6dyR5x_hd1M0yoU8i49dlDWvFmtnBT/edit?usp=sharing&ouid=115743073712072785012&rtpof=true&sd=true" download="example.docx"><img src="https://img.icons8.com/external-itim2101-lineal-color-itim2101/64/000000/external-article-blogger-and-influencer-itim2101-lineal-color-itim2101-1.png" alt="Article"></a>""",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)