File size: 8,742 Bytes
a3c4a9e | 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | """
Batch Processing Script for Engine Scanning System
Process multiple engine images in a directory
"""
import cv2
import os
from pathlib import Path
import json
from datetime import datetime
import argparse
from app import EngineScanner
from tqdm import tqdm
class BatchProcessor:
"""
Batch processing for multiple engine images
"""
def __init__(self, input_dir, output_dir=None):
self.scanner = EngineScanner()
self.input_dir = Path(input_dir)
if output_dir is None:
self.output_dir = Path("batch_results")
else:
self.output_dir = Path(output_dir)
self.output_dir.mkdir(exist_ok=True)
# Supported image formats
self.image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.tif'}
def get_image_files(self):
"""Get all image files from input directory"""
image_files = []
for ext in self.image_extensions:
image_files.extend(self.input_dir.glob(f'*{ext}'))
image_files.extend(self.input_dir.glob(f'*{ext.upper()}'))
return sorted(image_files)
def process_batch(self, save_images=True, generate_report=True):
"""Process all images in the input directory"""
image_files = self.get_image_files()
if not image_files:
print(f"No images found in {self.input_dir}")
return
print(f"Found {len(image_files)} images to process")
print(f"Output directory: {self.output_dir}")
print()
results = []
stats = {
'total': len(image_files),
'pass': 0,
'warning': 0,
'fail': 0,
'error': 0
}
# Process each image
for img_file in tqdm(image_files, desc="Processing engines"):
try:
# Read image
image = cv2.imread(str(img_file))
if image is None:
print(f"Error reading {img_file.name}")
stats['error'] += 1
continue
# Scan engine
result_image, report = self.scanner.scan_engine(image)
# Extract status from the last scan
if self.scanner.scan_history:
last_scan = self.scanner.scan_history[-1]
status = last_scan['defect_analysis']['status']
stats[status.lower()] += 1
# Save to results
result_data = {
'filename': img_file.name,
'status': status,
'timestamp': last_scan['timestamp'],
'cylinders': last_scan['cylinders'],
'defects': last_scan['defect_analysis']
}
results.append(result_data)
# Save annotated image if requested
if save_images and result_image is not None:
output_path = self.output_dir / f"annotated_{img_file.name}"
cv2.imwrite(str(output_path), result_image)
except Exception as e:
print(f"Error processing {img_file.name}: {str(e)}")
stats['error'] += 1
# Generate batch report
if generate_report:
self.generate_batch_report(results, stats)
return results, stats
def generate_batch_report(self, results, stats):
"""Generate comprehensive batch processing report"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Save JSON report
json_path = self.output_dir / f"batch_report_{timestamp}.json"
report_data = {
'timestamp': timestamp,
'statistics': stats,
'results': results
}
with open(json_path, 'w') as f:
json.dump(report_data, f, indent=2)
# Generate text report
txt_path = self.output_dir / f"batch_report_{timestamp}.txt"
with open(txt_path, 'w') as f:
f.write("="*70 + "\n")
f.write("BATCH PROCESSING REPORT - ENGINE SCANNING SYSTEM\n")
f.write("="*70 + "\n\n")
f.write(f"Processing Date: {timestamp}\n")
f.write(f"Input Directory: {self.input_dir}\n")
f.write(f"Output Directory: {self.output_dir}\n\n")
f.write("-"*70 + "\n")
f.write("SUMMARY STATISTICS\n")
f.write("-"*70 + "\n")
f.write(f"Total Images Processed: {stats['total']}\n")
f.write(f" β PASS: {stats['pass']:3d} ({stats['pass']/stats['total']*100:.1f}%)\n")
f.write(f" β WARNING: {stats['warning']:3d} ({stats['warning']/stats['total']*100:.1f}%)\n")
f.write(f" β FAIL: {stats['fail']:3d} ({stats['fail']/stats['total']*100:.1f}%)\n")
f.write(f" ! ERROR: {stats['error']:3d} ({stats['error']/stats['total']*100:.1f}%)\n\n")
f.write("-"*70 + "\n")
f.write("DETAILED RESULTS\n")
f.write("-"*70 + "\n\n")
# Sort by status (FAIL first, then WARNING, then PASS)
status_order = {'FAIL': 0, 'WARNING': 1, 'PASS': 2}
sorted_results = sorted(results, key=lambda x: status_order.get(x['status'], 3))
for result in sorted_results:
status_symbol = {
'PASS': 'β',
'WARNING': 'β ',
'FAIL': 'β'
}.get(result['status'], '?')
f.write(f"{status_symbol} {result['status']:8s} | {result['filename']}\n")
f.write(f" Cylinders: {result['cylinders']}\n")
f.write(f" Defects: {result['defects']['defect_count']} "
f"({result['defects']['defect_percentage']:.2f}%)\n")
f.write("\n")
f.write("="*70 + "\n")
f.write("END OF REPORT\n")
f.write("="*70 + "\n")
print(f"\nβ Batch report saved:")
print(f" - JSON: {json_path}")
print(f" - Text: {txt_path}")
# Print summary to console
print("\n" + "="*70)
print("BATCH PROCESSING COMPLETE")
print("="*70)
print(f"Total: {stats['total']}")
print(f"β PASS: {stats['pass']:3d} ({stats['pass']/stats['total']*100:.1f}%)")
print(f"β WARNING: {stats['warning']:3d} ({stats['warning']/stats['total']*100:.1f}%)")
print(f"β FAIL: {stats['fail']:3d} ({stats['fail']/stats['total']*100:.1f}%)")
print(f"! ERROR: {stats['error']:3d} ({stats['error']/stats['total']*100:.1f}%)")
print("="*70 + "\n")
def main():
parser = argparse.ArgumentParser(
description='Batch process engine images for quality control',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Process all images in a directory
python batch_process.py input_images/
# Process with custom output directory
python batch_process.py input_images/ -o results/
# Process without saving annotated images (faster)
python batch_process.py input_images/ --no-save-images
# Process without generating report (save time)
python batch_process.py input_images/ --no-report
"""
)
parser.add_argument(
'input_dir',
help='Directory containing engine images to process'
)
parser.add_argument(
'-o', '--output-dir',
help='Output directory for results (default: batch_results/)',
default=None
)
parser.add_argument(
'--no-save-images',
action='store_true',
help='Do not save annotated images (only generate report)'
)
parser.add_argument(
'--no-report',
action='store_true',
help='Do not generate batch report'
)
args = parser.parse_args()
# Validate input directory
if not os.path.isdir(args.input_dir):
print(f"Error: Input directory '{args.input_dir}' does not exist")
return 1
# Create processor
processor = BatchProcessor(args.input_dir, args.output_dir)
# Process batch
results, stats = processor.process_batch(
save_images=not args.no_save_images,
generate_report=not args.no_report
)
return 0
if __name__ == "__main__":
exit(main()) |