maheshmaddi92 commited on
Commit
37e0de7
·
verified ·
1 Parent(s): e66d8a6

getting runtime error due to image is not accessible.

Browse files
Files changed (1) hide show
  1. app.py +54 -29
app.py CHANGED
@@ -9,12 +9,9 @@ import sys
9
  from subprocess import call
10
  import psutil
11
 
 
 
12
 
13
-
14
-
15
- torch.hub.download_url_to_file('http://people.csail.mit.edu/billf/project%20pages/sresCode/Markov%20Random%20Fields%20for%20Super-Resolution_files/100075_lowres.jpg', 'bear.jpg')
16
-
17
-
18
  def run_cmd(command):
19
  try:
20
  print(command)
@@ -22,47 +19,75 @@ def run_cmd(command):
22
  except KeyboardInterrupt:
23
  print("Process interrupted")
24
  sys.exit(1)
25
- run_cmd("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P .")
26
- run_cmd("pip install basicsr")
27
- run_cmd("pip freeze")
28
 
29
- os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth -P .")
 
 
30
 
 
31
 
32
- def inference(img,mode):
 
 
 
33
  _id = randint(1, 10000)
34
- INPUT_DIR = "/tmp/input_image" + str(_id) + "/"
35
- OUTPUT_DIR = "/tmp/output_image" + str(_id) + "/"
36
- run_cmd("rm -rf " + INPUT_DIR)
37
- run_cmd("rm -rf " + OUTPUT_DIR)
38
- run_cmd("mkdir " + INPUT_DIR)
39
- run_cmd("mkdir " + OUTPUT_DIR)
 
 
40
  basewidth = 256
41
  wpercent = (basewidth/float(img.size[0]))
42
  hsize = int((float(img.size[1])*float(wpercent)))
43
  img = img.resize((basewidth,hsize), Image.LANCZOS)
44
- img.save(INPUT_DIR + "1.jpg", "JPEG")
 
 
 
45
  if mode == "base":
46
- run_cmd("python inference_realesrgan.py -n RealESRGAN_x4plus -i "+ INPUT_DIR + " -o " + OUTPUT_DIR)
47
  else:
48
- os.system("python inference_realesrgan.py -n RealESRGAN_x4plus_anime_6B -i "+ INPUT_DIR + " -o " + OUTPUT_DIR)
49
- return os.path.join(OUTPUT_DIR, "1_out.jpg")
50
-
51
-
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
-
54
  title = "Real-ESRGAN"
55
  description = "Gradio demo for Real-ESRGAN. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please click submit only once"
56
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2107.10833'>Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data</a> | <a href='https://github.com/xinntao/Real-ESRGAN'>Github Repo</a></p>"
57
 
58
- gr.Interface(
59
- inference,
60
- [gr.inputs.Image(type="pil", label="Input"),gr.inputs.Radio(["base","anime"], type="value", default="base", label="model type")],
 
 
 
 
61
  gr.outputs.Image(type="file", label="Output"),
62
  title=title,
63
  description=description,
64
  article=article,
65
  examples=[
66
- ['bear.jpg','base'],
67
- ['anime.png','anime']
68
- ]).launch()
 
 
 
 
 
9
  from subprocess import call
10
  import psutil
11
 
12
+ # Remove the torch.hub download and instead ensure 'bear.jpg' is in your directory
13
+ # Place bear.jpg and anime.png in your project directory manually
14
 
 
 
 
 
 
15
  def run_cmd(command):
16
  try:
17
  print(command)
 
19
  except KeyboardInterrupt:
20
  print("Process interrupted")
21
  sys.exit(1)
 
 
 
22
 
23
+ # Download model weights if they don't exist
24
+ if not os.path.exists("RealESRGAN_x4plus.pth"):
25
+ run_cmd("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P .")
26
 
27
+ run_cmd("pip install basicsr")
28
 
29
+ if not os.path.exists("RealESRGAN_x4plus_anime_6B.pth"):
30
+ os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth -P .")
31
+
32
+ def inference(img, mode):
33
  _id = randint(1, 10000)
34
+ INPUT_DIR = f"/tmp/input_image{_id}/"
35
+ OUTPUT_DIR = f"/tmp/output_image{_id}/"
36
+
37
+ # Create directories safely
38
+ os.makedirs(INPUT_DIR, exist_ok=True)
39
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
40
+
41
+ # Resize image
42
  basewidth = 256
43
  wpercent = (basewidth/float(img.size[0]))
44
  hsize = int((float(img.size[1])*float(wpercent)))
45
  img = img.resize((basewidth,hsize), Image.LANCZOS)
46
+
47
+ input_path = os.path.join(INPUT_DIR, "1.jpg")
48
+ img.save(input_path, "JPEG")
49
+
50
  if mode == "base":
51
+ model_name = "RealESRGAN_x4plus"
52
  else:
53
+ model_name = "RealESRGAN_x4plus_anime_6B"
54
+
55
+ command = f"python inference_realesrgan.py -n {model_name} -i {INPUT_DIR} -o {OUTPUT_DIR}"
56
+ run_cmd(command)
57
+
58
+ output_path = os.path.join(OUTPUT_DIR, "1_out.jpg")
59
+
60
+ # Cleanup temporary directories
61
+ try:
62
+ if os.path.exists(INPUT_DIR):
63
+ os.system(f"rm -rf {INPUT_DIR}")
64
+ if os.path.exists(OUTPUT_DIR):
65
+ os.system(f"rm -rf {OUTPUT_DIR}")
66
+ except Exception as e:
67
+ print(f"Cleanup error: {e}")
68
+
69
+ return output_path
70
 
 
71
  title = "Real-ESRGAN"
72
  description = "Gradio demo for Real-ESRGAN. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please click submit only once"
73
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2107.10833'>Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data</a> | <a href='https://github.com/xinntao/Real-ESRGAN'>Github Repo</a></p>"
74
 
75
+ # Create interface
76
+ interface = gr.Interface(
77
+ inference,
78
+ [
79
+ gr.inputs.Image(type="pil", label="Input"),
80
+ gr.inputs.Radio(["base", "anime"], type="value", default="base", label="model type")
81
+ ],
82
  gr.outputs.Image(type="file", label="Output"),
83
  title=title,
84
  description=description,
85
  article=article,
86
  examples=[
87
+ ['bear.jpg', 'base'],
88
+ ['anime.png', 'anime']
89
+ ]
90
+ )
91
+
92
+ # Launch the interface
93
+ interface.launch()