muneebable commited on
Commit
ff37045
·
verified ·
1 Parent(s): fb8a3f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -17
app.py CHANGED
@@ -8,14 +8,18 @@ import numpy as np
8
  import requests
9
  from io import BytesIO
10
 
11
- def load_image(img_path, max_size=400, shape=None):
12
- ''' Load in and transform an image, making sure the image
13
- is <= 400 pixels in the x-y dims.'''
14
- if "http" in img_path:
15
- response = requests.get(img_path)
16
- image = Image.open(BytesIO(response.content)).convert('RGB')
 
 
 
 
17
  else:
18
- image = Image.open(img_path).convert('RGB')
19
 
20
  # large images will slow down processing
21
  if max(image.size) > max_size:
@@ -97,16 +101,6 @@ def gram_matrix(tensor):
97
 
98
  return gram
99
 
100
- #Load VGG19 model
101
- vgg = models.vgg19(pretrained=True).features
102
- for param in vgg.parameters():
103
- param.requires_grad_(False)
104
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
105
- vgg.to(device)
106
-
107
- # Helper functions (load_image, im_convert, get_features, gram_matrix)
108
- # ... (Include the helper functions you provided earlier here)
109
-
110
  def style_transfer(content_image, style_image, alpha, beta, conv1_1, conv2_1, conv3_1, conv4_1, conv5_1, steps):
111
  content = load_image(content_image).to(device)
112
  style = load_image(style_image, shape=content.shape[-2:]).to(device)
@@ -161,6 +155,14 @@ examples = [
161
  "https://huggingface.co/spaces/muneebable/vgg-style-transfer/resolve/main/assets/style_3.jpg"],
162
  ]
163
 
 
 
 
 
 
 
 
 
164
  # Gradio interface
165
  with gr.Blocks() as demo:
166
  gr.Markdown("# Neural Style Transfer")
 
8
  import requests
9
  from io import BytesIO
10
 
11
+ def load_image(img_input, max_size=400, shape=None):
12
+ if isinstance(img_input, np.ndarray):
13
+ # Convert numpy array to PIL Image
14
+ image = Image.fromarray(img_input.astype('uint8'), 'RGB')
15
+ elif isinstance(img_input, str):
16
+ if "http" in img_input:
17
+ response = requests.get(img_input)
18
+ image = Image.open(BytesIO(response.content)).convert('RGB')
19
+ else:
20
+ image = Image.open(img_input).convert('RGB')
21
  else:
22
+ raise ValueError("Unsupported input type. Expected numpy array or string.")
23
 
24
  # large images will slow down processing
25
  if max(image.size) > max_size:
 
101
 
102
  return gram
103
 
 
 
 
 
 
 
 
 
 
 
104
  def style_transfer(content_image, style_image, alpha, beta, conv1_1, conv2_1, conv3_1, conv4_1, conv5_1, steps):
105
  content = load_image(content_image).to(device)
106
  style = load_image(style_image, shape=content.shape[-2:]).to(device)
 
155
  "https://huggingface.co/spaces/muneebable/vgg-style-transfer/resolve/main/assets/style_3.jpg"],
156
  ]
157
 
158
+ #Load VGG19 model
159
+ vgg = models.vgg19(pretrained=True).features
160
+ for param in vgg.parameters():
161
+ param.requires_grad_(False)
162
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
163
+ vgg.to(device)
164
+
165
+
166
  # Gradio interface
167
  with gr.Blocks() as demo:
168
  gr.Markdown("# Neural Style Transfer")