Spaces:
Running
Running
Upload generate_kolam_demo.py
Browse files- generate_kolam_demo.py +109 -0
generate_kolam_demo.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Simple script to generate a Kolam image using the AI generator.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
# Add project paths
|
| 12 |
+
sys.path.insert(0, str(Path.cwd()))
|
| 13 |
+
sys.path.insert(0, str(Path.cwd() / 'models'))
|
| 14 |
+
|
| 15 |
+
from models.gan_generator import KolamGenerator
|
| 16 |
+
from utils.image_utils import create_synthetic_kolam
|
| 17 |
+
from utils.metrics import KolamDesignMetrics
|
| 18 |
+
|
| 19 |
+
def generate_ai_kolam():
|
| 20 |
+
"""Generate a Kolam image using the AI generator."""
|
| 21 |
+
print("π¨ Generating AI Kolam Design...")
|
| 22 |
+
|
| 23 |
+
# Create the generator
|
| 24 |
+
generator = KolamGenerator(
|
| 25 |
+
noise_dim=100,
|
| 26 |
+
feature_dim=128,
|
| 27 |
+
output_channels=1,
|
| 28 |
+
image_size=64
|
| 29 |
+
)
|
| 30 |
+
generator.eval()
|
| 31 |
+
|
| 32 |
+
# Generate random noise
|
| 33 |
+
noise = torch.randn(1, 100)
|
| 34 |
+
|
| 35 |
+
# Generate the Kolam
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
generated_kolam = generator(noise)
|
| 38 |
+
|
| 39 |
+
# Convert to numpy and normalize
|
| 40 |
+
kolam_image = generated_kolam.squeeze().cpu().numpy()
|
| 41 |
+
kolam_image = (kolam_image + 1) / 2 # Convert from [-1, 1] to [0, 1]
|
| 42 |
+
kolam_image = np.clip(kolam_image, 0, 1)
|
| 43 |
+
|
| 44 |
+
return kolam_image
|
| 45 |
+
|
| 46 |
+
def generate_synthetic_kolam():
|
| 47 |
+
"""Generate a synthetic Kolam using traditional patterns."""
|
| 48 |
+
print("π¨ Generating Synthetic Kolam Design...")
|
| 49 |
+
|
| 50 |
+
# Create a synthetic Kolam with medium complexity
|
| 51 |
+
kolam_image = create_synthetic_kolam(size=(64, 64), complexity='medium')
|
| 52 |
+
|
| 53 |
+
return kolam_image
|
| 54 |
+
|
| 55 |
+
def analyze_kolam_quality(kolam_image):
|
| 56 |
+
"""Analyze the quality of the generated Kolam."""
|
| 57 |
+
print("π Analyzing Kolam Quality...")
|
| 58 |
+
|
| 59 |
+
metrics = KolamDesignMetrics()
|
| 60 |
+
quality = metrics.calculate_overall_quality(kolam_image)
|
| 61 |
+
|
| 62 |
+
print(f"Overall Quality Score: {quality['overall_quality']:.3f}")
|
| 63 |
+
print(f"Horizontal Symmetry: {quality['horizontal']:.3f}")
|
| 64 |
+
print(f"Vertical Symmetry: {quality['vertical']:.3f}")
|
| 65 |
+
print(f"Complexity: {quality['complexity']:.3f}")
|
| 66 |
+
print(f"Balance: {quality['balance']:.3f}")
|
| 67 |
+
print(f"Rhythm: {quality['rhythm']:.3f}")
|
| 68 |
+
|
| 69 |
+
return quality
|
| 70 |
+
|
| 71 |
+
def display_kolam(kolam_image, title="Generated Kolam Design"):
|
| 72 |
+
"""Display the Kolam image."""
|
| 73 |
+
plt.figure(figsize=(8, 8))
|
| 74 |
+
plt.imshow(kolam_image, cmap='gray', vmin=0, vmax=1)
|
| 75 |
+
plt.title(title, fontsize=16, fontweight='bold')
|
| 76 |
+
plt.axis('off')
|
| 77 |
+
|
| 78 |
+
# Save the image
|
| 79 |
+
output_dir = Path('data/generated')
|
| 80 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 81 |
+
|
| 82 |
+
plt.savefig(output_dir / 'generated_kolam.png', dpi=300, bbox_inches='tight')
|
| 83 |
+
print(f"β
Kolam saved to: {output_dir / 'generated_kolam.png'}")
|
| 84 |
+
|
| 85 |
+
plt.show()
|
| 86 |
+
|
| 87 |
+
def main():
|
| 88 |
+
"""Main function to generate and display Kolam."""
|
| 89 |
+
print("π KOLAM AI GENERATOR - IMAGE GENERATION")
|
| 90 |
+
print("=" * 50)
|
| 91 |
+
|
| 92 |
+
# Generate AI Kolam
|
| 93 |
+
ai_kolam = generate_ai_kolam()
|
| 94 |
+
print("β
AI Kolam generated!")
|
| 95 |
+
|
| 96 |
+
# Analyze quality
|
| 97 |
+
quality = analyze_kolam_quality(ai_kolam)
|
| 98 |
+
|
| 99 |
+
# Display the Kolam
|
| 100 |
+
display_kolam(ai_kolam, "AI Generated Kolam Design")
|
| 101 |
+
|
| 102 |
+
print("\nπ Kolam generation complete!")
|
| 103 |
+
print("\nTo generate more Kolams:")
|
| 104 |
+
print("1. Run this script again: python generate_kolam_demo.py")
|
| 105 |
+
print("2. Use the Jupyter notebooks for interactive generation")
|
| 106 |
+
print("3. Train the models with real data for better results")
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
main()
|