Spaces:
Runtime error
Runtime error
File size: 5,360 Bytes
bc73eb8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | """
Sample image generator for testing CompositeDefect AI
Generates synthetic NDT-style images with simulated defects
"""
import numpy as np
from PIL import Image, ImageDraw
import random
def generate_ultrasonic_cscan(size=(512, 512), num_defects=5):
"""Generate a synthetic ultrasonic C-scan image"""
# Create base gradient background
img = np.zeros((size[1], size[0]), dtype=np.uint8)
# Add gradient
for y in range(size[1]):
img[y, :] = int(128 + 50 * np.sin(y / 50))
# Add noise
noise = np.random.normal(0, 10, (size[1], size[0])).astype(np.int16)
img = np.clip(img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
# Add simulated defects (darker regions)
img_color = np.stack([img, img, img], axis=2)
pil_img = Image.fromarray(img_color)
draw = ImageDraw.Draw(pil_img)
for _ in range(num_defects):
x = random.randint(50, size[0] - 100)
y = random.randint(50, size[1] - 100)
w = random.randint(30, 80)
h = random.randint(30, 80)
# Draw dark ellipse for defect
draw.ellipse([x, y, x+w, y+h], fill=(40, 40, 60))
return pil_img
def generate_thermal_image(size=(512, 512), num_hotspots=4):
"""Generate a synthetic thermal/infrared image"""
img = np.zeros((size[1], size[0], 3), dtype=np.uint8)
# Base temperature (cool = blue)
img[:, :] = [30, 30, 80]
# Add hotspots (warm = red/yellow)
pil_img = Image.fromarray(img)
draw = ImageDraw.Draw(pil_img)
for _ in range(num_hotspots):
x = random.randint(50, size[0] - 100)
y = random.randint(50, size[1] - 100)
radius = random.randint(20, 60)
# Hotspot gradient
for r in range(radius, 0, -2):
intensity = int(255 * (1 - r/radius))
color = (255, intensity, 0) if intensity > 128 else (255, 0, 255-intensity)
draw.ellipse([x-r, y-r, x+r, y+r], fill=color)
return pil_img
def generate_radiograph(size=(512, 512), num_defects=3):
"""Generate a synthetic X-ray/radiographic image"""
# Base grey
img = np.full((size[1], size[0]), 180, dtype=np.uint8)
# Add structure lines (fibre orientation)
for i in range(0, size[0], 20):
img[:, i:i+2] = np.clip(img[:, i:i+2] - 20, 0, 255)
# Add defects (darker regions = less dense)
pil_img = Image.fromarray(img)
draw = ImageDraw.Draw(pil_img)
for _ in range(num_defects):
x = random.randint(50, size[0] - 150)
y = random.randint(50, size[1] - 150)
# Crack-like defect
if random.random() > 0.5:
x2 = x + random.randint(50, 150)
y2 = y + random.randint(-30, 30)
draw.line([(x, y), (x2, y2)], fill=80, width=3)
else:
# Void
w = random.randint(20, 50)
h = random.randint(20, 50)
draw.ellipse([x, y, x+w, y+h], fill=100)
return pil_img
def generate_visual_inspection(size=(512, 512), num_defects=4):
"""Generate a synthetic visual inspection image"""
# Carbon fibre weave pattern
img = np.zeros((size[1], size[0], 3), dtype=np.uint8)
img[:, :] = [40, 40, 40] # Dark carbon fibre base
# Add weave pattern
for i in range(0, size[0], 8):
img[:, i:i+4] = [60, 60, 60]
for i in range(0, size[1], 8):
img[i:i+4, :] = [50, 50, 50]
pil_img = Image.fromarray(img)
draw = ImageDraw.Draw(pil_img)
for _ in range(num_defects):
x = random.randint(50, size[0] - 100)
y = random.randint(50, size[1] - 100)
defect_type = random.choice(['delamination', 'scratch', 'discoloration'])
if defect_type == 'delamination':
# Delamination - lighter area
w = random.randint(40, 100)
h = random.randint(40, 100)
draw.ellipse([x, y, x+w, y+h], fill=(100, 100, 100, 128))
elif defect_type == 'scratch':
# Scratch line
x2 = x + random.randint(30, 100)
y2 = y + random.randint(-20, 20)
draw.line([(x, y), (x2, y2)], fill=(150, 150, 150), width=2)
else:
# Discoloration
w = random.randint(30, 80)
h = random.randint(30, 80)
draw.rectangle([x, y, x+w, y+h], fill=(80, 60, 40))
return pil_img
def main():
"""Generate sample images for testing"""
print("π¬ Generating sample NDT images for testing...")
# Generate different types of NDT images
ultrasonic = generate_ultrasonic_cscan(num_defects=6)
ultrasonic.save('sample_ultrasonic.png')
print("β
Generated: sample_ultrasonic.png")
thermal = generate_thermal_image(num_hotspots=5)
thermal.save('sample_thermal.png')
print("β
Generated: sample_thermal.png")
radiograph = generate_radiograph(num_defects=4)
radiograph.save('sample_radiograph.png')
print("β
Generated: sample_radiograph.png")
visual = generate_visual_inspection(num_defects=5)
visual.save('sample_visual.png')
print("β
Generated: sample_visual.png")
print("\nπ Sample images saved in current directory")
print("π You can now test the CompositeDefect AI application!")
if __name__ == "__main__":
main()
|