File size: 1,113 Bytes
40507b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from image_processor.analyzer import ImageAnalyzer
from PIL import Image
import numpy as np
import logging

@pytest.fixture
def sample_image():
    return Image.fromarray(np.random.randint(0, 255, (512, 512, 3), 'RGB')

def test_initialization():
    analyzer = ImageAnalyzer(device="cpu")
    assert analyzer.models is not None
    assert 'captioning' in analyzer.models

def test_image_analysis(sample_image):
    analyzer = ImageAnalyzer(device="cpu")
    results = analyzer.analyze_image(sample_image)
    
    assert isinstance(results, dict)
    assert 'title' in results
    assert 'style' in results
    assert len(results['colors']) == 5

def test_error_handling():
    analyzer = ImageAnalyzer(device="cpu")
    invalid_input = "not_an_image"
    
    with pytest.raises(Exception):
        analyzer.analyze_image(invalid_input)

def test_color_detection(sample_image):
    analyzer = ImageAnalyzer(device="cpu")
    colors = analyzer._get_colors(sample_image)
    
    assert len(colors) == 5
    assert all('hex' in c for c in colors)
    assert all(0 <= c['score'] <= 1 for c in colors)