mustafa2ak commited on
Commit
5b5967c
Β·
verified Β·
1 Parent(s): 50025d3

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +98 -0
database.py CHANGED
@@ -156,6 +156,104 @@ class DogDatabase:
156
  WHERE dog_id = ?
157
  """, (dog_id,))
158
  self.conn.commit()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  def merge_dogs(self, keep_id: int, merge_id: int) -> bool:
161
  """Merge two dogs, keeping keep_id"""
 
156
  WHERE dog_id = ?
157
  """, (dog_id,))
158
  self.conn.commit()
159
+ def export_training_dataset(self, output_dir: str = "training_dataset") -> Dict:
160
+ """
161
+ Export database images to folder structure for fine-tuning
162
+
163
+ Output structure:
164
+ training_dataset/
165
+ β”œβ”€β”€ dog_1/
166
+ β”‚ β”œβ”€β”€ img_0000.jpg
167
+ β”‚ └── ...
168
+ β”œβ”€β”€ dog_2/
169
+ └── metadata.json
170
+
171
+ Returns:
172
+ dict with export statistics
173
+ """
174
+ from pathlib import Path
175
+ import json
176
+ from datetime import datetime
177
+
178
+ output_path = Path(output_dir)
179
+ output_path.mkdir(parents=True, exist_ok=True)
180
+
181
+ # Get all active dogs
182
+ dogs_df = self.get_all_dogs(active_only=True)
183
+
184
+ if dogs_df.empty:
185
+ return {
186
+ 'success': False,
187
+ 'message': 'No dogs in database',
188
+ 'total_dogs': 0,
189
+ 'total_images': 0
190
+ }
191
+
192
+ total_images = 0
193
+ export_info = []
194
+
195
+ for _, dog in dogs_df.iterrows():
196
+ dog_id = dog['dog_id']
197
+ dog_name = dog['name'] or f"dog_{dog_id}"
198
+
199
+ # Create folder for this dog
200
+ dog_folder = output_path / dog_name
201
+ dog_folder.mkdir(exist_ok=True)
202
+
203
+ # Get all non-discarded images
204
+ images = self.get_dog_images(
205
+ dog_id=dog_id,
206
+ validated_only=False,
207
+ include_discarded=False
208
+ )
209
+
210
+ if not images:
211
+ print(f"Warning: Dog {dog_id} has no images")
212
+ continue
213
+
214
+ # Save each image as JPG
215
+ for idx, img_data in enumerate(images):
216
+ image = img_data['image'] # OpenCV array from BLOB
217
+ filename = f"img_{idx:04d}.jpg"
218
+ filepath = dog_folder / filename
219
+
220
+ # Write to disk
221
+ cv2.imwrite(str(filepath), image)
222
+
223
+ total_images += len(images)
224
+
225
+ export_info.append({
226
+ 'dog_id': dog_id,
227
+ 'name': dog_name,
228
+ 'image_count': len(images),
229
+ 'folder': str(dog_folder)
230
+ })
231
+
232
+ print(f"Exported {len(images)} images for {dog_name}")
233
+
234
+ # Create metadata file
235
+ metadata = {
236
+ 'export_date': datetime.now().isoformat(),
237
+ 'total_dogs': len(dogs_df),
238
+ 'total_images': total_images,
239
+ 'output_directory': str(output_path),
240
+ 'dogs': export_info
241
+ }
242
+
243
+ metadata_path = output_path / 'metadata.json'
244
+ with open(metadata_path, 'w') as f:
245
+ json.dump(metadata, f, indent=2)
246
+
247
+ print(f"\nExport complete: {len(dogs_df)} dogs, {total_images} images")
248
+ print(f"Location: {output_path}")
249
+
250
+ return {
251
+ 'success': True,
252
+ 'total_dogs': len(dogs_df),
253
+ 'total_images': total_images,
254
+ 'output_path': str(output_path),
255
+ 'dogs': export_info
256
+ }
257
 
258
  def merge_dogs(self, keep_id: int, merge_id: int) -> bool:
259
  """Merge two dogs, keeping keep_id"""