File size: 34,147 Bytes
e7712ed
14e8154
8c17ac0
e7712ed
c43ca93
e7712ed
 
 
 
 
 
 
 
 
 
 
16726e7
14e8154
e7712ed
 
 
14e8154
e7712ed
 
14e8154
e7712ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d5215c
 
e7712ed
 
 
 
 
 
 
 
 
 
 
14e8154
e7712ed
 
 
 
 
14e8154
e7712ed
 
 
5514ed7
 
 
 
 
e7712ed
 
 
 
 
 
 
14e8154
e7712ed
51eed27
e7712ed
 
 
2d5215c
e7712ed
 
 
 
 
 
51eed27
e7712ed
 
51eed27
 
e7712ed
51eed27
e7712ed
 
 
14e8154
e7712ed
8c17ac0
e7712ed
 
14e8154
e7712ed
 
d49c095
e7712ed
14e8154
e7712ed
 
 
8c17ac0
e7712ed
 
14e8154
 
e7712ed
14e8154
 
e7712ed
 
 
 
d49c095
 
14e8154
 
d49c095
14e8154
 
d49c095
 
 
 
14e8154
 
d49c095
14e8154
 
 
 
 
 
d49c095
 
14e8154
d49c095
 
 
 
 
14e8154
 
d49c095
 
 
 
 
 
14e8154
 
 
 
0bbcf51
14e8154
 
 
 
0bbcf51
14e8154
 
 
 
0bbcf51
14e8154
 
 
0bbcf51
 
 
 
 
 
 
d49c095
14e8154
 
d49c095
 
14e8154
 
 
e7712ed
2d5215c
e7712ed
14e8154
 
 
 
 
 
 
 
 
 
e7712ed
 
 
 
 
14e8154
e7712ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14e8154
e7712ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d5215c
e7712ed
 
 
 
 
 
 
 
 
 
 
 
 
d49c095
 
e7712ed
 
 
 
 
8c17ac0
e7712ed
 
8c17ac0
e7712ed
 
 
8c17ac0
 
e7712ed
 
8c17ac0
e7712ed
 
 
 
14e8154
d49c095
e7712ed
 
 
 
 
14e8154
 
d49c095
1d88abf
e7712ed
 
 
d49c095
e7712ed
 
d49c095
e7712ed
 
14e8154
e7712ed
 
 
 
 
8c17ac0
 
14e8154
d49c095
14e8154
 
e7712ed
8c17ac0
 
e7712ed
 
 
8c17ac0
 
 
 
 
 
 
e7712ed
d49c095
 
 
e7712ed
 
 
14e8154
 
 
 
 
 
 
 
 
d49c095
8c17ac0
14e8154
 
 
 
8c17ac0
14e8154
e7712ed
14e8154
 
 
 
 
 
 
 
 
 
e7712ed
14e8154
 
 
 
8c17ac0
e7712ed
 
 
 
14e8154
d49c095
 
14e8154
d49c095
 
 
 
14e8154
d49c095
 
 
 
 
e7712ed
 
 
d49c095
8c17ac0
e7712ed
 
 
 
 
8c17ac0
e7712ed
 
 
14e8154
e7712ed
 
 
 
14e8154
e7712ed
 
14e8154
d49c095
14e8154
 
 
d49c095
14e8154
 
 
e7712ed
 
 
 
 
 
 
 
 
 
 
14e8154
e7712ed
 
 
 
 
 
 
 
14e8154
e7712ed
 
 
 
 
8c17ac0
 
e7712ed
 
14e8154
2d5215c
8c17ac0
2d5215c
64407ac
14e8154
64407ac
2d5215c
64407ac
 
 
14e8154
64407ac
14e8154
64407ac
 
 
 
 
 
 
 
14e8154
64407ac
 
 
 
 
 
14e8154
64407ac
 
 
14e8154
64407ac
14e8154
64407ac
 
d49c095
 
14e8154
d49c095
14e8154
dc99cf1
14e8154
d49c095
 
14e8154
d49c095
14e8154
 
 
 
 
 
8c17ac0
14e8154
 
 
 
 
 
d49c095
14e8154
 
 
 
d49c095
 
 
 
14e8154
d49c095
 
 
14e8154
 
d49c095
e7712ed
14e8154
9fe78ad
14e8154
 
8c17ac0
14e8154
d49c095
 
14e8154
 
 
 
 
 
 
 
 
 
 
 
9fe78ad
 
8c17ac0
9fe78ad
8c17ac0
 
9fe78ad
8c17ac0
b43ed9e
14e8154
e7712ed
 
9fe78ad
14e8154
 
8c17ac0
14e8154
d49c095
9fe78ad
 
e7712ed
14e8154
 
 
 
 
 
 
 
 
 
 
9fe78ad
e7712ed
14e8154
 
 
 
 
 
 
 
 
 
9fe78ad
dc99cf1
 
 
e7712ed
14e8154
8c17ac0
14e8154
8c17ac0
d49c095
14e8154
 
8c17ac0
14e8154
8c17ac0
14e8154
8c17ac0
 
 
 
 
 
 
 
 
 
14e8154
8c17ac0
 
14e8154
8c17ac0
d49c095
8c17ac0
 
d49c095
14e8154
 
d49c095
8c17ac0
dc99cf1
8c17ac0
 
 
e7712ed
14e8154
 
9fe78ad
d49c095
9fe78ad
 
14e8154
 
d49c095
 
14e8154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7712ed
14e8154
 
 
8c17ac0
14e8154
 
 
8c17ac0
 
 
14e8154
 
9fe78ad
8c17ac0
 
 
9fe78ad
14e8154
d49c095
14e8154
 
d49c095
 
14e8154
 
 
 
 
 
 
 
 
d49c095
e7712ed
14e8154
9fe78ad
 
14e8154
8c17ac0
9fe78ad
 
 
 
 
 
 
 
14e8154
d49c095
9fe78ad
 
 
 
 
d49c095
 
 
 
 
 
 
14e8154
d49c095
 
8c17ac0
 
d49c095
 
 
 
 
 
 
 
9fe78ad
 
 
 
 
2d5215c
 
c43ca93
e7712ed
 
 
 
 
 
 
 
 
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
"""
resnet_dataset_creator.py - Fixed Dataset Creation Tool for ResNet Fine-tuning
Changes: Removed head extraction, fixed image gallery display
"""
import gradio as gr
import cv2
import numpy as np
import pandas as pd
import json
import shutil
import torch
from pathlib import Path
from typing import List, Dict, Optional, Tuple
from datetime import datetime
from PIL import Image
import zipfile
import gc

# Import required modules
from detection import DogDetector
from tracking import SimpleTracker
from reid import SingleModelReID  # Using simplified version
from ultralytics import YOLO

# ========== IMAGE QUALITY ANALYZER (unchanged) ==========
class ImageQualityAnalyzer:
    """Analyze and score image quality for dataset selection"""
    
    def __init__(self):
        self.quality_weights = {
            'sharpness': 0.3,
            'resolution': 0.2,
            'brightness': 0.15,
            'contrast': 0.15,
            'occlusion': 0.2
        }
    
    def calculate_sharpness(self, image: np.ndarray) -> float:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        laplacian = cv2.Laplacian(gray, cv2.CV_64F)
        return min(100, laplacian.var())
    
    def calculate_resolution_score(self, image: np.ndarray) -> float:
        h, w = image.shape[:2]
        pixels = h * w
        ideal_pixels = 224 * 224
        return min(100, (pixels / ideal_pixels) * 100)
    
    def calculate_brightness_score(self, image: np.ndarray) -> float:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        mean_brightness = np.mean(gray)
        return 100 - abs(mean_brightness - 127) * 0.78
    
    def calculate_contrast_score(self, image: np.ndarray) -> float:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        contrast = gray.std()
        return min(100, contrast * 2)
    
    def detect_occlusion(self, bbox: List[float], frame_shape: Tuple) -> float:
        x1, y1, x2, y2 = bbox
        h, w = frame_shape[:2]
        
        edge_penalty = 0
        if x1 <= 5 or y1 <= 5 or x2 >= w-5 or y2 >= h-5:
            edge_penalty = 30
        
        aspect = (x2 - x1) / (y2 - y1)
        if aspect < 0.3 or aspect > 3:
            edge_penalty += 20
        
        return 100 - edge_penalty
    
    def calculate_overall_quality(self, image: np.ndarray, bbox: List[float],
                                   frame_shape: Tuple) -> float:
        scores = {
            'sharpness': self.calculate_sharpness(image),
            'resolution': self.calculate_resolution_score(image),
            'brightness': self.calculate_brightness_score(image),
            'contrast': self.calculate_contrast_score(image),
            'occlusion': self.detect_occlusion(bbox, frame_shape)
        }
        
        total = sum(scores[k] * self.quality_weights[k] for k in scores)
        return total

# ========== SMART IMAGE SELECTOR (unchanged) ==========
class SmartImageSelector:
    """Intelligently select best images based on quality and diversity"""
    
    def __init__(self):
        self.quality_analyzer = ImageQualityAnalyzer()
        self.min_temporal_distance = 10
    
    def select_best_images(self, dog_data: List[Dict], max_images: int = 30,
                          video_fps: float = 30) -> List[Dict]:
        for item in dog_data:
            item['quality_score'] = self.quality_analyzer.calculate_overall_quality(
                item['crop'], item['bbox'], item['frame'].shape
            )
        
        if len(dog_data) <= max_images:
            return dog_data
        
        dog_data.sort(key=lambda x: x['quality_score'], reverse=True)
        
        selected = []
        selected_frames = set()
        selected_indices = set()
        
        for idx, item in enumerate(dog_data):
            frame_num = item['frame_num']
            
            too_close = any(
                abs(frame_num - f) < self.min_temporal_distance
                for f in selected_frames
            )
            
            if not too_close and len(selected) < max_images:
                selected.append(item)
                selected_frames.add(frame_num)
                selected_indices.add(idx)
        
        if len(selected) < max_images:
            for idx, item in enumerate(dog_data):
                if idx not in selected_indices and len(selected) < max_images:
                    selected.append(item)
                    selected_indices.add(idx)
        
        return selected[:max_images]

# ========== MAIN DATASET CREATOR - FIXED ==========
class ResNetDatasetCreator:
    """Main application with head extraction removed and gallery display fixed"""
    
    def __init__(self):
        # Directories
        self.temp_dir = Path("temp_dataset")
        self.final_dir = Path("resnet_finetune_dataset")
        self.database_dir = Path("permanent_database")
        
        # Components - initialize once
        self.detector = DogDetector(device='cuda' if torch.cuda.is_available() else 'cpu')
        self.tracker = SimpleTracker()
        self.reid = SingleModelReID(device='cuda' if torch.cuda.is_available() else 'cpu')
        # REMOVED: self.head_extractor = SimpleHeadExtractor()
        self.image_selector = SmartImageSelector()
        
        # Session data - temporary only
        self.current_video_path = None
        self.current_session = None
        self.temp_processed_dogs = {}  # Temporary dogs from current video
        self.permanent_dogs = {}  # Permanently saved dogs
        
        # Create directories
        self.temp_dir.mkdir(exist_ok=True)
        self.final_dir.mkdir(exist_ok=True)
        self.database_dir.mkdir(exist_ok=True)
        
        # Load permanent database
        self.load_permanent_database()
    
    def load_permanent_database(self):
        """Load only permanently saved dogs"""
        db_file = self.database_dir / "database.json"
        if db_file.exists():
            with open(db_file, 'r') as f:
                data = json.load(f)
                self.permanent_dogs = {int(k): v for k, v in data.get('dogs', {}).items()}
                print(f"Loaded {len(self.permanent_dogs)} permanently saved dogs")
    
    def save_to_permanent_database(self):
        """Save selected dogs to permanent database"""
        # Merge temp dogs into permanent
        self.permanent_dogs.update(self.temp_processed_dogs)
        
        # Save metadata
        db_file = self.database_dir / "database.json"
        data = {
            'dogs': {str(k): v for k, v in self.permanent_dogs.items()},
            'last_updated': datetime.now().isoformat()
        }
        with open(db_file, 'w') as f:
            json.dump(data, f, indent=2)
        
        # Copy images from temp to permanent
        for dog_id in self.temp_processed_dogs:
            src_dir = self.temp_dir / f"dog_{dog_id:03d}"
            dst_dir = self.database_dir / f"dog_{dog_id:03d}"
            if src_dir.exists():
                if dst_dir.exists():
                    shutil.rmtree(dst_dir)
                shutil.copytree(src_dir, dst_dir)
        
        print(f"Saved {len(self.temp_processed_dogs)} dogs to permanent database")
    
    def clear_temp_data(self):
        """Clear all temporary data for new video and free memory."""
        # Clear temp directory
        if self.temp_dir.exists():
            shutil.rmtree(self.temp_dir)
        self.temp_dir.mkdir()
    
        # Clear temp session data
        self.current_video_path = None
        self.current_session = None
        self.temp_processed_dogs = {}
    
        # Reset ReID (clears in-memory dogs)
        self.reid.reset_all()
        
        # πŸ‘‡ ADD THESE TWO LINES FOR MEMORY CLEANUP
        gc.collect()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
    
        print("Temporary data cleared and memory released.")

    
    def clear_all_permanent_data(self):
        """Clear entire permanent database"""
        if self.database_dir.exists():
            shutil.rmtree(self.database_dir)
        self.database_dir.mkdir()
        self.permanent_dogs = {}
        print("All permanent data cleared")
    
    def process_video(self, video_path: str, reid_threshold: float,
                     max_images_per_dog: int, sample_rate: int) -> Dict:
        """Process video with current settings"""
        
        # Clear previous temp data if new video
        if video_path != self.current_video_path:
            self.clear_temp_data()
            self.current_video_path = video_path
        else:
            # Re-processing same video - clear and start fresh
            self.clear_temp_data()
            self.current_video_path = video_path
        
        # Set ReID threshold
        self.reid.set_all_thresholds(reid_threshold)
        
        # Storage for dog data
        dog_data = {}
        
        # Open video
        cap = cv2.VideoCapture(video_path)
        fps = cap.get(cv2.CAP_PROP_FPS)
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        
        frame_num = 0
        processed_frames = 0
        
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
            
            # Sample frames
            if frame_num % sample_rate == 0:
                # Detect dogs
                detections = self.detector.detect(frame)
                
                # Update tracking
                tracks = self.tracker.update(detections)
                
                # Process each track
                for track in tracks:
                    # Get ReID result
                    results = self.reid.match_or_register_all(track)
                    dog_id = results['ResNet50']['dog_id']
                    confidence = results['ResNet50']['confidence']
                    
                    if dog_id > 0 and confidence > 0.3:
                        # Get best detection
                        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_data:
                                dog_data[dog_id] = []
                            
                            dog_data[dog_id].append({
                                'frame': frame.copy(),
                                'crop': detection.image_crop,
                                'bbox': detection.bbox,
                                'frame_num': frame_num,
                                'reid_confidence': confidence,
                                'detection_confidence': detection.confidence,
                                'timestamp': frame_num / fps
                            })
                    
                processed_frames += 1
            
            frame_num += 1
            
            # Yield progress
            if frame_num % 30 == 0:
                progress = int((frame_num / total_frames) * 100)
                yield {'progress': progress, 'status': f"Processing: {progress}%"}
        
        cap.release()
        
        # Select best images for each dog
        total_images = 0
        new_dogs = {}
        
        for dog_id, images in dog_data.items():
            selected = self.image_selector.select_best_images(
                images, max_images_per_dog, fps
            )
            
            # Save to temp directory only - ONLY FULL BODY IMAGES
            dog_dir = self.temp_dir / f"dog_{dog_id:03d}"
            dog_dir.mkdir(exist_ok=True)
            # REMOVED: (dog_dir / 'head').mkdir(exist_ok=True)
            
            saved_count = 0
            for idx, img_data in enumerate(selected):
                # Save full crop only
                full_path = dog_dir / f"frame_{img_data['frame_num']:06d}.jpg"
                cv2.imwrite(str(full_path), img_data['crop'])
                
                # REMOVED: Head extraction and saving
                saved_count += 1
            
            total_images += saved_count
            
            # Store in temp dogs only
            new_dogs[dog_id] = {
                'num_images': saved_count,
                'avg_confidence': np.mean([d['reid_confidence'] for d in selected]),
                'quality_scores': [d['quality_score'] for d in selected]
            }
        
        # Update temp dogs (not permanent)
        self.temp_processed_dogs = new_dogs
        
        # Save session info
        self.current_session = {
            'video': video_path,
            'timestamp': datetime.now().isoformat(),
            'num_dogs': len(new_dogs),
            'total_images': total_images,
            'reid_threshold': reid_threshold,
            'dogs': {str(k): v for k, v in new_dogs.items()}
        }
        
        # Save metadata to temp
        with open(self.temp_dir / 'session.json', 'w') as f:
            json.dump(self.current_session, f, indent=2)
        
        yield {'status': 'complete', 'session': self.current_session}
    
    def get_dog_images(self, dog_id: int, from_permanent: bool = False, max_display: int = None) -> List:
        """Get images for verification - FIXED to show all or specified number of images"""
        if from_permanent:
            dog_dir = self.database_dir / f"dog_{dog_id:03d}"
        else:
            dog_dir = self.temp_dir / f"dog_{dog_id:03d}"
        
        # Check directly in dog directory (no 'full' subdirectory anymore)
        if not dog_dir.exists():
            return []
        
        images = []
        image_files = sorted(dog_dir.glob("*.jpg"))
        
        # If max_display is specified, limit to that number, otherwise show all
        if max_display:
            image_files = image_files[:max_display]
        
        for img_path in image_files:
            img = cv2.imread(str(img_path))
            if img is not None:
                img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                images.append(img_rgb)
        
        return images
    
    def remove_images_by_selection(self, dog_id: int, selected_indices: List, from_permanent: bool = False):
        """Remove images based on gallery selection"""
        if from_permanent:
            dog_dir = self.database_dir / f"dog_{dog_id:03d}"
        else:
            dog_dir = self.temp_dir / f"dog_{dog_id:03d}"
        
        if not dog_dir.exists():
            return
        
        image_files = sorted(list(dog_dir.glob("*.jpg")))
        
        # Remove selected images
        for idx in selected_indices:
            if 0 <= idx < len(image_files):
                # Remove image
                image_files[idx].unlink(missing_ok=True)
    
    def delete_dog(self, dog_id: int, from_permanent: bool = False):
        """Delete entire dog folder"""
        if from_permanent:
            dog_dir = self.database_dir / f"dog_{dog_id:03d}"
            if dog_id in self.permanent_dogs:
                del self.permanent_dogs[dog_id]
        else:
            dog_dir = self.temp_dir / f"dog_{dog_id:03d}"
            if dog_id in self.temp_processed_dogs:
                del self.temp_processed_dogs[dog_id]
        
        if dog_dir.exists():
            shutil.rmtree(dog_dir)
    
    def save_final_dataset(self, format_type: str = 'both') -> str:
        """Export both temp and permanent dogs - UPDATED for full body only"""
        if self.final_dir.exists():
            shutil.rmtree(self.final_dir)
        self.final_dir.mkdir()
        
        # Combine temp and permanent dogs
        all_dog_dirs = []
        
        # Add temp dogs
        for d in self.temp_dir.iterdir():
            if d.is_dir() and d.name.startswith('dog_'):
                all_dog_dirs.append(d)
        
        # Add permanent dogs
        temp_dogs = {d.name for d in all_dog_dirs}
        for d in self.database_dir.iterdir():
            if d.is_dir() and d.name.startswith('dog_') and d.name not in temp_dogs:
                all_dog_dirs.append(d)
        
        data_entries = []
        final_id = 1
        
        for dog_dir in sorted(all_dog_dirs):
            if not dog_dir.exists():
                continue
            
            final_dog_dir = self.final_dir / f"dog_{final_id:03d}"
            shutil.copytree(dog_dir, final_dog_dir)
            
            for img_path in final_dog_dir.glob("*.jpg"):
                data_entries.append({
                    'dog_id': final_id,
                    'image_path': str(img_path.relative_to(self.final_dir)),
                    'class': final_id
                })
            
            final_id += 1
        
        if format_type in ['csv', 'both']:
            df = pd.DataFrame(data_entries)
            
            if len(df) > 5:
                from sklearn.model_selection import train_test_split
                train_df, val_df = train_test_split(
                    df, test_size=0.2, stratify=df['dog_id'], random_state=42
                )
                train_df.to_csv(self.final_dir / 'train.csv', index=False)
                val_df.to_csv(self.final_dir / 'val.csv', index=False)
            else:
                df.to_csv(self.final_dir / 'train.csv', index=False)
        
        metadata = {
            'total_dogs': final_id - 1,
            'total_images': len(data_entries),
            'format': format_type,
            'created': datetime.now().isoformat()
        }
        
        with open(self.final_dir / 'metadata.json', 'w') as f:
            json.dump(metadata, f, indent=2)
        
        # Create zip
        zip_path = self.final_dir.parent / f"resnet_dataset_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
        with zipfile.ZipFile(zip_path, 'w') as zipf:
            for file_path in self.final_dir.rglob('*'):
                zipf.write(file_path, file_path.relative_to(self.final_dir))
        
        return str(zip_path)
    
    def create_interface(self):
        """Create Gradio interface with fixes"""
        with gr.Blocks(
            title="ResNet Fine-tuning Dataset Creator",
            theme=gr.themes.Soft()
        ) as app:
            gr.Markdown("""
            # 🎯 ResNet Fine-tuning Dataset Creator - Full Body Only
            ### Creates dataset with full body dog images only (no head extraction)
            """)
            
            # States
            processing_state = gr.State(None)
            selected_indices_state = gr.State([])
            
            with gr.Tabs() as tabs:
                # ========== STEP 1: PROCESS VIDEO ==========
                with gr.Tab("πŸ“Ή Step 1: Process Video", id=0):
                    with gr.Row():
                        video_input = gr.Video(label="Upload Video")
                        with gr.Column():
                            reid_threshold = gr.Slider(
                                0.30, 0.85, 0.40, step=0.05,
                                label="ReID Threshold",
                                info="Lower = More lenient (combine similar dogs)"
                            )
                            max_images = gr.Slider(
                                10, 50, 30, step=5,
                                label="Max Images per Dog"
                            )
                            sample_rate = gr.Slider(
                                1, 5, 2, step=1,
                                label="Sample Rate",
                                info="Process every Nth frame"
                            )
                    
                    process_btn = gr.Button("πŸš€ Process Video", variant="primary", size="lg")
                    
                    with gr.Column():
                        progress_bar = gr.Textbox(label="Progress", interactive=False)
                        results_display = gr.HTML(label="Processing Results")
                        
                        with gr.Row():
                            clear_btn = gr.Button(
                                "πŸ”„ Clear & Reset (Process Again)", 
                                variant="secondary",
                                size="lg",
                                visible=False
                            )
                    
                    def process_wrapper(video, threshold, max_img, sample):
                        """Process with current settings"""
                        if not video:
                            return None, "", "Please upload a video", gr.update(visible=False)
                        
                        # Process video (will auto-clear if needed)
                        for update in self.process_video(video, threshold, int(max_img), int(sample)):
                            if 'progress' in update:
                                yield None, "", update['status'], gr.update(visible=False)
                            else:
                                # Format results
                                session = update['session']
                                html = f"""
                                <div style="padding: 20px; background: #f8f9fa; border-radius: 10px;">
                                    <h3>πŸ“Š Processing Complete!</h3>
                                    <p><b>Dogs detected:</b> {session['num_dogs']}</p>
                                    <p><b>Total full body images:</b> {session['total_images']}</p>
                                    <p><b>ReID threshold used:</b> {session['reid_threshold']:.2f}</p>
                                    <hr>
                                    <p>βœ… Data is in <b>temporary storage</b>. Review in Step 2 before saving permanently.</p>
                                </div>
                                """
                                yield session, html, "Complete! βœ…", gr.update(visible=True)
                    
                    def clear_and_reset():
                        """Clear all temp data for reprocessing"""
                        self.clear_temp_data()
                        return None, "", "", gr.update(visible=False)
                    
                    process_btn.click(
                        process_wrapper,
                        inputs=[video_input, reid_threshold, max_images, sample_rate],
                        outputs=[processing_state, results_display, progress_bar, clear_btn]
                    )
                    
                    clear_btn.click(
                        clear_and_reset,
                        outputs=[processing_state, results_display, progress_bar, clear_btn]
                    )
                
                # ========== STEP 2: VERIFY & CLEAN ==========
                with gr.Tab("βœ… Step 2: Verify & Clean", id=1):
                    gr.Markdown("""
                    Review temporary results. **Nothing is permanently saved until you click Save.**
                    Click images in the gallery to select them, then use Remove Selected.
                    """)
                    
                    with gr.Row():
                        with gr.Column():
                            source_selector = gr.Radio(
                                choices=["Temporary (Current Video)", "Permanent (Saved)"],
                                value="Temporary (Current Video)",
                                label="Data Source"
                            )
                            dog_selector = gr.Dropdown(
                                label="Select Dog",
                                choices=[],
                                interactive=True
                            )
                            refresh_btn = gr.Button("πŸ”„ Refresh List")
                    
                    image_gallery = gr.Gallery(
                        label="Full Body Images - Click to select for removal",
                        show_label=True,
                        columns=6,
                        rows=8,  # Increased rows for more visibility
                        object_fit="contain",
                        height=600,  # Fixed height for scrolling
                        interactive=True,
                        type="numpy"
                    )
                    
                    with gr.Row():
                        selected_info = gr.Textbox(
                            label="Selected Images",
                            value="No images selected",
                            interactive=False
                        )
                        remove_selected_btn = gr.Button("πŸ—‘ Remove Selected Images", variant="secondary")
                        delete_dog_btn = gr.Button("❌ Delete Entire Dog", variant="stop")
                    
                    with gr.Row():
                        save_to_permanent_btn = gr.Button(
                            "πŸ’Ύ Save Current Video Results to Permanent Database",
                            variant="primary",
                            size="lg"
                        )
                        clear_permanent_btn = gr.Button(
                            "⚠️ Clear All Permanent Data",
                            variant="stop"
                        )
                    
                    status_text = gr.Textbox(label="Status", interactive=False)
                    
                    def refresh_dogs(source):
                        """Refresh dog list based on source"""
                        if source == "Temporary (Current Video)":
                            if not self.temp_processed_dogs:
                                return gr.update(choices=[], value=None)
                            choices = [f"Dog {dog_id}" for dog_id in sorted(self.temp_processed_dogs.keys())]
                        else:
                            if not self.permanent_dogs:
                                return gr.update(choices=[], value=None)
                            choices = [f"Dog {dog_id}" for dog_id in sorted(self.permanent_dogs.keys())]
                        
                        if choices:
                            return gr.update(choices=choices, value=choices[0])
                        return gr.update(choices=[], value=None)
                    
                    def show_dog_images(dog_selection, source):
                        """Display ALL images for selected dog"""
                        if not dog_selection:
                            return [], [], "No dog selected"
                        
                        dog_id = int(dog_selection.split()[1])
                        from_permanent = (source == "Permanent (Saved)")
                        # Don't limit number of images - show all
                        images = self.get_dog_images(dog_id, from_permanent)
                        return images, [], f"Showing {len(images)} images for Dog {dog_id}"
                    
                    def handle_gallery_select(evt: gr.SelectData, selected_indices):
                        """Handle gallery selection"""
                        if evt.index in selected_indices:
                            selected_indices.remove(evt.index)
                        else:
                            selected_indices.append(evt.index)
                        
                        if selected_indices:
                            return selected_indices, f"Selected images: {sorted(selected_indices)}"
                        return [], "No images selected"
                    
                    def remove_selected_images(dog_selection, source, selected_indices):
                        """Remove selected images"""
                        if not dog_selection:
                            return "No dog selected", [], []
                        
                        if not selected_indices:
                            return "No images selected", gr.update(), selected_indices
                        
                        dog_id = int(dog_selection.split()[1])
                        from_permanent = (source == "Permanent (Saved)")
                        
                        self.remove_images_by_selection(dog_id, selected_indices, from_permanent)
                        
                        # Refresh gallery
                        images = self.get_dog_images(dog_id, from_permanent)
                        return f"Removed {len(selected_indices)} images", images, []
                    
                    def delete_dog(dog_selection, source):
                        """Delete entire dog"""
                        if not dog_selection:
                            return "No dog selected", []
                        
                        dog_id = int(dog_selection.split()[1])
                        from_permanent = (source == "Permanent (Saved)")
                        self.delete_dog(dog_id, from_permanent)
                        return f"Deleted Dog {dog_id}", []
                    
                    def save_to_permanent():
                        """Save current temp results to permanent database"""
                        if not self.temp_processed_dogs:
                            return "No temporary data to save"
                        
                        self.save_to_permanent_database()
                        count = len(self.temp_processed_dogs)
                        self.clear_temp_data()  # Clear temp after saving
                        return f"βœ… Saved {count} dogs to permanent database. Temp data cleared."
                    
                    def clear_all_permanent():
                        """Clear all permanent data"""
                        self.clear_all_permanent_data()
                        return "⚠️ All permanent data cleared"
                    
                    # Event handlers
                    refresh_btn.click(
                        refresh_dogs, 
                        inputs=source_selector,
                        outputs=dog_selector
                    )
                    
                    dog_selector.change(
                        show_dog_images,
                        inputs=[dog_selector, source_selector],
                        outputs=[image_gallery, selected_indices_state, selected_info]
                    )
                    
                    image_gallery.select(
                        handle_gallery_select,
                        inputs=selected_indices_state,
                        outputs=[selected_indices_state, selected_info]
                    )
                    
                    remove_selected_btn.click(
                        remove_selected_images,
                        inputs=[dog_selector, source_selector, selected_indices_state],
                        outputs=[status_text, image_gallery, selected_indices_state]
                    )
                    
                    delete_dog_btn.click(
                        delete_dog,
                        inputs=[dog_selector, source_selector],
                        outputs=[status_text, image_gallery]
                    )
                    
                    save_to_permanent_btn.click(
                        save_to_permanent,
                        outputs=status_text
                    )
                    
                    clear_permanent_btn.click(
                        clear_all_permanent,
                        outputs=status_text
                    )
                
                # ========== STEP 3: EXPORT DATASET ==========
                with gr.Tab("πŸ’Ύ Step 3: Export Dataset", id=2):
                    gr.Markdown("""
                    Export combined dataset (temporary + permanent dogs) for training.
                    **Dataset contains full body images only.**
                    """)
                    
                    format_selector = gr.Radio(
                        choices=["folder", "csv", "both"],
                        value="both",
                        label="Export Format"
                    )
                    
                    export_btn = gr.Button("πŸ“¦ Export Final Dataset", variant="primary", size="lg")
                    
                    export_output = gr.Textbox(label="Export Path", interactive=False)
                    download_file = gr.File(label="Download Dataset", interactive=False)
                    stats_display = gr.Markdown()
                    
                    def export_dataset(format_type):
                        try:
                            zip_path = self.save_final_dataset(format_type)
                            
                            with open(self.final_dir / 'metadata.json', 'r') as f:
                                metadata = json.load(f)
                            
                            stats = f"""
                            ### βœ… Dataset Exported!
                            
                            - **Total Dogs**: {metadata['total_dogs']}
                            - **Total Full Body Images**: {metadata['total_images']}
                            - **Format**: {format_type}
                            
                            Download the ZIP file below.
                            """
                            
                            return zip_path, zip_path, stats
                        except Exception as e:
                            return "", None, f"### ❌ Export Error\n{str(e)}"
                    
                    export_btn.click(
                        export_dataset,
                        inputs=format_selector,
                        outputs=[export_output, download_file, stats_display]
                    )
            
            return app

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