Spaces:
Paused
Paused
sachin
commited on
Commit
·
475ca6b
1
Parent(s):
d70006d
add edit
Browse files- Dockerfile +1 -1
- blender_script.py +58 -0
- main.py +91 -0
- requirements.txt +4 -1
Dockerfile
CHANGED
@@ -35,4 +35,4 @@ USER appuser
|
|
35 |
EXPOSE 7860
|
36 |
|
37 |
# Run the server
|
38 |
-
CMD ["python", "/app/
|
|
|
35 |
EXPOSE 7860
|
36 |
|
37 |
# Run the server
|
38 |
+
CMD ["python", "/app/main.py"]
|
blender_script.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import bpy
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
from mathutils import Vector
|
5 |
+
|
6 |
+
def clear_scene():
|
7 |
+
bpy.ops.object.select_all(action="SELECT")
|
8 |
+
bpy.ops.object.delete()
|
9 |
+
|
10 |
+
def setup_scene(objects, environment):
|
11 |
+
clear_scene()
|
12 |
+
# Load environment (e.g., desert)
|
13 |
+
bpy.ops.wm.open_mainfile(filepath=f"models/{environment}.blend")
|
14 |
+
|
15 |
+
# Import military assets
|
16 |
+
for obj in objects:
|
17 |
+
bpy.ops.import_scene.obj(filepath=f"models/{obj}.obj")
|
18 |
+
|
19 |
+
# Setup camera
|
20 |
+
bpy.ops.object.camera_add(location=(0, -10, 5), rotation=(1.0, 0, 0))
|
21 |
+
bpy.context.scene.camera = bpy.context.object
|
22 |
+
|
23 |
+
def render_image(output_dir, image_id):
|
24 |
+
bpy.context.scene.render.filepath = os.path.join(output_dir, f"image_{image_id}.png")
|
25 |
+
bpy.ops.render.render(write_still=True)
|
26 |
+
|
27 |
+
# Generate labels (bounding boxes)
|
28 |
+
labels = []
|
29 |
+
for obj in bpy.data.objects:
|
30 |
+
if obj.type == "MESH":
|
31 |
+
# Project 3D bounds to 2D
|
32 |
+
coords_2d = [bpy.context.scene.camera.matrix_world @ Vector(corner) for corner in obj.bound_box]
|
33 |
+
coords_2d = [bpy_extras.object_utils.world_to_camera_view(bpy.context.scene, bpy.context.scene.camera, coord) for coord in coords_2d]
|
34 |
+
x_coords = [coord.x * bpy.context.scene.render.resolution_x for coord in coords_2d]
|
35 |
+
y_coords = [(1 - coord.y) * bpy.context.scene.render.resolution_y for coord in coords_2d] # Invert y-axis
|
36 |
+
x_min, x_max = min(x_coords), max(x_coords)
|
37 |
+
y_min, y_max = min(y_coords), max(y_coords)
|
38 |
+
labels.append({
|
39 |
+
"category": obj.name,
|
40 |
+
"bbox": [x_min, y_min, x_max - x_min, y_max - y_min]
|
41 |
+
})
|
42 |
+
return labels
|
43 |
+
|
44 |
+
def generate_images(objects, environment, num_images, output_dir):
|
45 |
+
setup_scene(objects, environment)
|
46 |
+
annotations = []
|
47 |
+
for i in range(num_images):
|
48 |
+
# Randomize object positions, camera angles, etc. (simplified here)
|
49 |
+
labels = render_image(output_dir, i)
|
50 |
+
annotations.append({"image_id": i, "file_name": f"image_{i}.png", "labels": labels})
|
51 |
+
|
52 |
+
with open(os.path.join(output_dir, "annotations.json"), "w") as f:
|
53 |
+
json.dump(annotations, f)
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
import sys
|
57 |
+
objects, env, num, out_dir = sys.argv[1], sys.argv[2], int(sys.argv[3]), sys.argv[4]
|
58 |
+
generate_images(objects.split(","), env, num, out_dir)
|
main.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import FileResponse
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
import shutil
|
7 |
+
import tempfile
|
8 |
+
import zipfile
|
9 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline
|
10 |
+
import torch
|
11 |
+
from PIL import Image
|
12 |
+
import json
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
# Load InstructPix2Pix model
|
16 |
+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
17 |
+
"timm/instruct-pix2pix",
|
18 |
+
torch_dtype=torch.float16,
|
19 |
+
safety_checker=None,
|
20 |
+
).to("cuda")
|
21 |
+
|
22 |
+
class DatasetRequest(BaseModel):
|
23 |
+
objects: list[str]
|
24 |
+
environment: str
|
25 |
+
num_images: int
|
26 |
+
augmentation_prompts: list[str]
|
27 |
+
|
28 |
+
def augment_image(image_path, prompt):
|
29 |
+
image = Image.open(image_path).convert("RGB")
|
30 |
+
augmented = pipe(prompt=prompt, image=image, num_inference_steps=20, image_guidance_scale=1.5).images[0]
|
31 |
+
return augmented
|
32 |
+
|
33 |
+
@app.post("/generate_dataset")
|
34 |
+
async def generate_dataset(request: DatasetRequest):
|
35 |
+
try:
|
36 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
37 |
+
# Step 1: Generate base images with Blender
|
38 |
+
base_dir = os.path.join(tmpdirname, "base")
|
39 |
+
os.makedirs(base_dir)
|
40 |
+
subprocess.run([
|
41 |
+
"blender", "--background", "--python", "blender_script.py", "--",
|
42 |
+
",".join(request.objects), request.environment, str(request.num_images), base_dir
|
43 |
+
], check=True)
|
44 |
+
|
45 |
+
# Load base annotations
|
46 |
+
with open(os.path.join(base_dir, "annotations.json"), "r") as f:
|
47 |
+
base_annotations = json.load(f)
|
48 |
+
|
49 |
+
# Step 2: Augment images
|
50 |
+
output_dir = os.path.join(tmpdirname, "output/images")
|
51 |
+
os.makedirs(output_dir)
|
52 |
+
annotations = []
|
53 |
+
image_id = 0
|
54 |
+
for base_anno in base_annotations:
|
55 |
+
base_image_path = os.path.join(base_dir, base_anno["file_name"])
|
56 |
+
for prompt in request.augmentation_prompts:
|
57 |
+
augmented = augment_image(base_image_path, prompt)
|
58 |
+
new_filename = f"image_{image_id}.png"
|
59 |
+
augmented.save(os.path.join(output_dir, new_filename))
|
60 |
+
annotations.append({
|
61 |
+
"image_id": image_id,
|
62 |
+
"file_name": new_filename,
|
63 |
+
"labels": base_anno["labels"]
|
64 |
+
})
|
65 |
+
image_id += 1
|
66 |
+
|
67 |
+
# Save annotations
|
68 |
+
anno_file = os.path.join(tmpdirname, "output/annotations.json")
|
69 |
+
with open(anno_file, "w") as f:
|
70 |
+
json.dump(annotations, f)
|
71 |
+
|
72 |
+
# Step 3: Create zip file
|
73 |
+
zip_path = os.path.join(tmpdirname, "dataset.zip")
|
74 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
75 |
+
for root, _, files in os.walk(output_dir):
|
76 |
+
for file in files:
|
77 |
+
zipf.write(os.path.join(root, file), os.path.join("images", file))
|
78 |
+
zipf.write(anno_file, "annotations.json")
|
79 |
+
|
80 |
+
return FileResponse(zip_path, media_type="application/zip", filename="dataset.zip")
|
81 |
+
except Exception as e:
|
82 |
+
raise HTTPException(status_code=500, detail=str(e))
|
83 |
+
|
84 |
+
@app.get("/health")
|
85 |
+
async def health_check():
|
86 |
+
return {"status": "healthy"}
|
87 |
+
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
import uvicorn
|
91 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
@@ -4,4 +4,7 @@ torch
|
|
4 |
diffusers
|
5 |
huggingface_hub
|
6 |
safetensors
|
7 |
-
transformers
|
|
|
|
|
|
|
|
4 |
diffusers
|
5 |
huggingface_hub
|
6 |
safetensors
|
7 |
+
transformers
|
8 |
+
pillow
|
9 |
+
numpy
|
10 |
+
blenderproc
|