rlaidani commited on
Commit
75ef19f
·
verified ·
1 Parent(s): 0f4c637

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -33
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import gradio as gr
2
  import json, os, sys, glob, shutil, subprocess
3
  from PIL import Image
4
-
5
- #os.system("git clone https://github.com/sunset1995/HorizonNet.git /tmp/HorizonNet")
6
  sys.path.insert(0, "/tmp/HorizonNet")
7
 
8
  from huggingface_hub import hf_hub_download
@@ -20,45 +20,53 @@ def run(cmd, cwd="/tmp/HorizonNet"):
20
  return result.returncode, log
21
 
22
  def predict(image):
23
- for d in ["/tmp/hn_input", "/tmp/hn_pre", "/tmp/hn_out"]:
24
- shutil.rmtree(d, ignore_errors=True)
25
- os.makedirs(d)
 
 
 
 
 
26
 
27
- img_path = "/tmp/hn_input/room.png"
28
- image.convert("RGB").resize((1024, 512)).save(img_path)
 
 
 
 
29
 
30
- # Step 1: preprocess
31
- code, log = run([
32
- "python", "preprocess.py",
33
- "--img_glob", img_path,
34
- "--output_dir", "/tmp/hn_pre"
35
- ])
36
 
37
- aligned = glob.glob("/tmp/hn_pre/*_aligned_rgb.png")
38
- if not aligned:
39
- return json.dumps({"error": "Preprocessing failed", "code": code, "log": log[-1000:]})
40
 
41
- aligned_path = aligned[0]
42
- print("Aligned image:", aligned_path)
 
 
 
 
 
 
43
 
44
- # Step 2: inference
45
- code, log = run([
46
- "python", "inference.py",
47
- "--pth", CKPT,
48
- "--img_glob", aligned_path,
49
- "--output_dir", "/tmp/hn_out",
50
- "--no_cuda"
51
- ])
52
 
53
- out_jsons = glob.glob("/tmp/hn_out/*.json")
54
- if not out_jsons:
55
- return json.dumps({"error": "Inference failed", "code": code, "log": log[-1000:]})
56
 
57
- with open(out_jsons[0]) as f:
58
- result = json.load(f)
59
 
60
- print("Result:", result)
61
- return json.dumps(result)
 
 
 
62
 
63
  demo = gr.Interface(
64
  fn=predict,
 
1
  import gradio as gr
2
  import json, os, sys, glob, shutil, subprocess
3
  from PIL import Image
4
+ if not os.path.isdir('/tmp/HorizonNet'):
5
+ os.system("git clone https://github.com/sunset1995/HorizonNet.git /tmp/HorizonNet")
6
  sys.path.insert(0, "/tmp/HorizonNet")
7
 
8
  from huggingface_hub import hf_hub_download
 
20
  return result.returncode, log
21
 
22
  def predict(image):
23
+ try:
24
+ for d in ["/tmp/hn_input", "/tmp/hn_pre", "/tmp/hn_out"]:
25
+ shutil.rmtree(d, ignore_errors=True)
26
+ os.makedirs(d)
27
+
28
+ img_path = "/tmp/hn_input/room.png"
29
+ image.convert("RGB").resize((1024, 512)).save(img_path)
30
+ print("Image saved to", img_path)
31
 
32
+ # Step 1: preprocess
33
+ code, log = run([
34
+ "python", "preprocess.py",
35
+ "--img_glob", img_path,
36
+ "--output_dir", "/tmp/hn_pre"
37
+ ])
38
 
39
+ aligned = glob.glob("/tmp/hn_pre/*_aligned_rgb.png")
40
+ if not aligned:
41
+ return json.dumps({"error": "Preprocessing failed", "code": code, "log": log[-1000:]})
 
 
 
42
 
43
+ aligned_path = aligned[0]
44
+ print("Aligned image:", aligned_path)
 
45
 
46
+ # Step 2: inference
47
+ code, log = run([
48
+ "python", "inference.py",
49
+ "--pth", CKPT,
50
+ "--img_glob", aligned_path,
51
+ "--output_dir", "/tmp/hn_out",
52
+ "--no_cuda"
53
+ ])
54
 
55
+ out_jsons = glob.glob("/tmp/hn_out/*.json")
56
+ if not out_jsons:
57
+ return json.dumps({"error": "Inference failed", "code": code, "log": log[-1000:]})
 
 
 
 
 
58
 
59
+ with open(out_jsons[0]) as f:
60
+ result = json.load(f)
 
61
 
62
+ print("Result:", result)
63
+ return json.dumps(result)
64
 
65
+ except Exception as e:
66
+ import traceback
67
+ tb = traceback.format_exc()
68
+ print("=== EXCEPTION ===\n", tb)
69
+ return json.dumps({"error": str(e), "traceback": tb})
70
 
71
  demo = gr.Interface(
72
  fn=predict,