File size: 1,838 Bytes
5be7361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b3ac6c
 
 
5be7361
 
 
 
3b3ac6c
5be7361
 
 
 
3b3ac6c
 
 
5be7361
 
3b3ac6c
5be7361
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
from classifier import Classifier
from typing import List
from PIL import Image
import logging

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger(__name__)

classifier = Classifier()


class Inference():
    def __init__(self):
        self.classifier = classifier

    def _prepare_images(self, images: List[Image.Image]) -> List[Image.Image]:
        """
        Prepare PIL images for classification by converting to RGB
        Args:
            images: List of PIL Image objects
        Returns:
            List of RGB PIL Image objects
        """
        prepared_images = []
        for idx, image in enumerate(images):
            try:
                # Convert to RGB to ensure compatibility
                image = image.convert('RGB')
                prepared_images.append(image)
            except Exception as e:
                raise ValueError(f"Error processing image {idx}: {str(e)}")

        return prepared_images
        
    
    def classify_building(self, images: List[Image.Image], saved_image_paths: List[str] = None) -> dict:
        """
        Classify building type from a list of PIL Image objects
        Args:
            images: List of PIL Image objects
            saved_image_paths: List of paths where images were saved to disk (optional)
        Returns:
            Classification response
        """
        logger.info(f"Preparing {len(images)} images for classification")
        if saved_image_paths:
            logger.info(f"Images saved to disk at: {saved_image_paths}")
        
        prepared_images = self._prepare_images(images)
        logger.info(f"Image preparation successful")
        response = self.classifier.get_response(prepared_images, saved_image_paths)
        return response