File size: 6,892 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
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
#!/usr/bin/env python3
"""
Test the new Yes/No Person Detector
"""
import sys
import os
from io import BytesIO

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

def test_yes_no_detector():
    """Test the optimized Yes/No Person Detector"""
    print("TESTING YES/NO PERSON DETECTOR")
    print("=" * 50)
    print("Model: Local CNN (BLIP) - Best performer (100% success rate)")
    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
    
    # Find video file
    video_files = [f for f in os.listdir('.') if f.endswith('.mp4')]
    if not video_files:
        print("- No MP4 files found")
        return
    
    video_path = video_files[0]
    print(f"+ Using video: {video_path[:40]}...")
    
    # Initialize models
    try:
        local_manager = get_local_model_manager()
        available_models = local_manager.get_available_models()
        print(f"+ Available models: {available_models}")
        
        if "Yes/No Person Detector" not in available_models:
            print("- Yes/No Person Detector not found!")
            return
        
        print("+ Yes/No Person Detector ready")
    except Exception as e:
        print(f"- Model initialization error: {e}")
        return
    
    # Extract frames for testing
    try:
        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)  # Every 2 seconds
        
        if not frames:
            print("- No frames extracted")
            return
        
        print(f"+ Extracted {len(frames)} frames for testing")
        
        # Test with first 5 frames
        test_frames = frames[:5]
        
    except Exception as e:
        print(f"- Frame extraction error: {e}")
        return
    
    # Test Yes/No Person Detector on each frame
    print(f"\nTesting Yes/No Person Detector on {len(test_frames)} frames:")
    print("=" * 70)
    
    results = []
    
    for i, frame_data in enumerate(test_frames):
        frame_num = i + 1
        timestamp = frame_data['timestamp']
        
        print(f"\nFRAME {frame_num} (t={timestamp:.1f}s)")
        print("-" * 40)
        
        try:
            result = process_image_locally(
                frame_data['frame'],
                "Is there a person in this image?",  # This prompt is automatic
                'Yes/No Person Detector',
                local_manager
            )
            
            if 'error' in result:
                print(f"ERROR: {result['error']}")
                results.append({'frame': frame_num, 'answer': 'ERROR', 'confidence': 0})
            elif 'yes_no_detection' in result:
                detection = result['yes_no_detection']
                
                answer = detection.get('answer', 'UNKNOWN')
                person_detected = detection.get('person_detected', False)
                confidence = detection.get('confidence', 0)
                raw_response = detection.get('raw_response', 'N/A')
                
                # Display results
                print(f"Answer: {answer}")
                print(f"Person Detected: {person_detected}")
                print(f"Confidence: {confidence:.0%}")
                print(f"Raw Response: {raw_response}")
                
                results.append({
                    'frame': frame_num,
                    'timestamp': timestamp,
                    'answer': answer,
                    'person_detected': person_detected,
                    'confidence': confidence,
                    'raw_response': raw_response
                })
                
            else:
                print(f"Unexpected result format: {result}")
                results.append({'frame': frame_num, 'answer': 'UNKNOWN', 'confidence': 0})
                
        except Exception as e:
            print(f"ERROR: {e}")
            results.append({'frame': frame_num, 'answer': 'ERROR', 'confidence': 0})
    
    # Summary table
    print(f"\n" + "=" * 70)
    print("RESULTS SUMMARY TABLE")
    print("=" * 70)
    
    print(f"{'Frame':<8} {'Time':<8} {'Answer':<10} {'Detected':<10} {'Confidence':<12} {'Raw Response':<30}")
    print("-" * 78)
    
    for result in results:
        frame = result.get('frame', 0)
        timestamp = result.get('timestamp', 0)
        answer = result.get('answer', 'N/A')
        detected = 'Yes' if result.get('person_detected', False) else 'No'
        confidence = result.get('confidence', 0)
        raw_response = result.get('raw_response', 'N/A')[:25] + "..." if len(result.get('raw_response', '')) > 25 else result.get('raw_response', 'N/A')
        
        print(f"{frame:<8} {timestamp:<8.1f} {answer:<10} {detected:<10} {confidence:<12.0%} {raw_response:<30}")
    
    # Performance analysis
    print(f"\n" + "=" * 70)
    print("PERFORMANCE ANALYSIS")
    print("=" * 70)
    
    total = len(results)
    yes_count = sum(1 for r in results if r.get('answer') == 'YES')
    no_count = sum(1 for r in results if r.get('answer') == 'NO')
    error_count = sum(1 for r in results if r.get('answer') == 'ERROR')
    unclear_count = sum(1 for r in results if r.get('answer') == 'UNCLEAR')
    
    success_rate = (yes_count + no_count) / total * 100 if total > 0 else 0
    avg_confidence = sum(r.get('confidence', 0) for r in results) / total if total > 0 else 0
    
    print(f"Total frames tested: {total}")
    print(f"YES answers: {yes_count}")
    print(f"NO answers: {no_count}")  
    print(f"ERROR responses: {error_count}")
    print(f"UNCLEAR responses: {unclear_count}")
    print(f"Success rate: {success_rate:.1f}%")
    print(f"Average confidence: {avg_confidence:.0%}")
    
    print(f"\nMODEL RECOMMENDATION:")
    if success_rate >= 80:
        print("+ EXCELLENT: Yes/No Person Detector is working perfectly")
        print("+ Ready for production use in Streamlit app")
        print("+ Provides clear yes/no answers with high confidence")
    elif success_rate >= 60:
        print("+ GOOD: Yes/No Person Detector is working well")
        print("+ Minor issues but suitable for most use cases")
    else:
        print("- NEEDS IMPROVEMENT: Success rate below 60%")
        print("- Consider adjusting prompts or model parameters")
    
    print(f"\nNext steps:")
    print("1. Open http://localhost:8502")
    print("2. Select 'Yes/No Person Detector' from model dropdown")
    print("3. Upload your video")
    print("4. Click 'Process Video' for simple yes/no person detection")
    
    return results

if __name__ == "__main__":
    test_yes_no_detector()