Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/uploads
/uploads
__pycache__/
*.pyc
Binary file removed 3d-backend/__pycache__/app.cpython-311.pyc
Binary file not shown.
Binary file removed 3d-backend/__pycache__/routes.cpython-311.pyc
Binary file not shown.
59 changes: 52 additions & 7 deletions 3d-backend/routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@


from fastapi import APIRouter, File, UploadFile
from fastapi import APIRouter, File, UploadFile, HTTPException
import shutil
import os
import json
import uuid
from fastapi.responses import JSONResponse
from .utils.opencv_utils import process_blueprint_image, reconstruct_blueprint, remove_text_from_image
import cv2

router = APIRouter()

Expand All @@ -11,11 +14,53 @@

@router.post("/upload")
async def upload_blueprint(file: UploadFile = File(...)):
file_path = os.path.join(UPLOAD_DIR, file.filename)
# Generate unique filename to avoid collisions
unique_id = str(uuid.uuid4())
filename = f"{unique_id}_{file.filename}"
file_path = os.path.join(UPLOAD_DIR, filename)

# Save the file
# Save the uploaded file
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)

# Optionally process the file here (e.g., run CV logic)
return {"filename": file.filename, "message": "File uploaded successfully"}
try:
# First, remove text from the image
img_without_text = remove_text_from_image(file_path)

# Save the text-removed image
text_removed_path = os.path.join(UPLOAD_DIR, f"text_removed_{filename}")
cv2.imwrite(text_removed_path, img_without_text)

# Process the text-removed blueprint image
result = process_blueprint_image(text_removed_path)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))

# Save the processed data as JSON
json_filename = f"{os.path.splitext(filename)[0]}_blueprint.json"
json_path = os.path.join(UPLOAD_DIR, json_filename)
with open(json_path, "w") as json_file:
json.dump({"data": result}, json_file, indent=2)

# Reconstruct the blueprint image from the JSON data
reconstructed_img_path = os.path.join(UPLOAD_DIR, f"reconstructed_{os.path.splitext(filename)[0]}.png")
reconstruct_blueprint(json_path, output_path=reconstructed_img_path)

# Return both the reconstructed image and the JSON data
# We'll encode the image as base64 and include it in the JSON response
import base64
with open(reconstructed_img_path, "rb") as img_file:
encoded_image = base64.b64encode(img_file.read()).decode('utf-8')

# Read the JSON data
with open(json_path, "r") as json_file:
blueprint_data = json.load(json_file)

# Return combined response
response_data = {
"blueprint_data": blueprint_data,
"reconstructed_image": encoded_image,
"image_path": reconstructed_img_path # Optionally include the path if needed
}

return JSONResponse(content=response_data)
Empty file removed 3d-backend/utils.py
Empty file.
Loading