chenyingxu commited on
Commit
6da67e3
·
verified ·
1 Parent(s): 048b27d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -29
app.py CHANGED
@@ -4,8 +4,6 @@ import torch.nn as nn
4
  import gradio as gr
5
  import cv2
6
  import os
7
- import requests
8
- from io import BytesIO
9
  from PIL import Image
10
  from PIL import ImageEnhance
11
  import torchvision.transforms as transforms
@@ -101,25 +99,10 @@ model2 = Generator(3, 1, 3) # Initialize the model architecture
101
  model2.load_state_dict(torch.load('model2.pth', map_location=torch.device('cpu')))
102
  model2.eval() # Set the model to evaluation mode
103
 
104
-
105
-
106
-
107
  # 🖼️ Function to process the image and create line drawing
108
  def predict(input_img, ver):
109
- # 检查输入是否是 URL
110
- if isinstance(input_img, str) and input_img.startswith("http"):
111
- # 下载远程图片
112
- response = requests.get(input_img, timeout=10)
113
- response.raise_for_status()
114
- original_img = Image.open(BytesIO(response.content))
115
- else:
116
- # 处理本地路径
117
- if not os.path.exists(input_img):
118
- return f"文件路径无效:{input_img}"
119
- original_img = Image.open(input_img)
120
-
121
-
122
-
123
  original_size = original_img.size
124
 
125
  # Define the transformation pipeline
@@ -152,15 +135,9 @@ def predict(input_img, ver):
152
 
153
  # Optionally enhance the brightness slightly if the image is still too dark
154
  brightness_enhancer = ImageEnhance.Brightness(output_img)
155
- output_img = brightness_enhancer.enhance(1.0)
156
-
157
- # 使用临时文件保存生成的图像
158
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
159
- output_img.save(tmp_file, format='PNG')
160
- tmp_file_path = tmp_file.name # 获取临时文件路径
161
-
162
- return tmp_file_path # 返回临时文件路径
163
 
 
164
 
165
  # 📝 Title for the Gradio interface
166
  title="🖌️ Image to Simple and Complex Artistic Drawings"
@@ -177,10 +154,10 @@ for file in os.listdir(image_dir):
177
  iface = gr.Interface(
178
  fn=predict,
179
  inputs=[
180
- gr.Textbox(label="输入图片路径或 URL", placeholder="支持本地文件路径或 URL"),
181
  gr.Radio(['Complex Lines', 'Simple Lines'], label='version', value='Simple Lines')
182
  ],
183
- outputs=gr.File(label="下载生成的图片"),
184
  title=title,
185
  examples=examples
186
  )
 
4
  import gradio as gr
5
  import cv2
6
  import os
 
 
7
  from PIL import Image
8
  from PIL import ImageEnhance
9
  import torchvision.transforms as transforms
 
99
  model2.load_state_dict(torch.load('model2.pth', map_location=torch.device('cpu')))
100
  model2.eval() # Set the model to evaluation mode
101
 
 
 
 
102
  # 🖼️ Function to process the image and create line drawing
103
  def predict(input_img, ver):
104
+ # Open the image and get its original size
105
+ original_img = Image.open(input_img)
 
 
 
 
 
 
 
 
 
 
 
 
106
  original_size = original_img.size
107
 
108
  # Define the transformation pipeline
 
135
 
136
  # Optionally enhance the brightness slightly if the image is still too dark
137
  brightness_enhancer = ImageEnhance.Brightness(output_img)
138
+ output_img = brightness_enhancer.enhance(1.0) # Keep brightness the same, adjust if needed
 
 
 
 
 
 
 
139
 
140
+ return output_img
141
 
142
  # 📝 Title for the Gradio interface
143
  title="🖌️ Image to Simple and Complex Artistic Drawings"
 
154
  iface = gr.Interface(
155
  fn=predict,
156
  inputs=[
157
+ gr.Image(type='filepath'),
158
  gr.Radio(['Complex Lines', 'Simple Lines'], label='version', value='Simple Lines')
159
  ],
160
+ outputs=gr.Image(type="pil"),
161
  title=title,
162
  examples=examples
163
  )