File size: 24,046 Bytes
d52f70c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
"""
dataset_generator.py - Enhanced Dataset Generation with User Verification
Hybrid workflow for creating clean dog ReID fine-tuning datasets
"""
import gradio as gr
import cv2
import numpy as np
import pandas as pd
import json
import shutil
from pathlib import Path
from typing import List, Dict, Optional, Tuple
from datetime import datetime
import base64
from io import BytesIO
from PIL import Image
from ultralytics import YOLO

# Import existing modules
from detection import DogDetector
from tracking import SimpleTracker
from reid import SingleModelReID
from database import DogDatabase

# ========== ENHANCED HEADSHOT EXTRACTOR ==========
class ImprovedHeadExtractor:
    """Enhanced head extraction with multiple strategies"""
    
    def __init__(self):
        # Try to load pose model
        self.pose_model = None
        try:
            self.pose_model = YOLO('yolov8m-pose.pt')
            self.pose_model.to('cuda')
            print("Pose model loaded for head extraction")
        except:
            print("Using adaptive geometric head extraction")
        
        # Dog keypoint indices
        self.keypoints_map = {
            'nose': 0, 'left_eye': 1, 'right_eye': 2,
            'left_ear': 3, 'right_ear': 4
        }
    
    def extract_adaptive_geometric(self, dog_crop: np.ndarray, 
                                  aspect_ratio: float) -> Optional[np.ndarray]:
        """Adaptive geometric extraction based on dog orientation"""
        h, w = dog_crop.shape[:2]
        
        try:
            if aspect_ratio > 1.3:  # Dog sideways
                # Head is typically in first 35% horizontally
                head_width = int(w * 0.35)
                head_height = int(h * 0.5)
                head_crop = dog_crop[:head_height, :head_width]
            elif aspect_ratio < 0.7:  # Dog vertical (sitting/standing facing camera)
                # Head is top portion, centered
                head_height = int(h * 0.45)
                margin = int(w * 0.15)
                head_crop = dog_crop[:head_height, margin:w-margin]
            else:  # Normal orientation
                # Standard extraction
                head_height = int(h * 0.4)
                margin = int(w * 0.1)
                head_crop = dog_crop[:head_height, margin:w-margin]
            
            if head_crop.size == 0:
                return None
            
            # Resize to standard size
            head_crop = cv2.resize(head_crop, (128, 128))
            return head_crop
            
        except:
            return None
    
    def extract_with_pose(self, dog_crop: np.ndarray) -> Optional[np.ndarray]:
        """Extract using pose keypoints if available"""
        if self.pose_model is None:
            return None
        
        try:
            results = self.pose_model(dog_crop, conf=0.25, verbose=False)
            
            if results and len(results) > 0 and hasattr(results[0], 'keypoints'):
                keypoints = results[0].keypoints
                
                if keypoints is not None and keypoints.xy is not None:
                    kpts = keypoints.xy[0].cpu().numpy()
                    
                    # Collect head keypoints
                    head_points = []
                    for key in ['nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear']:
                        idx = self.keypoints_map[key]
                        if idx < len(kpts) and kpts[idx][0] > 0:
                            head_points.append(kpts[idx])
                    
                    if len(head_points) >= 3:  # Need at least 3 points
                        head_points = np.array(head_points)
                        
                        # Add padding around keypoints
                        padding = 30
                        min_x = max(0, int(np.min(head_points[:, 0]) - padding))
                        min_y = max(0, int(np.min(head_points[:, 1]) - padding))
                        max_x = min(dog_crop.shape[1], int(np.max(head_points[:, 0]) + padding))
                        max_y = min(dog_crop.shape[0], int(np.max(head_points[:, 1]) + padding * 1.2))
                        
                        head_crop = dog_crop[min_y:max_y, min_x:max_x]
                        
                        if head_crop.size > 0:
                            head_crop = cv2.resize(head_crop, (128, 128))
                            return head_crop
        except:
            pass
        
        return None
    
    def extract_head(self, image: np.ndarray, bbox: List[float]) -> Dict:
        """Main extraction method returning both head crop and metadata"""
        x1, y1, x2, y2 = map(int, bbox)
        dog_crop = image[y1:y2, x1:x2]
        
        if dog_crop.size == 0:
            return {'head_crop': None, 'method': 'failed', 'confidence': 0.0}
        
        aspect_ratio = (x2 - x1) / (y2 - y1)
        
        # Try pose-based extraction first
        head_crop = self.extract_with_pose(dog_crop)
        method = 'pose'
        
        # Fallback to adaptive geometric
        if head_crop is None:
            head_crop = self.extract_adaptive_geometric(dog_crop, aspect_ratio)
            method = 'geometric'
        
        # Calculate quality score
        confidence = 0.0
        if head_crop is not None:
            gray = cv2.cvtColor(head_crop, cv2.COLOR_BGR2GRAY)
            laplacian_var = cv2.Laplacian(gray, cv2.CV_64F).var()
            confidence = min(1.0, laplacian_var / 100)
        
        return {
            'head_crop': head_crop,
            'method': method,
            'confidence': confidence,
            'bbox': [x1, y1, x2, y2]
        }

# ========== DATASET GENERATOR WITH VERIFICATION ==========
class DatasetGenerator:
    """Generate and manage temporary datasets for verification"""
    
    def __init__(self, temp_dir: str = "temp_dataset", 
                 final_dir: str = "verified_dataset"):
        self.temp_dir = Path(temp_dir)
        self.final_dir = Path(final_dir)
        self.head_extractor = ImprovedHeadExtractor()
        
        # Create directories
        self.temp_dir.mkdir(exist_ok=True)
        self.final_dir.mkdir(exist_ok=True)
        
        # Tracking
        self.current_session = None
        self.verification_status = {}
    
    def process_video_for_dataset(self, video_path: str, reid_threshold: float = 0.75,
                                  max_images_per_dog: int = 30) -> Dict:
        """Process video and extract provisional dataset"""
        
        # Clear temp directory
        if self.temp_dir.exists():
            shutil.rmtree(self.temp_dir)
        self.temp_dir.mkdir()
        
        # Initialize components
        detector = DogDetector(device='cuda', confidence_threshold=0.45)
        tracker = SimpleTracker()
        reid = SingleModelReID(device='cuda')
        reid.set_all_thresholds(reid_threshold)
        
        # Storage for dog images
        dog_images = {}  # dog_id -> list of (image, bbox, frame_num, confidence)
        
        # Process video
        cap = cv2.VideoCapture(video_path)
        frame_num = 0
        
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
            
            # Process every 3rd frame to avoid too many similar images
            if frame_num % 3 == 0:
                # Detect and track
                detections = detector.detect(frame)
                tracks = tracker.update(detections)
                
                # Process each track
                for track in tracks:
                    # Get ReID result
                    results = reid.match_or_register_all(track)
                    dog_id = results['ResNet50']['dog_id']
                    confidence = results['ResNet50']['confidence']
                    
                    if dog_id > 0:
                        # Get best detection from track
                        detection = None
                        for det in reversed(track.detections):
                            if det.image_crop is not None:
                                detection = det
                                break
                        
                        if detection:
                            if dog_id not in dog_images:
                                dog_images[dog_id] = []
                            
                            # Store image data
                            dog_images[dog_id].append({
                                'frame': frame.copy(),
                                'bbox': detection.bbox,
                                'frame_num': frame_num,
                                'confidence': confidence,
                                'detection_conf': detection.confidence
                            })
            
            frame_num += 1
        
        cap.release()
        
        # Extract and save best images for each dog
        dataset_info = {
            'video_source': video_path,
            'timestamp': datetime.now().isoformat(),
            'dogs': {}
        }
        
        for dog_id, images in dog_images.items():
            # Sort by confidence and quality
            images.sort(key=lambda x: x['confidence'] * x['detection_conf'], reverse=True)
            
            # Take top N images
            selected_images = images[:max_images_per_dog]
            
            # Create dog directory
            dog_dir = self.temp_dir / f"dog_{dog_id:03d}_provisional"
            dog_dir.mkdir(exist_ok=True)
            
            # Create subdirectories
            (dog_dir / 'full').mkdir(exist_ok=True)
            (dog_dir / 'head').mkdir(exist_ok=True)
            
            dog_info = {
                'dog_id': dog_id,
                'num_images': len(selected_images),
                'avg_confidence': np.mean([img['confidence'] for img in selected_images]),
                'images': []
            }
            
            # Save images
            for idx, img_data in enumerate(selected_images):
                # Extract crops
                x1, y1, x2, y2 = map(int, img_data['bbox'])
                full_crop = img_data['frame'][y1:y2, x1:x2]
                
                # Extract head
                head_result = self.head_extractor.extract_head(
                    img_data['frame'], img_data['bbox']
                )
                
                # Save full crop
                full_path = dog_dir / 'full' / f"frame_{img_data['frame_num']:06d}.jpg"
                cv2.imwrite(str(full_path), full_crop)
                
                # Save head crop if available
                head_path = None
                if head_result['head_crop'] is not None:
                    head_path = dog_dir / 'head' / f"frame_{img_data['frame_num']:06d}_head.jpg"
                    cv2.imwrite(str(head_path), head_result['head_crop'])
                
                # Store metadata
                dog_info['images'].append({
                    'frame_num': img_data['frame_num'],
                    'confidence': img_data['confidence'],
                    'detection_conf': img_data['detection_conf'],
                    'has_head': head_path is not None,
                    'head_method': head_result['method'],
                    'head_confidence': head_result['confidence']
                })
            
            dataset_info['dogs'][dog_id] = dog_info
        
        # Save metadata
        with open(self.temp_dir / 'dataset_info.json', 'w') as f:
            json.dump(dataset_info, f, indent=2)
        
        self.current_session = dataset_info
        return dataset_info
    
    def get_dog_preview_images(self, dog_id: int, num_images: int = 6) -> List:
        """Get preview images for verification interface"""
        dog_dir = self.temp_dir / f"dog_{dog_id:03d}_provisional"
        full_dir = dog_dir / 'full'
        
        if not full_dir.exists():
            return []
        
        images = []
        image_files = sorted(list(full_dir.glob("*.jpg")))[:num_images]
        
        for img_path in image_files:
            img = cv2.imread(str(img_path))
            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            images.append(img_rgb)
        
        return images
    
    def verify_dog(self, dog_id: int, action: str, images_to_remove: List[str] = None):
        """Process user verification action"""
        if action == 'approve':
            self.verification_status[dog_id] = 'approved'
        elif action == 'delete':
            self.verification_status[dog_id] = 'deleted'
        elif action == 'remove_images' and images_to_remove:
            # Remove specific images
            dog_dir = self.temp_dir / f"dog_{dog_id:03d}_provisional"
            for img_name in images_to_remove:
                img_path = dog_dir / 'full' / img_name
                if img_path.exists():
                    img_path.unlink()
                # Also remove corresponding head image
                head_path = dog_dir / 'head' / img_name.replace('.jpg', '_head.jpg')
                if head_path.exists():
                    head_path.unlink()
            self.verification_status[dog_id] = 'cleaned'
    
    def merge_dogs(self, dog_id1: int, dog_id2: int):
        """Merge two dog folders"""
        dir1 = self.temp_dir / f"dog_{dog_id1:03d}_provisional"
        dir2 = self.temp_dir / f"dog_{dog_id2:03d}_provisional"
        
        if dir1.exists() and dir2.exists():
            # Move all images from dir2 to dir1
            for img_path in (dir2 / 'full').glob("*.jpg"):
                shutil.move(str(img_path), str(dir1 / 'full' / img_path.name))
            for img_path in (dir2 / 'head').glob("*.jpg"):
                shutil.move(str(img_path), str(dir1 / 'head' / img_path.name))
            
            # Remove dir2
            shutil.rmtree(dir2)
            self.verification_status[dog_id2] = 'merged'
    
    def finalize_dataset(self) -> Dict:
        """Move verified dogs to final dataset"""
        if not self.current_session:
            return {'error': 'No active session'}
        
        # Clear final directory
        if self.final_dir.exists():
            shutil.rmtree(self.final_dir)
        self.final_dir.mkdir()
        
        final_dogs = []
        dog_id_mapping = {}  # provisional_id -> final_id
        final_id = 1
        
        for dog_id, status in self.verification_status.items():
            if status in ['approved', 'cleaned']:
                # Copy to final directory
                src_dir = self.temp_dir / f"dog_{dog_id:03d}_provisional"
                dst_dir = self.final_dir / f"dog_{final_id:03d}"
                
                if src_dir.exists():
                    shutil.copytree(src_dir, dst_dir)
                    dog_id_mapping[dog_id] = final_id
                    final_dogs.append({
                        'final_id': final_id,
                        'provisional_id': dog_id,
                        'num_images': len(list((dst_dir / 'full').glob("*.jpg")))
                    })
                    final_id += 1
        
        # Create training metadata
        self.create_training_metadata(final_dogs)
        
        return {
            'total_dogs': len(final_dogs),
            'dogs': final_dogs,
            'dataset_path': str(self.final_dir)
        }
    
    def create_training_metadata(self, dogs: List[Dict]):
        """Create CSV files for fine-tuning"""
        data = []
        
        for dog_info in dogs:
            dog_dir = self.final_dir / f"dog_{dog_info['final_id']:03d}"
            
            # Get all images
            for img_path in (dog_dir / 'full').glob("*.jpg"):
                head_path = dog_dir / 'head' / img_path.name.replace('.jpg', '_head.jpg')
                
                data.append({
                    'dog_id': dog_info['final_id'],
                    'full_image': str(img_path.relative_to(self.final_dir)),
                    'head_image': str(head_path.relative_to(self.final_dir)) if head_path.exists() else None
                })
        
        # Create DataFrame
        df = pd.DataFrame(data)
        
        # Split into train/val (80/20)
        train_size = int(len(df) * 0.8)
        train_df = df.iloc[:train_size]
        val_df = df.iloc[train_size:]
        
        # Save CSVs
        train_df.to_csv(self.final_dir / 'train.csv', index=False)
        val_df.to_csv(self.final_dir / 'val.csv', index=False)
        
        # Save metadata
        metadata = {
            'total_dogs': len(dogs),
            'total_images': len(df),
            'train_images': len(train_df),
            'val_images': len(val_df),
            'created': datetime.now().isoformat()
        }
        
        with open(self.final_dir / 'metadata.json', 'w') as f:
            json.dump(metadata, f, indent=2)

# ========== GRADIO INTERFACE ==========
class DatasetVerificationApp:
    """Gradio app for dataset verification"""
    
    def __init__(self):
        self.generator = DatasetGenerator()
    
    def create_interface(self):
        with gr.Blocks(
            title="Dog Dataset Verification",
            theme=gr.themes.Soft()
        ) as app:
            gr.Markdown("""
            # πŸ• Dog ReID Dataset Generator
            ### Hybrid workflow: AI grouping + Human verification = Clean dataset
            """)
            
            # Step 1: Process Video
            with gr.Tab("Step 1: Process Video"):
                with gr.Row():
                    video_input = gr.Video(label="Upload Video")
                    with gr.Column():
                        reid_threshold = gr.Slider(
                            0.65, 0.85, 0.75, step=0.05,
                            label="ReID Threshold (Higher = Stricter)"
                        )
                        max_images = gr.Slider(
                            10, 50, 30, step=5,
                            label="Max Images per Dog"
                        )
                        process_btn = gr.Button("Process Video", variant="primary")
                
                process_output = gr.JSON(label="Processing Results")
                
                def process_video(video_path, threshold, max_imgs):
                    if not video_path:
                        return {"error": "Please upload a video"}
                    
                    result = self.generator.process_video_for_dataset(
                        video_path, threshold, int(max_imgs)
                    )
                    return result
                
                process_btn.click(
                    process_video,
                    inputs=[video_input, reid_threshold, max_images],
                    outputs=process_output
                )
            
            # Step 2: Verify Dogs
            with gr.Tab("Step 2: Verify Dogs"):
                gr.Markdown("Review each dog folder and verify/clean the images")
                
                with gr.Row():
                    dog_selector = gr.Dropdown(
                        label="Select Dog to Review",
                        choices=[]
                    )
                    refresh_btn = gr.Button("Refresh Dog List")
                
                preview_gallery = gr.Gallery(
                    label="Dog Images Preview",
                    show_label=True,
                    columns=3,
                    rows=2,
                    height="auto"
                )
                
                with gr.Row():
                    approve_btn = gr.Button("βœ… Approve", variant="primary")
                    clean_btn = gr.Button("🧹 Remove Selected", variant="secondary")
                    delete_btn = gr.Button("❌ Delete All", variant="stop")
                
                with gr.Row():
                    merge_dog1 = gr.Dropdown(label="Merge Dog 1")
                    merge_dog2 = gr.Dropdown(label="With Dog 2")
                    merge_btn = gr.Button("πŸ”„ Merge Dogs")
                
                verification_status = gr.Textbox(label="Verification Status")
                
                def refresh_dogs():
                    if not self.generator.current_session:
                        return gr.update(choices=[])
                    
                    dogs = self.generator.current_session['dogs']
                    choices = [f"Dog {dog_id}" for dog_id in dogs.keys()]
                    return gr.update(choices=choices)
                
                def show_dog_preview(dog_selection):
                    if not dog_selection:
                        return []
                    
                    dog_id = int(dog_selection.split()[1])
                    return self.generator.get_dog_preview_images(dog_id)
                
                def approve_dog(dog_selection):
                    if not dog_selection:
                        return "No dog selected"
                    
                    dog_id = int(dog_selection.split()[1])
                    self.generator.verify_dog(dog_id, 'approve')
                    return f"βœ… Dog {dog_id} approved"
                
                def delete_dog(dog_selection):
                    if not dog_selection:
                        return "No dog selected"
                    
                    dog_id = int(dog_selection.split()[1])
                    self.generator.verify_dog(dog_id, 'delete')
                    return f"❌ Dog {dog_id} deleted"
                
                def merge_dogs(dog1, dog2):
                    if not dog1 or not dog2:
                        return "Select both dogs to merge"
                    
                    id1 = int(dog1.split()[1])
                    id2 = int(dog2.split()[1])
                    self.generator.merge_dogs(id1, id2)
                    return f"πŸ”„ Merged Dog {id2} into Dog {id1}"
                
                refresh_btn.click(refresh_dogs, outputs=dog_selector)
                dog_selector.change(show_dog_preview, inputs=dog_selector, outputs=preview_gallery)
                approve_btn.click(approve_dog, inputs=dog_selector, outputs=verification_status)
                delete_btn.click(delete_dog, inputs=dog_selector, outputs=verification_status)
                merge_btn.click(merge_dogs, inputs=[merge_dog1, merge_dog2], outputs=verification_status)
            
            # Step 3: Finalize Dataset
            with gr.Tab("Step 3: Finalize"):
                gr.Markdown("Save verified dogs to final dataset")
                
                finalize_btn = gr.Button("πŸ“¦ Create Final Dataset", variant="primary", size="lg")
                final_output = gr.JSON(label="Dataset Creation Results")
                
                download_section = gr.Markdown("")
                
                def finalize():
                    result = self.generator.finalize_dataset()
                    
                    if 'error' not in result:
                        download_html = f"""
                        ### βœ… Dataset Ready!
                        - **Total Dogs:** {result['total_dogs']}
                        - **Location:** `{result['dataset_path']}`
                        - **Files:** `train.csv`, `val.csv`, `metadata.json`
                        
                        Ready for ResNet50 fine-tuning!
                        """
                    else:
                        download_html = f"❌ Error: {result['error']}"
                    
                    return result, download_html
                
                finalize_btn.click(finalize, outputs=[final_output, download_section])
        
        return app

# Main entry point
if __name__ == "__main__":
    app = DatasetVerificationApp()
    interface = app.create_interface()
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )