uruguayai commited on
Commit
79e725b
·
verified ·
1 Parent(s): 34d270e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -21,7 +21,7 @@ def generate_image(prompt):
21
  image = image_model(prompt).images[0]
22
  return image
23
  except Exception as e:
24
- return f"Error generating image: {str(e)}"
25
 
26
  # Face swap function placeholder (replace with actual implementation)
27
  def swap_faces(image1, image2):
@@ -31,54 +31,62 @@ def swap_faces(image1, image2):
31
  def face_swap(image1, image2):
32
  try:
33
  if image1 is None or image2 is None:
34
- return None
35
  image1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2BGR)
36
  image2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2BGR)
37
  swapped_image = swap_faces(image1, image2)
38
- return cv2.cvtColor(swapped_image, cv2.COLOR_BGR2RGB)
39
  except Exception as e:
40
- return f"Error during face swap: {str(e)}"
41
 
42
  # Upscaling function
43
  def upscale_image(image, scale_factor=2):
44
  try:
 
 
45
  image = Image.fromarray(np.array(image))
46
  width, height = image.size
47
  new_size = (int(width * scale_factor), int(height * scale_factor))
48
  upscaled_image = image.resize(new_size, Image.LANCZOS)
49
- return upscaled_image
50
  except Exception as e:
51
- return f"Error during upscaling: {str(e)}"
52
 
53
  # Gradio interface function
54
  def process_image(prompt, image1=None, image2=None, scale_factor=2):
55
  try:
56
  if prompt:
57
  # Generate image from prompt
58
- generated_image = generate_image(prompt)
59
- return generated_image, None, None
 
 
60
  elif image1 and image2:
61
  # Perform faceswap if two images are provided
62
- swapped_image = face_swap(image1, image2)
63
- upscaled_image = upscale_image(swapped_image, scale_factor)
64
- return None, swapped_image, upscaled_image
 
 
 
 
65
  else:
66
- return None, None, None
67
  except Exception as e:
68
- return f"Error in process_image function: {str(e)}", None, None
69
 
70
  # Gradio interface setup
71
  iface = gr.Interface(
72
  fn=process_image,
73
  inputs=[
74
  gr.Textbox(label="Enter your prompt", placeholder="Type your prompt here..."),
75
- gr.Image(label="Image 1 (for faceswap)", type="pil", optional=True),
76
- gr.Image(label="Image 2 (for faceswap)", type="pil", optional=True),
77
  gr.Slider(label="Upscale Factor", minimum=1, maximum=4, step=1, value=2)
78
  ],
79
  outputs=[
80
  gr.Image(label="Output Image"),
81
- gr.Textbox(label="Error Message", placeholder="Error details will appear here...", visible=True)
82
  ],
83
  title="Fooocus Image Processing",
84
  description="Generate images from prompts, swap faces, and upscale images."
 
21
  image = image_model(prompt).images[0]
22
  return image
23
  except Exception as e:
24
+ return None, f"Error generating image: {str(e)}"
25
 
26
  # Face swap function placeholder (replace with actual implementation)
27
  def swap_faces(image1, image2):
 
31
  def face_swap(image1, image2):
32
  try:
33
  if image1 is None or image2 is None:
34
+ return None, "Images for face swap are required"
35
  image1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2BGR)
36
  image2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2BGR)
37
  swapped_image = swap_faces(image1, image2)
38
+ return cv2.cvtColor(swapped_image, cv2.COLOR_BGR2RGB), None
39
  except Exception as e:
40
+ return None, f"Error during face swap: {str(e)}"
41
 
42
  # Upscaling function
43
  def upscale_image(image, scale_factor=2):
44
  try:
45
+ if image is None:
46
+ return None, "Image for upscaling is required"
47
  image = Image.fromarray(np.array(image))
48
  width, height = image.size
49
  new_size = (int(width * scale_factor), int(height * scale_factor))
50
  upscaled_image = image.resize(new_size, Image.LANCZOS)
51
+ return upscaled_image, None
52
  except Exception as e:
53
+ return None, f"Error during upscaling: {str(e)}"
54
 
55
  # Gradio interface function
56
  def process_image(prompt, image1=None, image2=None, scale_factor=2):
57
  try:
58
  if prompt:
59
  # Generate image from prompt
60
+ generated_image, error = generate_image(prompt)
61
+ if error:
62
+ return None, error
63
+ return generated_image, None
64
  elif image1 and image2:
65
  # Perform faceswap if two images are provided
66
+ swapped_image, error = face_swap(image1, image2)
67
+ if error:
68
+ return None, error
69
+ upscaled_image, error = upscale_image(swapped_image, scale_factor)
70
+ if error:
71
+ return None, error
72
+ return upscaled_image, None
73
  else:
74
+ return None, "Either a prompt or two images must be provided"
75
  except Exception as e:
76
+ return None, f"Error in process_image function: {str(e)}"
77
 
78
  # Gradio interface setup
79
  iface = gr.Interface(
80
  fn=process_image,
81
  inputs=[
82
  gr.Textbox(label="Enter your prompt", placeholder="Type your prompt here..."),
83
+ gr.Image(label="Image 1 (for faceswap)", type="pil"),
84
+ gr.Image(label="Image 2 (for faceswap)", type="pil"),
85
  gr.Slider(label="Upscale Factor", minimum=1, maximum=4, step=1, value=2)
86
  ],
87
  outputs=[
88
  gr.Image(label="Output Image"),
89
+ gr.Textbox(label="Error Message", placeholder="Error details will appear here...")
90
  ],
91
  title="Fooocus Image Processing",
92
  description="Generate images from prompts, swap faces, and upscale images."