File size: 3,082 Bytes
a6e5aa5
f23140a
 
 
 
 
a6e5aa5
f23140a
 
 
 
a6e5aa5
f23140a
 
a6e5aa5
 
 
 
 
 
f23140a
 
a6e5aa5
f23140a
a6e5aa5
f23140a
 
 
a6e5aa5
 
 
 
 
 
 
 
 
f23140a
 
a6e5aa5
f23140a
a6e5aa5
 
 
f23140a
a6e5aa5
 
 
 
 
 
 
f23140a
 
 
a6e5aa5
 
f23140a
 
 
 
a6e5aa5
f23140a
a6e5aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import cv2
import numpy as np
import logging
from tensorflow.keras.models import load_model

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class TBImageProcessor:
    """Processes TB images using a trained CNN model for risk assessment."""

    def __init__(self, model_path="tb_cnn_model.h5"):
        # Validate model path
        if not os.path.exists(model_path):
            logger.error(f"Model path '{model_path}' does not exist. Please check the path.")
            self.model = None
            return

        try:
            self.model = load_model(model_path)
            logger.info("TB Image Processor model loaded successfully.")
        except Exception as e:
            logger.error(f"Failed to load the TB Image Model: {e}")
            self.model = None

    def process_image(self, image_path):
        """Analyze a TB image and return risk assessment."""
        # Validate the image file
        if not os.path.exists(image_path):
            logger.error(f"Image path '{image_path}' does not exist.")
            return {"error": "Image file not found."}
        
        if self.model is None:
            logger.error("Model is not loaded. Cannot process the image.")
            return {"error": "Model not loaded."}

        try:
            # Load and preprocess image
            image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
            if image is None:
                logger.error(f"Failed to read the image at path '{image_path}'.")
                return {"error": "Invalid image format or corrupted file."}

            # Resize for CNN input and normalize
            if image.shape[0] < 128 or image.shape[1] < 128:
                logger.warning("Image dimensions are smaller than expected, resizing may affect accuracy.")
            image = cv2.resize(image, (128, 128))
            image = np.expand_dims(image, axis=[0, -1]) / 255.0

            # Make prediction
            prediction = self.model.predict(image)
            confidence = float(prediction[0][0])
            result = "TB Detected" if confidence > 0.5 else "No TB"

            logger.info(f"Prediction result: {result}, Confidence: {confidence:.2f}")
            return {
                "result": result,
                "confidence": confidence
            }

        except Exception as e:
            logger.error(f"Error during image processing: {e}")
            return {"error": f"Failed to process image: {str(e)}"}

# Example usage
if __name__ == "__main__":
    # Specify the model and image paths
    model_path = "path/to/your/tb_cnn_model.h5"
    image_path = "path/to/your/tb_image.jpg"

    # Instantiate the processor and analyze the image
    processor = TBImageProcessor(model_path=model_path)
    result = processor.process_image(image_path=image_path)

    # Log or print the final result
    if "error" in result:
        logger.error(f"Processing failed: {result['error']}")
    else:
        logger.info(f"Final Result: {result['result']}, Confidence: {result['confidence']:.2f}")