import cv2
import numpy as np
import os

# Create directory
output_dir = r"F:\mydr\slab2025\crack_detection_web\test_images"
os.makedirs(output_dir, exist_ok=True)

def generate_safe_image(filename):
    # Generate safe concrete background (noisy)
    img = np.random.randint(100, 160, (400, 400), dtype=np.uint8)
    img = cv2.GaussianBlur(img, (7, 7), 0)
    # Add some texture (small random dots)
    noise = np.random.randint(0, 50, (400, 400), dtype=np.uint8)
    img = cv2.add(img, noise)
    # Convert to BGR
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    cv2.imwrite(os.path.join(output_dir, filename), img)
    print(f"Generated {filename}")

def generate_cracked_image(filename, crack_type='hairline'):
    # Generate safe concrete background
    img = np.random.randint(100, 160, (400, 400), dtype=np.uint8)
    img = cv2.GaussianBlur(img, (7, 7), 0)
    
    # Draw crack
    if crack_type == 'hairline':
        thickness = 2
        color = 40
    elif crack_type == 'spalling':
        thickness = 6
        color = 20
    else: # shear
        thickness = 15
        color = 10
        
    # Draw a jagged line for the crack
    points = []
    x, y = np.random.randint(50, 100), np.random.randint(50, 100)
    for _ in range(5):
        points.append([x, y])
        x += np.random.randint(30, 80)
        y += np.random.randint(30, 80)
    
    points = np.array(points, np.int32)
    points = points.reshape((-1, 1, 2))
    cv2.polylines(img, [points], isClosed=False, color=(color,), thickness=thickness)
    
    # Add some texture
    noise = np.random.randint(0, 50, (400, 400), dtype=np.uint8)
    img = cv2.add(img, noise)
    
    # Convert to BGR
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    cv2.imwrite(os.path.join(output_dir, filename), img)
    print(f"Generated {filename}")

if __name__ == "__main__":
    print("Generating sample test images...")
    generate_safe_image("safe_concrete_1.jpg")
    generate_safe_image("safe_concrete_2.jpg")
    
    generate_cracked_image("hairline_crack_1.jpg", "hairline")
    generate_cracked_image("spalling_crack_1.jpg", "spalling")
    generate_cracked_image("shear_crack_1.jpg", "shear")
    print("Done!")
