Muhammad Rama Nurimani commited on
Commit
2d90c94
·
1 Parent(s): 9f795ae
Files changed (1) hide show
  1. app.py +10 -26
app.py CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
4
  import gradio as gr
5
 
6
  def save_image(image, path):
 
7
  image.save(path)
8
 
9
  def load_image(path):
@@ -12,12 +13,11 @@ def load_image(path):
12
  def process_image(input_image):
13
  input_path = "./datasets/data/test/input_image.png"
14
  output_dir = "./results/demo/color_pix2pix/test_latest"
15
- output_image_path = os.path.join(output_dir, "images", "fake_B.png")
16
 
17
  # Save the input image
18
  save_image(input_image, input_path)
19
 
20
-
21
  cmd = [
22
  "python", "test.py", # Command to run the test script
23
  "--dataroot", "./datasets/data", # Adjust path as needed
@@ -33,42 +33,26 @@ def process_image(input_image):
33
  # Execute the command to process the image
34
  subprocess.run(cmd, check=True)
35
  except subprocess.CalledProcessError as e:
36
- print(f"Error while running command: {e}")
37
- return None
38
 
39
  # Check if the output directory exists
40
  if not os.path.exists(output_dir):
41
- print(f"Error: Output directory {output_dir} does not exist.")
42
- return None
43
 
44
  # After processing, load the output image from the results directory
45
  output_files = [f for f in os.listdir(os.path.join(output_dir, "images")) if f.endswith('fake_B_rgb.png')]
46
- print(output_files)
47
  if not output_files:
48
- print(f"Error: No output files found in {os.path.join(output_dir, 'images')}.")
49
- return None
50
 
51
- print(output_files)
52
-
53
- newest_file = max(output_files, key=lambda f: os.path.getctime(os.path.join(output_dir, "images", f)))
54
- output_image_path = os.path.join(output_dir, "images", newest_file)
55
- if os.path.exists(output_image_path):
56
- print(f"Output image saved at {output_image_path}")
57
- return load_image(output_image_path)
58
- else:
59
- print(f"Error: Output image {output_image_path} not found.")
60
- return None
61
 
62
- # Define the Gradio interface
63
  iface = gr.Interface(
64
  fn=process_image,
65
  inputs=gr.Image(type="pil"),
66
  outputs=gr.Image(type="pil"),
67
- live=True,
68
- title="Pix2Pix Colorization",
69
- description="Upload an image, which will be processed using Pix2Pix model and the output will be displayed."
70
  )
71
 
72
-
73
- # Launch the app
74
- iface.launch()
 
4
  import gradio as gr
5
 
6
  def save_image(image, path):
7
+ os.makedirs(os.path.dirname(path), exist_ok=True)
8
  image.save(path)
9
 
10
  def load_image(path):
 
13
  def process_image(input_image):
14
  input_path = "./datasets/data/test/input_image.png"
15
  output_dir = "./results/demo/color_pix2pix/test_latest"
16
+ output_image_path = os.path.join(output_dir, "images", "fake_B_rgb.png")
17
 
18
  # Save the input image
19
  save_image(input_image, input_path)
20
 
 
21
  cmd = [
22
  "python", "test.py", # Command to run the test script
23
  "--dataroot", "./datasets/data", # Adjust path as needed
 
33
  # Execute the command to process the image
34
  subprocess.run(cmd, check=True)
35
  except subprocess.CalledProcessError as e:
36
+ return f"Error while running command: {e}"
 
37
 
38
  # Check if the output directory exists
39
  if not os.path.exists(output_dir):
40
+ return f"Error: Output directory {output_dir} does not exist."
 
41
 
42
  # After processing, load the output image from the results directory
43
  output_files = [f for f in os.listdir(os.path.join(output_dir, "images")) if f.endswith('fake_B_rgb.png')]
 
44
  if not output_files:
45
+ return f"Error: No output files found in {os.path.join(output_dir, 'images')}."
 
46
 
47
+ return load_image(os.path.join(output_dir, "images", output_files[0]))
 
 
 
 
 
 
 
 
 
48
 
 
49
  iface = gr.Interface(
50
  fn=process_image,
51
  inputs=gr.Image(type="pil"),
52
  outputs=gr.Image(type="pil"),
53
+ title="Image Colorization",
54
+ description="Upload an image to colorize it using the model."
 
55
  )
56
 
57
+ if __name__ == "__main__":
58
+ iface.launch()