File size: 955 Bytes
7b74407
 
 
 
 
1c817fd
 
 
7b74407
1c817fd
 
7b74407
 
 
1c817fd
7b74407
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
```

```python
--- START OF FILE image_to_3d_api.py ---
import os, uuid
from flask import jsonify, send_file, request
from main import *
from PIL import Image
import torch, numpy as np, io, base64

def image_to_3d_func(image_path, output_path="output_3d.obj"):
    if image_to_3d_model is None: return {"error": "Image-to-3D model not initialized."}
    pil_image = Image.open(image_path).convert("RGB"); image = torch.tensor(np.array(pil_image)).float().permute(2,0,1).unsqueeze(0) / 255.0; image = image.to(device)
    with torch.no_grad(): mesh_obj = image_to_3d_model(image); return {"model_3d": mesh_obj}

def image_to_3d_api(image_path):
    output = image_to_3d_func(image_path)
    if "error" in output: return {"error": output["error"]}
    model_3d_base64 = base64.b64encode(output['model_3d'].encode('utf-8')).decode('utf-8'); return {"model_3d_base64": model_3d_base64, "mimetype": "model/obj", "filename": "output_3d.obj"}