Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import mediapipe as mp
|
|
@@ -40,45 +39,51 @@ def create_3d_plot(vertices):
|
|
| 40 |
scene=dict(
|
| 41 |
xaxis_title='X',
|
| 42 |
yaxis_title='Y',
|
| 43 |
-
zaxis_title='Z'
|
|
|
|
| 44 |
),
|
| 45 |
-
margin=dict(l=0, r=0, b=0, t=0)
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
return fig
|
| 49 |
|
| 50 |
def save_obj_and_display(frame):
|
| 51 |
"""Save frame as OBJ and return both file path and 3D visualization"""
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
def cleanup_old_files():
|
| 84 |
"""Remove files older than 1 hour"""
|
|
@@ -105,8 +110,20 @@ def create_interface():
|
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column():
|
| 108 |
-
# Input methods
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
generate_button = gr.Button("Generate 3D Model")
|
| 111 |
|
| 112 |
with gr.Column():
|
|
@@ -115,13 +132,29 @@ def create_interface():
|
|
| 115 |
plot_3d = gr.Plot(label="3D Preview")
|
| 116 |
status_text = gr.Textbox(label="Status")
|
| 117 |
|
| 118 |
-
# Set up event
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
generate_button.click(
|
| 120 |
fn=save_obj_and_display,
|
| 121 |
inputs=[input_image],
|
| 122 |
outputs=[obj_file, plot_3d, status_text]
|
| 123 |
)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# Cleanup old files periodically
|
| 126 |
cleanup_old_files()
|
| 127 |
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import mediapipe as mp
|
|
|
|
| 39 |
scene=dict(
|
| 40 |
xaxis_title='X',
|
| 41 |
yaxis_title='Y',
|
| 42 |
+
zaxis_title='Z',
|
| 43 |
+
aspectmode='cube'
|
| 44 |
),
|
| 45 |
+
margin=dict(l=0, r=0, b=0, t=0),
|
| 46 |
+
showlegend=False
|
| 47 |
)
|
| 48 |
|
| 49 |
return fig
|
| 50 |
|
| 51 |
def save_obj_and_display(frame):
|
| 52 |
"""Save frame as OBJ and return both file path and 3D visualization"""
|
| 53 |
+
try:
|
| 54 |
+
if frame is None:
|
| 55 |
+
return None, None, "No frame provided"
|
| 56 |
+
|
| 57 |
+
img, faces = process_frame(frame)
|
| 58 |
+
|
| 59 |
+
if not faces:
|
| 60 |
+
return None, None, "No face detected in the frame"
|
| 61 |
+
|
| 62 |
+
# Scale vertices
|
| 63 |
+
vertices = np.array(faces[0]) * 150
|
| 64 |
+
|
| 65 |
+
# Create temporary directory if it doesn't exist
|
| 66 |
+
temp_dir = Path('temp')
|
| 67 |
+
temp_dir.mkdir(exist_ok=True)
|
| 68 |
+
|
| 69 |
+
# Generate timestamp
|
| 70 |
+
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 71 |
+
filename = f"face_mesh_{timestamp}"
|
| 72 |
+
|
| 73 |
+
# Save OBJ file
|
| 74 |
+
obj_path = temp_dir / f"{filename}.obj"
|
| 75 |
+
success = detector.save_obj_file(vertices, filename)
|
| 76 |
+
|
| 77 |
+
if not success:
|
| 78 |
+
return None, None, "Failed to save OBJ file"
|
| 79 |
+
|
| 80 |
+
# Create 3D visualization
|
| 81 |
+
fig = create_3d_plot(vertices)
|
| 82 |
+
|
| 83 |
+
return str(obj_path), fig, "Model generated successfully"
|
| 84 |
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return None, None, f"Error: {str(e)}"
|
| 87 |
|
| 88 |
def cleanup_old_files():
|
| 89 |
"""Remove files older than 1 hour"""
|
|
|
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column():
|
| 113 |
+
# Input methods - using webcam and image input
|
| 114 |
+
with gr.Tab("Webcam"):
|
| 115 |
+
input_webcam = gr.Image(
|
| 116 |
+
label="Webcam Input",
|
| 117 |
+
type="numpy",
|
| 118 |
+
sources="webcam"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
with gr.Tab("Upload"):
|
| 122 |
+
input_image = gr.Image(
|
| 123 |
+
label="Upload Image",
|
| 124 |
+
type="numpy"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
generate_button = gr.Button("Generate 3D Model")
|
| 128 |
|
| 129 |
with gr.Column():
|
|
|
|
| 132 |
plot_3d = gr.Plot(label="3D Preview")
|
| 133 |
status_text = gr.Textbox(label="Status")
|
| 134 |
|
| 135 |
+
# Set up event handlers for both webcam and uploaded image
|
| 136 |
+
generate_button.click(
|
| 137 |
+
fn=save_obj_and_display,
|
| 138 |
+
inputs=[input_webcam],
|
| 139 |
+
outputs=[obj_file, plot_3d, status_text]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
generate_button.click(
|
| 143 |
fn=save_obj_and_display,
|
| 144 |
inputs=[input_image],
|
| 145 |
outputs=[obj_file, plot_3d, status_text]
|
| 146 |
)
|
| 147 |
|
| 148 |
+
# Example section
|
| 149 |
+
gr.Markdown("""
|
| 150 |
+
## Tips for Best Results
|
| 151 |
+
- Ensure good lighting
|
| 152 |
+
- Face the camera directly
|
| 153 |
+
- Keep a neutral expression
|
| 154 |
+
- Avoid extreme head rotations
|
| 155 |
+
- Stay within arm's length of the camera
|
| 156 |
+
""")
|
| 157 |
+
|
| 158 |
# Cleanup old files periodically
|
| 159 |
cleanup_old_files()
|
| 160 |
|