File size: 3,359 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
#!/usr/bin/env python3
"""
Test the encoding fix for CNN model outputs
"""
import sys
import os
from io import BytesIO
from PIL import Image

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

def test_encoding_fix():
    """Test if the encoding issue is fixed"""
    print("Testing Encoding Fix for CNN Model")
    print("=" * 40)
    
    try:
        from local_models import get_local_model_manager
        from app import extract_frames_from_video, process_image_locally
        print("+ Successfully imported components")
    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[:50]}...")
    
    # Initialize models
    try:
        local_manager = get_local_model_manager()
        print("+ Models initialized")
    except Exception as e:
        print(f"- Model error: {e}")
        return
    
    # Extract one frame 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.1)  # Just first frame
        
        if not frames:
            print("- No frames extracted")
            return
            
        test_frame = frames[0]['frame']
        print("+ Extracted test frame")
        
    except Exception as e:
        print(f"- Frame extraction error: {e}")
        return
    
    # Test CNN model with cleaned output
    print("\nTesting CNN (BLIP) with encoding fix:")
    print("-" * 40)
    
    try:
        result = process_image_locally(
            test_frame,
            "Describe what you see",
            'CNN (BLIP)',
            local_manager
        )
        
        if 'error' in result:
            print(f"- Error: {result['error']}")
        else:
            caption = result.get('generated_text', 'No caption')
            print(f"+ Result: {caption}")
            
            # Check for problematic characters
            has_issues = False
            for char in caption:
                if ord(char) > 127:
                    print(f"- Found non-ASCII character: {repr(char)} (ord: {ord(char)})")
                    has_issues = True
            
            if not has_issues:
                print("+ No encoding issues detected!")
            else:
                print("- Still has encoding issues")
                
    except Exception as e:
        print(f"- Exception: {e}")
    
    # Test Transformer for comparison
    print("\nTesting Transformer (ViT-GPT2) for comparison:")
    print("-" * 40)
    
    try:
        result = process_image_locally(
            test_frame,
            "Describe what you see",
            'Transformer (ViT-GPT2)',
            local_manager
        )
        
        if 'error' in result:
            print(f"- Error: {result['error']}")
        else:
            caption = result.get('generated_text', 'No caption')
            print(f"+ Result: {caption}")
            
    except Exception as e:
        print(f"- Exception: {e}")

if __name__ == "__main__":
    test_encoding_fix()