Spaces:
Sleeping
Sleeping
Blair Johnson commited on
Commit ·
9c47dab
1
Parent(s): f374884
Auto estimate depth on upload
Browse files- app.py +50 -62
- requirements.txt +10 -10
app.py
CHANGED
|
@@ -68,7 +68,6 @@ DEFAULT_TEXTURE_STRENGTH = 0.1
|
|
| 68 |
DEFAULT_TEXTURE_SMOOTHING = 0
|
| 69 |
DEFAULT_MIN_Z = 0.5
|
| 70 |
DEFAULT_MAX_Z = 5.0
|
| 71 |
-
REFERENCE_TEXTURE_Z_SPAN = 10.0
|
| 72 |
DEFAULT_CURVE_POINTS = [
|
| 73 |
{"x": 0.0, "y": 0.0},
|
| 74 |
{"x": 1.0, "y": 1.0},
|
|
@@ -681,8 +680,6 @@ def prepare_depth(input_filepath: str):
|
|
| 681 |
adjusted_depth,
|
| 682 |
DEFAULT_TEXTURE_STRENGTH,
|
| 683 |
DEFAULT_TEXTURE_SMOOTHING,
|
| 684 |
-
DEFAULT_MIN_Z,
|
| 685 |
-
DEFAULT_MAX_Z,
|
| 686 |
)
|
| 687 |
state = {
|
| 688 |
"image_rgb": input_image,
|
|
@@ -694,8 +691,6 @@ def prepare_depth(input_filepath: str):
|
|
| 694 |
"invert_depth": False,
|
| 695 |
"texture_strength": DEFAULT_TEXTURE_STRENGTH,
|
| 696 |
"texture_smoothing": DEFAULT_TEXTURE_SMOOTHING,
|
| 697 |
-
"min_z": DEFAULT_MIN_Z,
|
| 698 |
-
"max_z": DEFAULT_MAX_Z,
|
| 699 |
"load_status": load_status,
|
| 700 |
}
|
| 701 |
status = (
|
|
@@ -713,6 +708,33 @@ def prepare_depth(input_filepath: str):
|
|
| 713 |
)
|
| 714 |
|
| 715 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
def get_smoothed_depth_from_state(depth_state: dict[str, Any] | None, depth_map_smoothing: int) -> np.ndarray:
|
| 717 |
if not depth_state or "raw_depth" not in depth_state:
|
| 718 |
raise gr.Error("Please upload an image and estimate its depth first.")
|
|
@@ -726,14 +748,12 @@ def update_depth_preview(
|
|
| 726 |
invert_depth: bool,
|
| 727 |
texture_strength: float,
|
| 728 |
texture_smoothing: int,
|
| 729 |
-
min_z: float,
|
| 730 |
-
max_z: float,
|
| 731 |
):
|
| 732 |
-
|
|
|
|
|
|
|
| 733 |
texture_strength = coerce_float(texture_strength, DEFAULT_TEXTURE_STRENGTH)
|
| 734 |
texture_smoothing = int(coerce_float(texture_smoothing, DEFAULT_TEXTURE_SMOOTHING))
|
| 735 |
-
if max_z <= min_z:
|
| 736 |
-
raise gr.Error("Max Z-height must be greater than Min Z-height.")
|
| 737 |
smoothed_depth = get_smoothed_depth_from_state(depth_state, depth_map_smoothing)
|
| 738 |
adjusted_depth = apply_depth_curve(smoothed_depth, curve_points_json, invert_depth)
|
| 739 |
points = sanitize_curve_points(curve_points_json)
|
|
@@ -743,8 +763,6 @@ def update_depth_preview(
|
|
| 743 |
adjusted_depth,
|
| 744 |
texture_strength,
|
| 745 |
texture_smoothing,
|
| 746 |
-
min_z,
|
| 747 |
-
max_z,
|
| 748 |
)
|
| 749 |
if depth_state is not None:
|
| 750 |
depth_state["adjusted_depth"] = adjusted_depth
|
|
@@ -754,12 +772,9 @@ def update_depth_preview(
|
|
| 754 |
depth_state["invert_depth"] = bool(invert_depth)
|
| 755 |
depth_state["texture_strength"] = float(texture_strength)
|
| 756 |
depth_state["texture_smoothing"] = int(texture_smoothing)
|
| 757 |
-
depth_state["min_z"] = float(min_z)
|
| 758 |
-
depth_state["max_z"] = float(max_z)
|
| 759 |
-
effective_texture_strength = normalized_texture_strength(texture_strength, min_z, max_z)
|
| 760 |
status = (
|
| 761 |
-
f"Preview updated with {len(points)} curve points, texture strength {float(texture_strength):.3f} "
|
| 762 |
-
f"
|
| 763 |
)
|
| 764 |
return (
|
| 765 |
depth_state,
|
|
@@ -803,10 +818,7 @@ def get_height_map_for_settings(
|
|
| 803 |
invert_depth: bool,
|
| 804 |
texture_strength: float,
|
| 805 |
texture_smoothing: int,
|
| 806 |
-
min_z: float,
|
| 807 |
-
max_z: float,
|
| 808 |
) -> np.ndarray:
|
| 809 |
-
min_z, max_z = coerce_z_bounds(min_z, max_z)
|
| 810 |
texture_strength = coerce_float(texture_strength, DEFAULT_TEXTURE_STRENGTH)
|
| 811 |
texture_smoothing = int(coerce_float(texture_smoothing, DEFAULT_TEXTURE_SMOOTHING))
|
| 812 |
normalized_curve_json = curve_points_to_json(sanitize_curve_points(curve_points_json))
|
|
@@ -818,8 +830,6 @@ def get_height_map_for_settings(
|
|
| 818 |
and bool(depth_state.get("invert_depth", False)) == bool(invert_depth)
|
| 819 |
and float(depth_state.get("texture_strength", -1.0)) == float(texture_strength)
|
| 820 |
and int(depth_state.get("texture_smoothing", -1)) == int(texture_smoothing)
|
| 821 |
-
and float(depth_state.get("min_z", float("nan"))) == float(min_z)
|
| 822 |
-
and float(depth_state.get("max_z", float("nan"))) == float(max_z)
|
| 823 |
)
|
| 824 |
if cache_matches:
|
| 825 |
return cached_height_map
|
|
@@ -835,30 +845,18 @@ def get_height_map_for_settings(
|
|
| 835 |
adjusted_depth,
|
| 836 |
texture_strength,
|
| 837 |
texture_smoothing,
|
| 838 |
-
min_z,
|
| 839 |
-
max_z,
|
| 840 |
)
|
| 841 |
depth_state["height_map"] = height_map
|
| 842 |
depth_state["texture_strength"] = float(texture_strength)
|
| 843 |
depth_state["texture_smoothing"] = int(texture_smoothing)
|
| 844 |
-
depth_state["min_z"] = float(min_z)
|
| 845 |
-
depth_state["max_z"] = float(max_z)
|
| 846 |
return height_map
|
| 847 |
|
| 848 |
|
| 849 |
-
def normalized_texture_strength(texture_strength: float, min_z: float, max_z: float) -> float:
|
| 850 |
-
min_z, max_z = coerce_z_bounds(min_z, max_z)
|
| 851 |
-
z_span = max(max_z - min_z, 1e-6)
|
| 852 |
-
return coerce_float(texture_strength, DEFAULT_TEXTURE_STRENGTH) * (REFERENCE_TEXTURE_Z_SPAN / z_span)
|
| 853 |
-
|
| 854 |
-
|
| 855 |
def combine_depth_and_texture(
|
| 856 |
image_rgb: np.ndarray,
|
| 857 |
adjusted_depth: np.ndarray,
|
| 858 |
texture_strength: float,
|
| 859 |
texture_smoothing: int,
|
| 860 |
-
min_z: float,
|
| 861 |
-
max_z: float,
|
| 862 |
) -> np.ndarray:
|
| 863 |
texture_smoothing = int(coerce_float(texture_smoothing, DEFAULT_TEXTURE_SMOOTHING))
|
| 864 |
gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
|
|
@@ -872,8 +870,8 @@ def combine_depth_and_texture(
|
|
| 872 |
interpolation=cv2.INTER_LINEAR,
|
| 873 |
)
|
| 874 |
|
| 875 |
-
|
| 876 |
-
combined_map = adjusted_depth + (brightness_normalized *
|
| 877 |
return np.clip(combined_map, 0.0, 1.0).astype(np.float32)
|
| 878 |
|
| 879 |
|
|
@@ -970,8 +968,6 @@ def generate_3d_model_from_adjusted_depth(
|
|
| 970 |
invert_depth,
|
| 971 |
texture_strength,
|
| 972 |
texture_smoothing,
|
| 973 |
-
min_z,
|
| 974 |
-
max_z,
|
| 975 |
)
|
| 976 |
z_data = min_z + height_map * (max_z - min_z)
|
| 977 |
|
|
@@ -1074,19 +1070,25 @@ with gr.Blocks(title="Image to 3D Relief Generator") as demo:
|
|
| 1074 |
output_file = gr.File(label="Download STL File")
|
| 1075 |
generation_status = gr.Textbox(label="Generation status", interactive=False)
|
| 1076 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1077 |
estimate_btn.click(
|
| 1078 |
fn=prepare_depth,
|
| 1079 |
inputs=[input_file],
|
| 1080 |
-
outputs=
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
curve_points_json,
|
| 1087 |
-
analysis_status,
|
| 1088 |
-
preview_status,
|
| 1089 |
-
],
|
| 1090 |
)
|
| 1091 |
|
| 1092 |
preview_inputs = [
|
|
@@ -1096,8 +1098,6 @@ with gr.Blocks(title="Image to 3D Relief Generator") as demo:
|
|
| 1096 |
invert_depth,
|
| 1097 |
texture_strength,
|
| 1098 |
texture_smoothing,
|
| 1099 |
-
min_z,
|
| 1100 |
-
max_z,
|
| 1101 |
]
|
| 1102 |
preview_outputs = [depth_state, adjusted_depth_preview, depth_histogram_json, curve_points_json, preview_status]
|
| 1103 |
update_preview_btn.click(
|
|
@@ -1135,18 +1135,6 @@ with gr.Blocks(title="Image to 3D Relief Generator") as demo:
|
|
| 1135 |
outputs=preview_outputs,
|
| 1136 |
show_progress=False,
|
| 1137 |
)
|
| 1138 |
-
min_z.change(
|
| 1139 |
-
fn=update_depth_preview,
|
| 1140 |
-
inputs=preview_inputs,
|
| 1141 |
-
outputs=preview_outputs,
|
| 1142 |
-
show_progress=False,
|
| 1143 |
-
)
|
| 1144 |
-
max_z.change(
|
| 1145 |
-
fn=update_depth_preview,
|
| 1146 |
-
inputs=preview_inputs,
|
| 1147 |
-
outputs=preview_outputs,
|
| 1148 |
-
show_progress=False,
|
| 1149 |
-
)
|
| 1150 |
generate_btn.click(
|
| 1151 |
fn=generate_3d_model_from_adjusted_depth,
|
| 1152 |
inputs=[
|
|
|
|
| 68 |
DEFAULT_TEXTURE_SMOOTHING = 0
|
| 69 |
DEFAULT_MIN_Z = 0.5
|
| 70 |
DEFAULT_MAX_Z = 5.0
|
|
|
|
| 71 |
DEFAULT_CURVE_POINTS = [
|
| 72 |
{"x": 0.0, "y": 0.0},
|
| 73 |
{"x": 1.0, "y": 1.0},
|
|
|
|
| 680 |
adjusted_depth,
|
| 681 |
DEFAULT_TEXTURE_STRENGTH,
|
| 682 |
DEFAULT_TEXTURE_SMOOTHING,
|
|
|
|
|
|
|
| 683 |
)
|
| 684 |
state = {
|
| 685 |
"image_rgb": input_image,
|
|
|
|
| 691 |
"invert_depth": False,
|
| 692 |
"texture_strength": DEFAULT_TEXTURE_STRENGTH,
|
| 693 |
"texture_smoothing": DEFAULT_TEXTURE_SMOOTHING,
|
|
|
|
|
|
|
| 694 |
"load_status": load_status,
|
| 695 |
}
|
| 696 |
status = (
|
|
|
|
| 708 |
)
|
| 709 |
|
| 710 |
|
| 711 |
+
def clear_depth_outputs():
|
| 712 |
+
return (
|
| 713 |
+
None,
|
| 714 |
+
None,
|
| 715 |
+
None,
|
| 716 |
+
None,
|
| 717 |
+
"[]",
|
| 718 |
+
curve_points_to_json(DEFAULT_CURVE_POINTS),
|
| 719 |
+
"Upload an image to estimate its depth map.",
|
| 720 |
+
"",
|
| 721 |
+
)
|
| 722 |
+
|
| 723 |
+
|
| 724 |
+
def clear_preview_outputs(message: str = "Upload an image to estimate its depth map before tuning preview controls."):
|
| 725 |
+
return (
|
| 726 |
+
None,
|
| 727 |
+
None,
|
| 728 |
+
"[]",
|
| 729 |
+
curve_points_to_json(DEFAULT_CURVE_POINTS),
|
| 730 |
+
message,
|
| 731 |
+
)
|
| 732 |
+
|
| 733 |
+
def auto_prepare_depth(input_filepath: str):
|
| 734 |
+
if input_filepath is None:
|
| 735 |
+
return clear_depth_outputs()
|
| 736 |
+
return prepare_depth(input_filepath)
|
| 737 |
+
|
| 738 |
def get_smoothed_depth_from_state(depth_state: dict[str, Any] | None, depth_map_smoothing: int) -> np.ndarray:
|
| 739 |
if not depth_state or "raw_depth" not in depth_state:
|
| 740 |
raise gr.Error("Please upload an image and estimate its depth first.")
|
|
|
|
| 748 |
invert_depth: bool,
|
| 749 |
texture_strength: float,
|
| 750 |
texture_smoothing: int,
|
|
|
|
|
|
|
| 751 |
):
|
| 752 |
+
if not depth_state or "raw_depth" not in depth_state:
|
| 753 |
+
return clear_preview_outputs()
|
| 754 |
+
|
| 755 |
texture_strength = coerce_float(texture_strength, DEFAULT_TEXTURE_STRENGTH)
|
| 756 |
texture_smoothing = int(coerce_float(texture_smoothing, DEFAULT_TEXTURE_SMOOTHING))
|
|
|
|
|
|
|
| 757 |
smoothed_depth = get_smoothed_depth_from_state(depth_state, depth_map_smoothing)
|
| 758 |
adjusted_depth = apply_depth_curve(smoothed_depth, curve_points_json, invert_depth)
|
| 759 |
points = sanitize_curve_points(curve_points_json)
|
|
|
|
| 763 |
adjusted_depth,
|
| 764 |
texture_strength,
|
| 765 |
texture_smoothing,
|
|
|
|
|
|
|
| 766 |
)
|
| 767 |
if depth_state is not None:
|
| 768 |
depth_state["adjusted_depth"] = adjusted_depth
|
|
|
|
| 772 |
depth_state["invert_depth"] = bool(invert_depth)
|
| 773 |
depth_state["texture_strength"] = float(texture_strength)
|
| 774 |
depth_state["texture_smoothing"] = int(texture_smoothing)
|
|
|
|
|
|
|
|
|
|
| 775 |
status = (
|
| 776 |
+
f"Preview updated with {len(points)} curve points, texture strength {float(texture_strength):.3f}, "
|
| 777 |
+
f"texture smoothing {int(texture_smoothing)}."
|
| 778 |
)
|
| 779 |
return (
|
| 780 |
depth_state,
|
|
|
|
| 818 |
invert_depth: bool,
|
| 819 |
texture_strength: float,
|
| 820 |
texture_smoothing: int,
|
|
|
|
|
|
|
| 821 |
) -> np.ndarray:
|
|
|
|
| 822 |
texture_strength = coerce_float(texture_strength, DEFAULT_TEXTURE_STRENGTH)
|
| 823 |
texture_smoothing = int(coerce_float(texture_smoothing, DEFAULT_TEXTURE_SMOOTHING))
|
| 824 |
normalized_curve_json = curve_points_to_json(sanitize_curve_points(curve_points_json))
|
|
|
|
| 830 |
and bool(depth_state.get("invert_depth", False)) == bool(invert_depth)
|
| 831 |
and float(depth_state.get("texture_strength", -1.0)) == float(texture_strength)
|
| 832 |
and int(depth_state.get("texture_smoothing", -1)) == int(texture_smoothing)
|
|
|
|
|
|
|
| 833 |
)
|
| 834 |
if cache_matches:
|
| 835 |
return cached_height_map
|
|
|
|
| 845 |
adjusted_depth,
|
| 846 |
texture_strength,
|
| 847 |
texture_smoothing,
|
|
|
|
|
|
|
| 848 |
)
|
| 849 |
depth_state["height_map"] = height_map
|
| 850 |
depth_state["texture_strength"] = float(texture_strength)
|
| 851 |
depth_state["texture_smoothing"] = int(texture_smoothing)
|
|
|
|
|
|
|
| 852 |
return height_map
|
| 853 |
|
| 854 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
def combine_depth_and_texture(
|
| 856 |
image_rgb: np.ndarray,
|
| 857 |
adjusted_depth: np.ndarray,
|
| 858 |
texture_strength: float,
|
| 859 |
texture_smoothing: int,
|
|
|
|
|
|
|
| 860 |
) -> np.ndarray:
|
| 861 |
texture_smoothing = int(coerce_float(texture_smoothing, DEFAULT_TEXTURE_SMOOTHING))
|
| 862 |
gray_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
|
|
|
|
| 870 |
interpolation=cv2.INTER_LINEAR,
|
| 871 |
)
|
| 872 |
|
| 873 |
+
texture_strength = coerce_float(texture_strength, DEFAULT_TEXTURE_STRENGTH)
|
| 874 |
+
combined_map = adjusted_depth + (brightness_normalized * texture_strength)
|
| 875 |
return np.clip(combined_map, 0.0, 1.0).astype(np.float32)
|
| 876 |
|
| 877 |
|
|
|
|
| 968 |
invert_depth,
|
| 969 |
texture_strength,
|
| 970 |
texture_smoothing,
|
|
|
|
|
|
|
| 971 |
)
|
| 972 |
z_data = min_z + height_map * (max_z - min_z)
|
| 973 |
|
|
|
|
| 1070 |
output_file = gr.File(label="Download STL File")
|
| 1071 |
generation_status = gr.Textbox(label="Generation status", interactive=False)
|
| 1072 |
|
| 1073 |
+
depth_estimation_outputs = [
|
| 1074 |
+
depth_state,
|
| 1075 |
+
original_preview,
|
| 1076 |
+
raw_depth_preview,
|
| 1077 |
+
adjusted_depth_preview,
|
| 1078 |
+
depth_histogram_json,
|
| 1079 |
+
curve_points_json,
|
| 1080 |
+
analysis_status,
|
| 1081 |
+
preview_status,
|
| 1082 |
+
]
|
| 1083 |
estimate_btn.click(
|
| 1084 |
fn=prepare_depth,
|
| 1085 |
inputs=[input_file],
|
| 1086 |
+
outputs=depth_estimation_outputs,
|
| 1087 |
+
)
|
| 1088 |
+
input_file.change(
|
| 1089 |
+
fn=auto_prepare_depth,
|
| 1090 |
+
inputs=[input_file],
|
| 1091 |
+
outputs=depth_estimation_outputs,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1092 |
)
|
| 1093 |
|
| 1094 |
preview_inputs = [
|
|
|
|
| 1098 |
invert_depth,
|
| 1099 |
texture_strength,
|
| 1100 |
texture_smoothing,
|
|
|
|
|
|
|
| 1101 |
]
|
| 1102 |
preview_outputs = [depth_state, adjusted_depth_preview, depth_histogram_json, curve_points_json, preview_status]
|
| 1103 |
update_preview_btn.click(
|
|
|
|
| 1135 |
outputs=preview_outputs,
|
| 1136 |
show_progress=False,
|
| 1137 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1138 |
generate_btn.click(
|
| 1139 |
fn=generate_3d_model_from_adjusted_depth,
|
| 1140 |
inputs=[
|
requirements.txt
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
transformers
|
| 3 |
-
torch
|
| 4 |
-
torchvision
|
| 5 |
-
accelerate
|
| 6 |
-
opencv-python-headless
|
| 7 |
-
numpy-stl
|
| 8 |
-
Pillow
|
| 9 |
-
scikit-learn
|
| 10 |
-
pillow-heif
|
|
|
|
| 1 |
+
gradio==6.14.0
|
| 2 |
+
transformers==5.8.0
|
| 3 |
+
torch==2.11.0
|
| 4 |
+
torchvision==0.26.0
|
| 5 |
+
accelerate==1.13.0
|
| 6 |
+
opencv-python-headless==4.13.0.92
|
| 7 |
+
numpy-stl==3.2.0
|
| 8 |
+
Pillow==12.2.0
|
| 9 |
+
scikit-learn==1.8.0
|
| 10 |
+
pillow-heif==1.3.0
|