cle4rview commited on
Commit
062b080
·
1 Parent(s): b92847a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -23
app.py CHANGED
@@ -46,7 +46,7 @@ def realesrgan(img, model_name, denoise_strength, outscale):
46
  'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth'
47
  ]
48
 
49
- # Determine model paths
50
  model_path = os.path.join('weights', model_name + '.pth')
51
  if not os.path.isfile(model_path):
52
  ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -55,7 +55,7 @@ def realesrgan(img, model_name, denoise_strength, outscale):
55
  model_path = load_file_from_url(
56
  url=url, model_dir=os.path.join(ROOT_DIR, 'weights'), progress=True, file_name=None)
57
 
58
- # Use dni to control the denoise strength
59
  dni_weight = None
60
  if model_name == 'realesr-general-x4v3' and denoise_strength != 1:
61
  wdn_model_path = model_path.replace('realesr-general-x4v3', 'realesr-general-wdn-x4v3')
@@ -78,7 +78,7 @@ def realesrgan(img, model_name, denoise_strength, outscale):
78
 
79
 
80
 
81
- # Convert the input PIL image to cv2 image, so that it can be processed by realesrgan
82
  cv_img = numpy.array(img)
83
  img = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
84
 
@@ -103,17 +103,14 @@ def realesrgan(img, model_name, denoise_strength, outscale):
103
 
104
 
105
  def rnd_string(x):
106
- """Returns a string of 'x' random characters
107
- """
108
  characters = "abcdefghijklmnopqrstuvwxyz_0123456789"
109
  result = "".join((random.choice(characters)) for i in range(x))
110
  return result
111
 
112
 
113
  def reset():
114
- """Resets the Image components of the Gradio interface and deletes
115
- the last processed image
116
- """
117
  global last_file
118
  if last_file:
119
  print(f"Deleting {last_file} ...")
@@ -123,14 +120,7 @@ def reset():
123
 
124
 
125
  def has_transparency(img):
126
- """This function works by first checking to see if a "transparency" property is defined
127
- in the image's info -- if so, we return "True". Then, if the image is using indexed colors
128
- (such as in GIFs), it gets the index of the transparent color in the palette
129
- (img.info.get("transparency", -1)) and checks if it's used anywhere in the canvas
130
- (img.getcolors()). If the image is in RGBA mode, then presumably it has transparency in
131
- it, but it double-checks by getting the minimum and maximum values of every color channel
132
- (img.getextrema()), and checks if the alpha channel's smallest value falls below 255.
133
- https://stackoverflow.com/questions/43864101/python-pil-check-if-image-is-transparent
134
  """
135
  if img.info.get("transparency", None) is not None:
136
  return True
@@ -147,8 +137,8 @@ def has_transparency(img):
147
 
148
 
149
  def image_properties(img):
150
- """Returns the dimensions (width and height) and color mode of the input image and
151
- also sets the global img_mode variable to be used by the realesrgan function
152
  """
153
  global img_mode
154
  if img:
@@ -170,7 +160,6 @@ def main():
170
 
171
 
172
  <div align="center"> Documentation will be added soon. </div>
173
- <div align="center"> Big cheh a Taha </div>
174
  """
175
  )
176
 
@@ -201,15 +190,13 @@ def main():
201
  inputs=[input_image, model_name, denoise_strength, outscale],
202
  outputs=output_image)
203
  reset_btn.click(fn=reset, inputs=[], outputs=[output_image, input_image])
204
- # reset_btn.click(None, inputs=[], outputs=[input_image], _js="() => (null)\n")
205
- # Undocumented method to clear a component's value using Javascript
206
 
207
  gr.Markdown(
208
- """ <br<<br><br><div align="center">Models need more training, so the upscaling might yield some artifacting and or smearing like effects. </div>
209
  """
210
  )
211
 
212
- demo.launch(share=False,show_api=True)
213
 
214
 
215
  if __name__ == "__main__":
 
46
  'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth'
47
  ]
48
 
49
+ # Determine model paths (local loading in other project, try and merge ?)
50
  model_path = os.path.join('weights', model_name + '.pth')
51
  if not os.path.isfile(model_path):
52
  ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
 
55
  model_path = load_file_from_url(
56
  url=url, model_dir=os.path.join(ROOT_DIR, 'weights'), progress=True, file_name=None)
57
 
58
+ # denoiser control
59
  dni_weight = None
60
  if model_name == 'realesr-general-x4v3' and denoise_strength != 1:
61
  wdn_model_path = model_path.replace('realesr-general-x4v3', 'realesr-general-wdn-x4v3')
 
78
 
79
 
80
 
81
+ # TO cv2 conversion
82
  cv_img = numpy.array(img)
83
  img = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
84
 
 
103
 
104
 
105
  def rnd_string(x):
106
+
 
107
  characters = "abcdefghijklmnopqrstuvwxyz_0123456789"
108
  result = "".join((random.choice(characters)) for i in range(x))
109
  return result
110
 
111
 
112
  def reset():
113
+
 
 
114
  global last_file
115
  if last_file:
116
  print(f"Deleting {last_file} ...")
 
120
 
121
 
122
  def has_transparency(img):
123
+ """Alpha channel checking
 
 
 
 
 
 
 
124
  """
125
  if img.info.get("transparency", None) is not None:
126
  return True
 
137
 
138
 
139
  def image_properties(img):
140
+ """
141
+ Dimensions, (A)RGB
142
  """
143
  global img_mode
144
  if img:
 
160
 
161
 
162
  <div align="center"> Documentation will be added soon. </div>
 
163
  """
164
  )
165
 
 
190
  inputs=[input_image, model_name, denoise_strength, outscale],
191
  outputs=output_image)
192
  reset_btn.click(fn=reset, inputs=[], outputs=[output_image, input_image])
 
 
193
 
194
  gr.Markdown(
195
+ """ <br><br><br><div align="center">Models need more training, so the upscaling might yield some artifacting and or smearing like effects. </div>
196
  """
197
  )
198
 
199
+ demo.launch(share=False,show_api=False)
200
 
201
 
202
  if __name__ == "__main__":