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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,7 +1,5 @@
1
  import gradio as gr
2
- import torch
3
- import numpy as np
4
- import json, os, sys, glob, shutil
5
  from PIL import Image
6
 
7
  #os.system("git clone https://github.com/sunset1995/HorizonNet.git /tmp/HorizonNet")
@@ -12,6 +10,15 @@ from huggingface_hub import hf_hub_download
12
  CKPT = hf_hub_download(repo_id="gum-tech/horizonnet-resnet50-rnn", filename="resnet50_rnn__st3d.pth")
13
  print("Model checkpoint at:", CKPT)
14
 
 
 
 
 
 
 
 
 
 
15
  def predict(image):
16
  for d in ["/tmp/hn_input", "/tmp/hn_pre", "/tmp/hn_out"]:
17
  shutil.rmtree(d, ignore_errors=True)
@@ -20,36 +27,32 @@ def predict(image):
20
  img_path = "/tmp/hn_input/room.png"
21
  image.convert("RGB").resize((1024, 512)).save(img_path)
22
 
23
- os.system(
24
- "cd /tmp/HorizonNet && python preprocess.py"
25
- " --img_glob " + img_path +
26
- " --output_dir /tmp/hn_pre"
27
- " > /tmp/hn_pre/preprocess_log.txt 2>&1"
28
- )
29
- log = open("/tmp/hn_pre/preprocess_log.txt").read() if os.path.exists("/tmp/hn_pre/preprocess_log.txt") else "no log"
30
- print("=== preprocess log ===\n", log)
31
 
32
  aligned = glob.glob("/tmp/hn_pre/*_aligned_rgb.png")
33
  if not aligned:
34
- return json.dumps({"error": "Preprocessing failed", "log": log})
35
 
36
  aligned_path = aligned[0]
37
  print("Aligned image:", aligned_path)
38
 
39
- os.system(
40
- "cd /tmp/HorizonNet && python inference.py"
41
- " --pth " + CKPT +
42
- " --img_glob " + aligned_path +
43
- " --output_dir /tmp/hn_out"
44
- " --no_cuda"
45
- " > /tmp/hn_out/inference_log.txt 2>&1"
46
- )
47
- inf_log = open("/tmp/hn_out/inference_log.txt").read() if os.path.exists("/tmp/hn_out/inference_log.txt") else "no log"
48
- print("=== inference log ===\n", inf_log)
49
 
50
  out_jsons = glob.glob("/tmp/hn_out/*.json")
51
  if not out_jsons:
52
- return json.dumps({"error": "Inference failed", "log": inf_log})
53
 
54
  with open(out_jsons[0]) as f:
55
  result = json.load(f)
 
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")
 
10
  CKPT = hf_hub_download(repo_id="gum-tech/horizonnet-resnet50-rnn", filename="resnet50_rnn__st3d.pth")
11
  print("Model checkpoint at:", CKPT)
12
 
13
+ def run(cmd, cwd="/tmp/HorizonNet"):
14
+ result = subprocess.run(
15
+ cmd, cwd=cwd, shell=False,
16
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT
17
+ )
18
+ log = result.stdout.decode("utf-8", errors="replace")
19
+ print(f"=== {cmd[0]} (exit {result.returncode}) ===\n{log}\n===")
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)
 
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)