This project has been created as part of the 42 curriculum by dporhomo.
Goal: To build a full-stack web application that transforms a flat 2D image into an interactive, 3D LEGO-style web scene.
Overview: SliceReady3D is an end-to-end pipeline that takes user-uploaded images and estimates their physical depth using a custom, lightweight OpenCV heuristic. The pipeline collapses this continuous depth map into discrete spatial layers, repaints the image strictly using an official LEGO color palette (via Euclidean distance color quantization), and optimizes the resulting 1x1 studs into larger, efficient LEGO bricks. The final output is served to the frontend as a highly interactive, rotatable 3D instanced mesh using Three.js, complete with layer-by-layer assembly guides and 3D mesh export capabilities.
- Robust Image Upload: Accepts PNG, JPEG, and WEBP formats with strict 5MB size validation and graceful error handling.
- Local Depth Estimation: Extracts luminance and edge gradients to build a coherent depth map without relying on heavy ML models.
- Depth Layer Extraction: Collapses the continuous depth map into user-defined discrete layers (3 to 10).
- LEGO Color Quantization: Maps every pixel to the nearest valid color in an official LEGO palette.
- Interactive 3D UI: A fully responsive web interface where users can rotate, pan, and zoom around the generated 3D volume.
- Seamless UX: Real-time UI updates, dynamic status boxes detailing stud/brick counts, and asynchronous loading states.
- Bonus 1 (Real-World Instructions): Generates a layer-by-layer, step-by-step 2D visual assembly guide natively in the browser, optimized for PDF printing. Includes a full parts inventory.
- Bonus 2 (Brick Optimization): Implements a greedy 1D meshing algorithm to merge adjacent identical 1x1 studs into larger bricks (up to 1x8), drastically cutting down the physical brick count.
- Bonus 3 (Scene Export): Allows the user to download the generated geometry as a standard GLTF 3D mesh (ready for PrusaSlicer/Blender), a JSON blueprint, and a PNG snapshot.
- Bonus 4 (Artistic Modes): Includes switchable render modes: Solid Voxel Sculpture, Floating 2.5D Heightmap, and Traditional Flat Mosaic.
- Bonus 5 (Built-in AI Image Generator): I attempted to integrate the Hugging Face Inference API (
stable-diffusion-xl-base-1.0) to allow text-to-image generation prior to LEGO-fication. While the standalone Python test script successfully connected via a local.envtoken, the feature was intentionally omitted from the core FastAPI application. Free-tier inference APIs suffer from severe "cold start" timeouts (often taking minutes to load model weights into VRAM), which posed an unacceptable risk of hanging the web application during evaluation. Upgrading to a dedicated, always-on inference endpoint would make this viable in the future.
Given the hardware constraints of the development environment (Ubuntu, 4GB RAM), the architecture prioritizes performance, strict typing, and memory efficiency:
- Backend (Python / FastAPI): Selected for its speed, robust file handling, and clean MVC separation. All Python code strictly adheres to PEP 8 standards and MyPy static typing.
- Depth Processing (OpenCV): Instead of using a memory-heavy monocular depth ML model (like MiDaS), depth is calculated using a local
cv2.Sobeledge and luminance heuristic. This guarantees instant generation times without out-of-memory crashes. - Frontend Rendering (Three.js
InstancedMesh): Rather than rendering thousands of individual 3D box geometries (which destroys browser framerates), the scene uses a singleInstancedMeshto render the entire mosaic in one draw call, keeping the UI highly responsive.
- Python 3
pipandvenv(Virtual Environments)
- Clone the repository and navigate to the project root.
- Install the required Python dependencies in a new virtual environment that will be created in the parent directory ../.venv:
make install- Launch the FastAPI development server:
make runRunning 'make run' starts the web server, which makes the operation of the web application at the specified URL possible. When you finish using the web application, return to the terminal, press Ctr+C to abort its operation.
make run
Starting SliceReady3D development server...
../.venv/bin/uvicorn src.main:app --reload
INFO: Will watch for changes in these directories: ['/data/PROJECTS/ML03/BrickDepth/Code_']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [345171] using WatchFiles
INFO: Started server process [345173]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:36220 - "GET / HTTP/1.1" 200 OK
INFO: 127.0.0.1:36230 - "POST /api/upload HTTP/1.1" 200 OK
INFO: 127.0.0.1:36230 - "GET /outputs/01_radial_gradient_data.json HTTP/1.1" 200 OK
[Triggerred termination of the server by pressing CTRL+C]^C
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [345173]
INFO: Stopping reloader process [345171]
-
Open a modern web browser and navigate to the URL provided by Uvicorn
(
http://127.0.0.1:8000). -
Usage Example:
- On the left sidebar, click "Upload Image" and select a small test PNG/JPG.
- Adjust the "Depth layers" and "Studs" sliders if desired.
- Click the red Build Mosaic button.
- The status box will appear detailing the optimization metrics (e.g., "Done - 2304 studs, 6 depth layers...").
- The 3D scene will appear in the main container. Use your mouse to rotate (left-click drag), pan (right-click drag), and zoom (scroll).
- Explore the export options or print the assembly guide using the buttons in the sidebar.
- Depth Estimator: Custom OpenCV luminance/edge heuristic (No external API or credentials required).
- 3D Library: Three.js (Served via CDN in
index.html). - Image Generator (Tested for Bonus 5): Hugging Face Inference API (
stabilityai/stable-diffusion-xl-base-1.0). Requires anHF_API_TOKENset in a local.envfile to run the isolatedhf_test.pyscript. In development - not submitted with this project.
- dporhomo: As a solo developer on this project, I architected and implemented the entire end-to-end pipeline. This includes the FastAPI backend controller, the OpenCV depth/quantization mathematical logic, the 1D greedy meshing optimization algorithm, the HTML/CSS user interface, and the Three.js 3D rendering integration.
Classic References:
AI Partner (Gemini 3.1 Pro): This project was developed with an AI co-pilot. The AI was utilized to:
- Brainstorm lightweight alternatives to heavy ML depth models.
- Draft boilerplate for the Three.js
InstancedMeshsetup and GLTF exporting logic. - Help debug a JavaScript race condition (
DOMContentLoaded) where UI sliders failed to attach event listeners. - Refactor and enforce strict PEP 8 and MyPy type-hinting across the Python backend to meet submission standards.
I deliberately avoided relying on external neural networks like MiDaS, Depth-Anything, or Hugging Face APIs. Early testing revealed that cluster firewalls and strict DNS proxying often block external API calls, which creates a single point of failure and unstable performance during an evaluation.
Instead, I engineered a highly resilient, zero-network Computer Vision (CV) Heuristic using OpenCV. The heuristic relies on two fundamental principles of photography to estimate depth:
- Luminance (Brightness): In naive depth estimation, brighter objects are generally closer to the light source/camera. I converted the image to grayscale to extract this.
- Sharpness (Edges): Objects in the foreground are usually in focus, while backgrounds are softer. I used the Sobel operator to calculate the spatial gradients (edges) of the image.
I then blended these two matrices together using a 50/50 weight (cv2.addWeighted) and applied a Gaussian blur to smooth the noise. This approach runs instantly on the CPU, requires no internet connection, and provides the exact stylistic 'chunky' depth map required for a LEGO mosaic."
I used a Uniform Slicing approach to reduce the continuous 0–255 grayscale depth map into discrete layers.
While clustering algorithms like K-Means could work, they are computationally heavy and overkill for this application. Because grayscale values are already strictly bounded between 0 and 255, uniform slicing is mathematically faster and perfectly predictable.
The Math: I divided the maximum pixel value (256) by the target number of layers to get a bin size of roughly 42.6. Using NumPy's vectorized operations, I divided the entire depth matrix by this bin size and applied a floor function (np.floor()).
- Pixels with brightness 0–42 became Layer 0.
- Pixels with brightness 43–85 became Layer 1.
- ...and so on.
This instantly snapped the smooth 2.5D gradient into flat, physical 'steps' suitable for 3D modeling."
The project utilizes the provided official LEGO color palette (palette.json), which contains the exact RGB values for standard manufactured bricks.
To map the pixels to this palette, I used Euclidean Distance in 3D Color Space.
Instead of using slow, nested for-loops to check all 65,536 pixels one by one, I leveraged NumPy Array Broadcasting to do the math simultaneously on the CPU.
- I flattened the original image into a 1D list of RGB pixels.
- I calculated the difference between every pixel and every color in the LEGO palette simultaneously.
- I squared the differences (using squared distance is a standard optimization trick because it avoids calculating the computationally expensive square root).
- Finally, I used
np.argmin()to find the index of the LEGO color with the smallest distance to the original pixel, effectively 'snapping' the pixel to its nearest official color."
The program's pipeline is built on a strict MVC architecture using FastAPI and Three.js. The data flows through 5 distinct stages:
- Upload (Controller): The user uploads an image via the web UI. The FastAPI backend intercepts this, applies security guards (checking MIME types and strictly enforcing a 5MB size limit to prevent memory exhaustion), and temporarily saves the raw image to the server.
- Depth (Heuristic Engine): The image is passed to
depth_pipeline.py. Here, OpenCV converts the image to grayscale, applies Sobel edge detection, and blends them into a smoothed, continuous 0–255 depth map. - Layers (Quantization Engine): In
quantization.py, the smooth depth map is mathematically divided using uniform slicing into a strict integer matrix, creating physical Z-axis levels. - Mosaic (Reconstruction & Optimization): In the same module, the original colors are snapped to the official LEGO palette using Euclidean distance. Then, 1D Greedy Meshing algorithm (Bonus 2) runs. It scans horizontally and merges adjacent pixels of the exact same color and depth into standard LEGO bricks (e.g., 1x4, 1x8). This cuts the total brick count by 40–70%. The data is packaged into a JSON blueprint.
- 3D (Interactive Web UI): The browser fetches the JSON blueprint. To ensure 60FPS performance without crashing the browser, I use Three.js
InstancedMesh. This allows the GPU to render tens of thousands of individual LEGO bricks in a single draw call. The user can then interact with the scene or export it as a GLTF mesh for 3D printing."
Note: While a full PDF guide wasn't generated, the foundational and most computationally heavy part of this bonus—the exact Brick Inventory—was fully implemented.
Instead of looping through 65,536 pixels in Python to count colors (which would bottleneck the API), the backend leverages NumPy's highly optimized C-routines.
- I used
np.unique(closest_indices, return_counts=True)immediately after the color quantization step. This instantly tallies every color used in the matrix. - The algorithm maps these index counts back to the
palette.jsonto extract the official LEGO color names and hex codes. - It dynamically sorts the list in descending order and generates a responsive UI grid, providing the user with an exact "Bill of Materials" needed to physically build their mosaic.
To reduce the mosaic from thousands of tiny 1x1 studs into a realistic, structurally sound LEGO build, I engineered a Greedy 1D Meshing Algorithm in the Python backend (src/quantization.py).
- The algorithm scans the 3D matrix row by row (the X-axis). When it finds adjacent 1x1 studs that share the exact same depth layer and the exact same color, it merges them into a single, larger block (e.g., 1x2, 1x4, up to a maximum of 1x8).
- A 1D row-based scan runs in linear
$O(n)$ time, ensuring the API remains lightning-fast. Furthermore, real physical LEGO mosaics are much stronger when built with staggered linear bricks rather than massive 4x4 plates. - Because the perfect 2D grid was destroyed by merged blocks, I pivoted the JSON output from a flat array to a list of independent dictionary objects containing coordinate and scale data:
{x, y, z, width, color}. - The UI mathematically compares the original stud count vs. the optimized block count, consistently proving a 40% to 70% reduction in physical bricks.
I made a strict architectural decision to handle all exports Client-Side (in the browser) rather than Server-Side. This keeps the backend stateless, costs zero server memory, and makes downloads instant.
- GLTF 3D Mesh: I integrated
THREE.GLTFExporter. I specifically chose GLTF over OBJ because the.gltfformat natively supports Three.jsInstancedMeshand securely binds the exact RGB vertex colors to the geometry. This means a user can drag the exported file directly into PrusaSlicer, and a multi-material 3D printer will automatically know which colors to use. - PNG Snapshot: To allow the user to capture their current camera angle, I used the canvas
.toDataURL()method. The crucial fix: I had to initialize the WebGLRenderer withpreserveDrawingBuffer: true, otherwise the GPU would clear the memory before the snapshot was taken, resulting in a black image. - JSON Blueprint: The cached raw data array is simply stringified and exported as a
Blobfor use in external rendering software.
I implemented three distinct mathematical rendering modes that the user can switch between instantly without reloading the page or pinging the server.
- When the 3D data is fetched, it is stored in a JavaScript variable (
cachedBlueprint). When the user changes the dropdown menu, theInstancedMeshmatrix is instantly recalculated locally. - The Modes:
-
2.5D Exploded Heightmap (Floating): The exact subject requirement. Z-depth is multiplied by 4, and blocks are scaled to
0.9to show floating gaps. -
Voxel Sculpture (Solid Printable): Every block calculates its total physical height (
zLayer * zScale) and extrudes a solid column down to a perfectly flat$Z=0$ backplane. This fulfills the 3D-printable requirement. - Traditional Flat Mosaic: The Z-depth math is entirely ignored, and all blocks are rendered at a uniform height to simulate a standard flat LEGO baseplate.