rmayormartins commited on
Commit
47cc597
1 Parent(s): eab0c71

Descrição das mudanças

Browse files
Files changed (2) hide show
  1. app.py +32 -5
  2. requirements.txt +1 -0
app.py CHANGED
@@ -2,6 +2,7 @@ import tensorflow as tf
2
  import tensorflow_hub as hub
3
  import numpy as np
4
  import gradio as gr
 
5
 
6
  # Carrega o modelo de transferência de estilo pré-treinado
7
  style_transfer_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
@@ -13,13 +14,34 @@ def load_image(image):
13
  image = image[..., :3]
14
  return image
15
 
16
- def style_transfer(content_image, style_image):
 
 
 
 
 
 
 
 
 
 
17
  # Processa as imagens
18
  content_image = load_image(content_image)
19
  style_image = load_image(style_image)
20
 
 
 
 
 
21
  # Executa a transferência de estilo
22
- stylized_image = style_transfer_model(tf.constant(content_image), tf.constant(style_image))[0]
 
 
 
 
 
 
 
23
 
24
  # Converte a imagem resultante para o formato correto
25
  stylized_image = np.array(stylized_image * 255, np.uint8)
@@ -30,8 +52,13 @@ def style_transfer(content_image, style_image):
30
 
31
  iface = gr.Interface(
32
  fn=style_transfer,
33
- inputs=["image", "image"],
34
- outputs="image"
 
 
 
 
 
35
  )
36
 
37
- iface.launch()
 
2
  import tensorflow_hub as hub
3
  import numpy as np
4
  import gradio as gr
5
+ import cv2
6
 
7
  # Carrega o modelo de transferência de estilo pré-treinado
8
  style_transfer_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
 
14
  image = image[..., :3]
15
  return image
16
 
17
+ def interpolate_images(baseline, target, alpha):
18
+ return baseline + alpha * (target - baseline)
19
+
20
+ def apply_sharpness(image, intensity):
21
+ kernel = np.array([[0, -intensity, 0],
22
+ [-intensity, 1 + 4 * intensity, -intensity],
23
+ [0, -intensity, 0]])
24
+ sharp_image = cv2.filter2D(image, -1, kernel)
25
+ return np.clip(sharp_image, 0, 255)
26
+
27
+ def style_transfer(content_image, style_image, style_density, content_sharpness):
28
  # Processa as imagens
29
  content_image = load_image(content_image)
30
  style_image = load_image(style_image)
31
 
32
+ # Aplica nitidez na imagem de conteúdo antes da transferência de estilo
33
+ content_image_sharp = apply_sharpness(content_image[0], intensity=content_sharpness)
34
+ content_image_sharp = content_image_sharp[np.newaxis, ...]
35
+
36
  # Executa a transferência de estilo
37
+ stylized_image = style_transfer_model(tf.constant(content_image_sharp), tf.constant(style_image))[0]
38
+
39
+ # Interpola entre a imagem de conteúdo e a imagem estilizada para densidade de estilo
40
+ stylized_image = interpolate_images(
41
+ baseline=content_image[0],
42
+ target=stylized_image.numpy(),
43
+ alpha=style_density
44
+ )
45
 
46
  # Converte a imagem resultante para o formato correto
47
  stylized_image = np.array(stylized_image * 255, np.uint8)
 
52
 
53
  iface = gr.Interface(
54
  fn=style_transfer,
55
+ inputs=[
56
+ gr.Image(label="Content Image"), # Imagem de conteúdo
57
+ gr.Image(label="Style Image"), # Imagem de estilo
58
+ gr.Slider(minimum=0, maximum=1, value=0.5, label="Adjust Style Density"),
59
+ gr.Slider(minimum=0, maximum=1, value=0.5, label="Content Sharpness")
60
+ ],
61
+ outputs=gr.Image(label="Stylized Image")
62
  )
63
 
64
+ iface.launch()
requirements.txt CHANGED
@@ -2,5 +2,6 @@ gradio
2
  tensorflow-hub
3
  numpy
4
  tensorflow
 
5
 
6
 
 
2
  tensorflow-hub
3
  numpy
4
  tensorflow
5
+ opencv-python-headless
6
 
7