-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_folders.py
More file actions
245 lines (201 loc) · 8.46 KB
/
setup_folders.py
File metadata and controls
245 lines (201 loc) · 8.46 KB
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
#!/usr/bin/env python3
"""
Setup and Test Script for Folder-Based Detection
This script helps set up the folder structure and test the
automatic folder-based damage detection system.
Author: AI Assistant
Date: June 2025
"""
import os
import shutil
from pathlib import Path
import requests
import json
def create_folder_structure():
"""Create the complete folder structure for organized processing."""
print("🏗️ Creating folder structure...")
base_folders = [
"input_images", "output_images", "processed_images",
"reports", "logs", "temp"
]
damage_types = [
"undamaged", "damaged", "tear", "crease", "stain",
"fade", "burn", "water_damage", "scratches", "holes", "wrinkles"
]
# Create main folders
for folder in base_folders:
Path(folder).mkdir(exist_ok=True)
# Create damage type subfolders
for main_folder in ["input_images", "output_images", "processed_images"]:
for damage_type in damage_types:
(Path(main_folder) / damage_type).mkdir(exist_ok=True)
# Create report subfolders
report_subfolders = ["batch_reports", "individual", "summaries"]
for subfolder in report_subfolders:
(Path("reports") / subfolder).mkdir(exist_ok=True)
# Create temp subfolders
temp_subfolders = ["resized", "cache"]
for subfolder in temp_subfolders:
(Path("temp") / subfolder).mkdir(exist_ok=True)
print("✅ Folder structure created successfully!")
def download_sample_images():
"""Download some sample images for testing."""
print("📥 Downloading sample images...")
# Sample images (Creative Commons/Public Domain)
sample_images = [
{
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Vintage_poster_art.jpg/512px-Vintage_poster_art.jpg",
"filename": "vintage_poster_1.jpg",
"folder": "undamaged"
},
{
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Old_poster.jpg/512px-Old_poster.jpg",
"filename": "old_poster_1.jpg",
"folder": "damaged"
}
]
for image_info in sample_images:
try:
folder_path = Path("input_images") / image_info["folder"]
file_path = folder_path / image_info["filename"]
if not file_path.exists():
print(f" Downloading {image_info['filename']}...")
response = requests.get(image_info["url"], timeout=30)
response.raise_for_status()
with open(file_path, 'wb') as f:
f.write(response.content)
print(f" ✅ Saved: {file_path}")
else:
print(f" ⏭️ Already exists: {file_path}")
except Exception as e:
print(f" ❌ Failed to download {image_info['filename']}: {e}")
print("📥 Sample image download completed!")
def create_sample_config():
"""Create a sample configuration file."""
config = {
"detection_settings": {
"confidence_threshold": 0.3,
"device": "auto",
"batch_size": 1
},
"folder_settings": {
"auto_organize": True,
"save_annotations": True,
"backup_processed": True
},
"monitoring": {
"watch_interval": 30,
"log_level": "INFO",
"enable_notifications": False
},
"output_formats": {
"save_json_reports": True,
"save_annotated_images": True,
"image_annotation_style": "bbox_and_label"
}
}
with open("detection_config.json", "w") as f:
json.dump(config, f, indent=2)
print("⚙️ Created detection_config.json")
def test_folder_detection():
"""Test the folder-based detection system."""
print("\n🧪 Testing folder-based detection...")
try:
from pretrained_damage_detector import PretrainedDamageDetector
print(" Initializing detector...")
detector = PretrainedDamageDetector(device='cpu') # Use CPU for testing
print(" Scanning input folder...")
image_files = detector.scan_input_folder()
if image_files:
print(f" Found {len(image_files)} images to process")
print(" Running automatic processing...")
summary = detector.process_folder_automatically(
auto_organize=True,
save_annotations=True
)
print(f" ✅ Processing completed!")
print(f" - Images found: {summary['total_images_found']}")
print(f" - Successfully processed: {summary['successfully_processed']}")
if summary['successfully_processed'] > 0:
print(f"\n📁 Check these folders for results:")
print(f" - output_images/ (annotated images)")
print(f" - reports/batch_reports/ (analysis reports)")
print(f" - logs/ (processing logs)")
else:
print(" ⚠️ No images found in input folders")
print(" Add some images to input_images/ subfolders and try again")
except ImportError:
print(" ❌ Could not import PretrainedDamageDetector")
print(" Make sure to install requirements: pip install -r requirements.txt")
except Exception as e:
print(f" ❌ Test failed: {e}")
def show_usage_examples():
"""Show usage examples for the folder-based system."""
print("\n" + "="*60)
print("🎯 FOLDER-BASED DETECTION USAGE")
print("="*60)
examples = [
{
"title": "Automatic Processing",
"command": "python3 pretrained_damage_detector.py --auto-folder",
"description": "Process all images in input_images/ and organize results"
},
{
"title": "Watch Mode",
"command": "python3 pretrained_damage_detector.py --watch",
"description": "Continuously monitor input_images/ for new files"
},
{
"title": "Manual Organization",
"command": "python3 pretrained_damage_detector.py --auto-folder --no-auto-organize",
"description": "Process images but don't auto-organize by damage type"
},
{
"title": "Without Annotations",
"command": "python3 pretrained_damage_detector.py --auto-folder --no-save-annotations",
"description": "Process images without saving annotated versions"
}
]
for i, example in enumerate(examples, 1):
print(f"\n{i}. {example['title']}")
print(f" Command: {example['command']}")
print(f" Description: {example['description']}")
print(f"\n📂 FOLDER STRUCTURE:")
print(" input_images/ - Place your images here")
print(" ├── undamaged/ - Clean poster images")
print(" ├── damaged/ - General damaged images")
print(" ├── tear/ - Images with tears")
print(" ├── stain/ - Images with stains")
print(" └── ... (other damage types)")
print()
print(" output_images/ - Annotated results organized by detection")
print(" processed_images/ - Backup copies")
print(" reports/ - JSON analysis reports")
print(" logs/ - Processing logs")
def main():
"""Main setup and test function."""
print("🎯 FOLDER-BASED DAMAGE DETECTION SETUP")
print("="*50)
# Create folder structure
create_folder_structure()
# Download sample images
download_sample_images()
# Create sample config
create_sample_config()
# Test the system
test_folder_detection()
# Show usage examples
show_usage_examples()
print("\n" + "="*50)
print("🎉 SETUP COMPLETED!")
print("="*50)
print("\n💡 Next Steps:")
print("1. Add your poster images to input_images/ subfolders")
print("2. Run: python3 pretrained_damage_detector.py --auto-folder")
print("3. Check output_images/ for annotated results")
print("4. Review reports/ for detailed analysis")
print(f"\n📖 For more help, see:")
print(" - FOLDER_STRUCTURE.md (detailed folder guide)")
print(" - PRETRAINED_README.md (full documentation)")
if __name__ == "__main__":
main()