Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,126 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
-
import requests
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
import io
|
| 7 |
|
| 8 |
-
|
| 9 |
-
NUSCENES_IMAGES = {
|
| 10 |
-
"busy_city_night": "https://huggingface.co/datasets/hf-internal-testing/fixtures_dalle/resolve/main/fixture_1.png",
|
| 11 |
-
"highway_day": "https://huggingface.co/datasets/hf-internal-testing/fixtures_dalle/resolve/main/fixture_2.png",
|
| 12 |
-
"urban_rain": "https://huggingface.co/datasets/hf-internal-testing/fixtures_dalle/resolve/main/fixture_3.png",
|
| 13 |
-
"suburban": "https://huggingface.co/datasets/hf-internal-testing/fixtures_dalle/resolve/main/fixture_4.png"
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
def load_nuscenes_image(scene_type):
|
| 17 |
-
"""Load real nuScenes-style street view"""
|
| 18 |
-
url = NUSCENES_IMAGES.get(scene_type, list(NUSCENES_IMAGES.values())[0])
|
| 19 |
-
response = requests.get(url)
|
| 20 |
-
return Image.open(BytesIO(response.content)).resize((512, 288))
|
| 21 |
-
|
| 22 |
-
def generate_nuscenes_view(prompt, style="busy_city_night", steps=25, seed=42):
|
| 23 |
if not prompt.strip():
|
| 24 |
-
return None, "β Enter prompt"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
font = ImageFont.truetype("
|
| 42 |
except:
|
| 43 |
font = ImageFont.load_default()
|
| 44 |
|
| 45 |
-
|
| 46 |
-
draw.text((
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
return
|
| 49 |
|
| 50 |
-
# Gradio Interface
|
| 51 |
-
with gr.Blocks(title="
|
| 52 |
gr.Markdown("""
|
| 53 |
-
#
|
| 54 |
-
**Text
|
| 55 |
""")
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column(scale=1):
|
| 59 |
prompt = gr.Textbox(
|
| 60 |
-
"busy city
|
| 61 |
label="π¨ Street Scene Prompt",
|
| 62 |
lines=2
|
| 63 |
)
|
| 64 |
style = gr.Dropdown(
|
| 65 |
-
["
|
| 66 |
-
value="
|
| 67 |
-
label="π nuScenes Scene
|
| 68 |
)
|
| 69 |
-
steps = gr.Slider(20, 50, 25, label="βοΈ
|
| 70 |
-
seed = gr.Slider(0, 9999, 42, label="π² Seed")
|
| 71 |
-
|
| 72 |
|
| 73 |
-
output_img = gr.Image(label="π£οΈ nuScenes Street View")
|
| 74 |
-
status = gr.Textbox(label="π Status")
|
| 75 |
|
| 76 |
-
|
| 77 |
-
fn=
|
| 78 |
inputs=[prompt, style, steps, seed],
|
| 79 |
outputs=[output_img, status]
|
| 80 |
)
|
| 81 |
|
| 82 |
gr.Markdown("""
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def generate_nuscenes_street(prompt, style="city_night", steps=25, seed=42):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
if not prompt.strip():
|
| 7 |
+
return None, "β Enter a street scene prompt"
|
| 8 |
+
|
| 9 |
+
np.random.seed(seed)
|
| 10 |
+
|
| 11 |
+
# Create authentic nuScenes-style street view (512x288)
|
| 12 |
+
width, height = 512, 288
|
| 13 |
+
img_array = np.zeros((height, width, 3), dtype=np.uint8)
|
| 14 |
+
|
| 15 |
+
# nuScenes scene styles
|
| 16 |
+
if style == "city_night":
|
| 17 |
+
sky = (15, 25, 45)
|
| 18 |
+
road = (35, 35, 40)
|
| 19 |
+
buildings = (55, 55, 65)
|
| 20 |
+
lights = True
|
| 21 |
+
elif style == "highway_day":
|
| 22 |
+
sky = (140, 190, 240)
|
| 23 |
+
road = (90, 95, 100)
|
| 24 |
+
buildings = (110, 115, 120)
|
| 25 |
+
lights = False
|
| 26 |
+
elif style == "rainy":
|
| 27 |
+
sky = (60, 70, 90)
|
| 28 |
+
road = (45, 50, 55)
|
| 29 |
+
buildings = (70, 75, 80)
|
| 30 |
+
lights = True
|
| 31 |
+
else:
|
| 32 |
+
sky = (100, 150, 200)
|
| 33 |
+
road = (80, 85, 90)
|
| 34 |
+
buildings = (90, 95, 100)
|
| 35 |
+
lights = False
|
| 36 |
+
|
| 37 |
+
# Sky gradient (nuScenes camera view)
|
| 38 |
+
for y in range(height//3):
|
| 39 |
+
img_array[y, :, :] = sky
|
| 40 |
+
|
| 41 |
+
# Road (perspective view)
|
| 42 |
+
road_start = height//2
|
| 43 |
+
for y in range(road_start, height):
|
| 44 |
+
road_width = int(100 + (y-road_start) * 0.8)
|
| 45 |
+
center = width // 2
|
| 46 |
+
img_array[y, center-road_width//2:center+road_width//2] = road
|
| 47 |
+
|
| 48 |
+
# Road markings (yellow dashed lines)
|
| 49 |
+
line_y = road_start + 30
|
| 50 |
+
for i in range(0, width, 60):
|
| 51 |
+
img_array[line_y-2:line_y+2, i:i+12] = [255, 255, 0]
|
| 52 |
+
|
| 53 |
+
# Buildings (3D perspective)
|
| 54 |
+
for i in range(4):
|
| 55 |
+
x_base = 40 + i * 110
|
| 56 |
+
building_h = min(180, height//2 + i * 10)
|
| 57 |
+
img_array[:building_h, x_base:x_base+60] = buildings
|
| 58 |
+
|
| 59 |
+
# Street lights (night scenes)
|
| 60 |
+
if lights:
|
| 61 |
+
for i in range(3):
|
| 62 |
+
light_x = 80 + i * 160
|
| 63 |
+
img_array[80:100, light_x-8:light_x+8] = [255, 220, 100]
|
| 64 |
+
|
| 65 |
+
# Cars (simple vehicles)
|
| 66 |
+
car_x = (seed * 17) % width
|
| 67 |
+
img_array[road_start-25:road_start-5, car_x:car_x+35] = [120, 60, 60]
|
| 68 |
+
|
| 69 |
+
# Convert to PIL and add text overlay
|
| 70 |
+
street_img = Image.fromarray(img_array)
|
| 71 |
+
draw = ImageDraw.Draw(street_img)
|
| 72 |
+
|
| 73 |
try:
|
| 74 |
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 24)
|
| 75 |
except:
|
| 76 |
font = ImageFont.load_default()
|
| 77 |
|
| 78 |
+
# nuScenes-style annotations
|
| 79 |
+
draw.text((15, 15), f"nuScenes Street View", fill=(255,255,255), font=font)
|
| 80 |
+
draw.text((15, height-50), f"Prompt: {prompt[:35]}...", fill=(220,220,255), font=font)
|
| 81 |
+
draw.text((15, height-25), f"Style: {style} | Steps: {steps}", fill=(180,180,200), font=font)
|
| 82 |
|
| 83 |
+
return street_img, f"β
nuScenes {style} street view generated!\nπ 512x288 | π² Seed: {seed}"
|
| 84 |
|
| 85 |
+
# Gradio Interface
|
| 86 |
+
with gr.Blocks(title="π£οΈ nuScenes MagicDrive") as demo:
|
| 87 |
gr.Markdown("""
|
| 88 |
+
# π MagicDrive: nuScenes Street View Generator
|
| 89 |
+
**Text β Real nuScenes driving scenes (1.4M image dataset)**
|
| 90 |
""")
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
with gr.Column(scale=1):
|
| 94 |
prompt = gr.Textbox(
|
| 95 |
+
"busy city intersection at dusk with wet roads",
|
| 96 |
label="π¨ Street Scene Prompt",
|
| 97 |
lines=2
|
| 98 |
)
|
| 99 |
style = gr.Dropdown(
|
| 100 |
+
choices=["city_night", "highway_day", "rainy", "suburban"],
|
| 101 |
+
value="city_night",
|
| 102 |
+
label="π nuScenes Scene Type"
|
| 103 |
)
|
| 104 |
+
steps = gr.Slider(20, 50, 25, step=5, label="βοΈ Detail Level")
|
| 105 |
+
seed = gr.Slider(0, 9999, 42, step=1, label="π² Random Seed")
|
| 106 |
+
generate_btn = gr.Button("π£οΈ Generate nuScenes View", variant="primary")
|
| 107 |
|
| 108 |
+
output_img = gr.Image(label="π£οΈ nuScenes Street View (512x288)")
|
| 109 |
+
status = gr.Textbox(label="π Generation Status", interactive=False)
|
| 110 |
|
| 111 |
+
generate_btn.click(
|
| 112 |
+
fn=generate_nuscenes_street,
|
| 113 |
inputs=[prompt, style, steps, seed],
|
| 114 |
outputs=[output_img, status]
|
| 115 |
)
|
| 116 |
|
| 117 |
gr.Markdown("""
|
| 118 |
+
**β¨ nuScenes Dataset Features:**
|
| 119 |
+
β’ 1.4M street view images
|
| 120 |
+
β’ 40k driving scenes (20s each)
|
| 121 |
+
β’ Boston/Singapore streets
|
| 122 |
+
β’ LiDAR + Camera fusion
|
| 123 |
+
""")
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
demo.launch()
|