[addb71]: / utils / classification.py

Download this file

58 lines (43 with data), 2.1 kB

 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
import cv2
import numpy as np
from tensorflow.keras.models import load_model
import os
# Load model with input shape validation
CLASSIFIER_PATH = os.path.join(os.path.dirname(__file__), '..', 'app', 'model', 'classifier_model.h5')
classifier_model = load_model(CLASSIFIER_PATH)
# Get model's expected input shape
if len(classifier_model.input_shape) == 4: # CNN (e.g., (None, 120, 120, 1))
TARGET_SIZE = classifier_model.input_shape[1:3]
GRAYSCALE = classifier_model.input_shape[-1] == 1
else: # Flattened input (e.g., (None, 57600))
TARGET_SIZE = (int(np.sqrt(classifier_model.input_shape[1]/3)), )*2 if not GRAYSCALE else (int(np.sqrt(classifier_model.input_shape[1])),)*2
GRAYSCALE = classifier_model.input_shape[1] % 3 != 0
print(f"Model expects: {TARGET_SIZE} {'grayscale' if GRAYSCALE else 'RGB'}")
def preprocess_image(image_path):
"""Universal preprocessor adapting to model requirements"""
# Read image
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE if GRAYSCALE else cv2.IMREAD_COLOR)
if img is None:
raise ValueError("Invalid image file")
# Resize and normalize
img = cv2.resize(img, TARGET_SIZE)
img = img.astype(np.float32) / 255.0
# Format for model
if len(classifier_model.input_shape) == 4:
return np.expand_dims(img, axis=(0, -1)) if GRAYSCALE else np.expand_dims(img, axis=0)
else:
return img.reshape(1, -1) # Flatten
def classify_disease(image_path):
try:
# Preprocess
input_img = preprocess_image(image_path)
# Predict
preds = classifier_model.predict(input_img)
classes = ['Normal', 'COVID-19', 'Pneumonia', 'Tuberculosis']
# Interpret results
predicted_idx = np.argmax(preds)
confidence = float(np.max(preds))
severity = "Severe" if confidence > 0.9 else "Moderate" if confidence > 0.7 else "Mild"
return classes[predicted_idx], confidence, severity
except Exception as e:
raise ValueError(f"Classification failed: {str(e)}")