Download composite-defect-detector/generate_sample.py from randomnameplease/DefectDetection: direct link, hf CLI and curl.
- Browser
- Download file 5.36 kB
-
https://huggingface.co/randomnameplease/DefectDetection/resolve/bbb083f4e4130af04e257cdde2dee2208973e4f5/composite-defect-detector/generate_sample.py
- Command line
-
hf download hf://randomnameplease/DefectDetection@bbb083f4e4130af04e257cdde2dee2208973e4f5/composite-defect-detector/generate_sample.py
-
curl -L -o generate_sample.py https://huggingface.co/randomnameplease/DefectDetection/resolve/bbb083f4e4130af04e257cdde2dee2208973e4f5/composite-defect-detector/generate_sample.py
5.36 kB
| """ | |
| 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() | |