File size: 5,678 Bytes
8a74c03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test the FIXED Person on Track Detector that no longer gives false positives
"""
import sys
import os
from io import BytesIO
import glob

# Add current directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

def test_fixed_detector():
    """Test the fixed Person on Track Detector"""
    print("TESTING FIXED PERSON ON TRACK DETECTOR")
    print("=" * 50)
    print("Should now give accurate YES/NO results")
    print()
    
    try:
        from local_models import get_local_model_manager
        from app import extract_frames_from_video, process_image_locally
        print("+ Components loaded successfully")
    except ImportError as e:
        print(f"- Import error: {e}")
        return
    
    # Test with multiple videos
    test_videos = glob.glob("test\\*.mp4")[:3]  # Test first 3 videos
    if not test_videos:
        print("- No test videos found")
        return
    
    print(f"+ Testing {len(test_videos)} videos")
    
    try:
        local_manager = get_local_model_manager()
        print("+ Fixed Person on Track Detector ready")
    except Exception as e:
        print(f"- Model error: {e}")
        return
    
    all_results = []
    
    # Test each video
    for video_idx, video_path in enumerate(test_videos):
        video_name = os.path.basename(video_path)
        print(f"\n" + "=" * 50)
        print(f"VIDEO {video_idx + 1}: {video_name}")
        print("=" * 50)
        
        try:
            # Extract frames
            with open(video_path, 'rb') as f:
                video_data = f.read()
            
            video_file = BytesIO(video_data)
            frames = extract_frames_from_video(video_file, fps=0.5)
            
            if not frames:
                print(f"- No frames from {video_name}")
                continue
            
            # Test first 2 frames per video
            test_frames = frames[:2]
            
            for frame_idx, frame_data in enumerate(test_frames):
                frame_num = frame_idx + 1
                timestamp = frame_data['timestamp']
                
                print(f"\n  Frame {frame_num} ({timestamp:.1f}s):")
                print(f"  {'-' * 30}")
                
                try:
                    result = process_image_locally(
                        frame_data['frame'],
                        "Track Safety Analysis",
                        'Person on Track Detector',
                        local_manager
                    )
                    
                    if 'person_on_track_detection' in result:
                        detection = result['person_on_track_detection']
                        
                        on_track = detection.get('person_on_track', False)
                        answer = detection.get('answer', 'UNKNOWN')
                        confidence = detection.get('confidence', 0)
                        reasoning = detection.get('reasoning', 'No reasoning')
                        
                        # Show result with clear status
                        if on_track:
                            print(f"  🚨 PERSON ON TRACK: {answer} ({confidence:.0%})")
                        else:
                            print(f"  βœ… TRACKS CLEAR: {answer} ({confidence:.0%})")
                        
                        print(f"  Reasoning: {reasoning}")
                        
                        all_results.append({
                            'video': video_name,
                            'frame': frame_num,
                            'on_track': on_track,
                            'answer': answer,
                            'confidence': confidence
                        })
                        
                    else:
                        print(f"  ERROR: Unexpected result format")
                        
                except Exception as e:
                    print(f"  ERROR: {e}")
            
        except Exception as e:
            print(f"- Failed to process {video_name}: {e}")
    
    # Summary
    print(f"\n" + "=" * 60)
    print("SUMMARY OF FIXED DETECTOR PERFORMANCE")
    print("=" * 60)
    
    if all_results:
        total = len(all_results)
        yes_count = sum(1 for r in all_results if r['answer'] == 'YES')
        no_count = sum(1 for r in all_results if r['answer'] == 'NO')
        avg_confidence = sum(r['confidence'] for r in all_results) / total
        
        print(f"Total frames tested: {total}")
        print(f"YES results (person on track): {yes_count}")
        print(f"NO results (tracks clear): {no_count}")
        print(f"Average confidence: {avg_confidence:.0%}")
        
        if no_count > 0:
            print(f"\nβœ… SUCCESS: Detector now gives NO results!")
            print(f"   - Fixed the false positive issue")
            print(f"   - Now provides varied and accurate responses")
        else:
            print(f"\n❌ STILL PROBLEMATIC: Only giving YES results")
        
        print(f"\nDETAILED RESULTS:")
        for r in all_results:
            status = "🚨" if r['on_track'] else "βœ…"
            print(f"  {r['video']} Frame {r['frame']}: {status} {r['answer']} ({r['confidence']:.0%})")
    
    print(f"\n" + "=" * 60)
    print("NEXT STEPS")
    print("=" * 60)
    print("1. Open http://localhost:8502")
    print("2. Select 'Person on Track Detector' from dropdown")
    print("3. Upload videos from test/ folder")
    print("4. Verify you now get both YES and NO results")
    print("5. Check that reasoning makes sense")
    
    return all_results

if __name__ == "__main__":
    test_fixed_detector()