File size: 9,576 Bytes
0eef5f7 | 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | #!/usr/bin/env python3
"""
Person Classification TensorFlow Lite Inference Example
This script demonstrates how to use the person classification TFLite models
for binary classification (person vs. non-person) on input images.
Usage:
python inference_example.py --model flash --image test_image.jpg
python inference_example.py --model sram --image test_image.jpg
"""
import argparse
import sys
import os
import time
import numpy as np
import tensorflow as tf
from PIL import Image
from pathlib import Path
class PersonClassifier:
"""Person Classification using TensorFlow Lite models"""
def __init__(self, model_path):
"""
Initialize the classifier with a TFLite model
Args:
model_path (str): Path to the .tflite model file
"""
self.model_path = model_path
self.interpreter = None
self.input_details = None
self.output_details = None
self.input_shape = None
self.load_model()
def load_model(self):
"""Load the TensorFlow Lite model"""
try:
self.interpreter = tf.lite.Interpreter(model_path=self.model_path)
self.interpreter.allocate_tensors()
self.input_details = self.interpreter.get_input_details()
self.output_details = self.interpreter.get_output_details()
self.input_shape = self.input_details[0]['shape']
print(f"β
Model loaded successfully: {self.model_path}")
print(f"π Input shape: {self.input_shape}")
print(f"π Input dtype: {self.input_details[0]['dtype']}")
print(f"π Output shape: {self.output_details[0]['shape']}")
print(f"π Output dtype: {self.output_details[0]['dtype']}")
except Exception as e:
print(f"β Error loading model: {e}")
sys.exit(1)
def preprocess_image(self, image_path):
"""
Preprocess input image for model inference
Args:
image_path (str): Path to input image
Returns:
np.ndarray: Preprocessed image array ready for inference
"""
try:
# Load and convert image
image = Image.open(image_path).convert('RGB')
print(f"πΈ Original image size: {image.size}")
# Get target size from model input shape (height, width)
target_height = self.input_shape[1]
target_width = self.input_shape[2]
target_size = (target_width, target_height) # PIL uses (width, height)
# Resize image to model's expected input size
image = image.resize(target_size, Image.Resampling.BILINEAR)
print(f"π Resized to: {target_size} (WΓH)")
# Convert to numpy array
image_array = np.array(image, dtype=np.uint8)
# Add batch dimension [batch, height, width, channels]
image_batch = np.expand_dims(image_array, axis=0)
print(f"β
Preprocessed shape: {image_batch.shape}")
print(f"π Value range: [{image_batch.min()}, {image_batch.max()}]")
return image_batch
except Exception as e:
print(f"β Error preprocessing image: {e}")
sys.exit(1)
def predict(self, image_data):
"""
Run inference on preprocessed image data
Args:
image_data (np.ndarray): Preprocessed image data
Returns:
tuple: (probability, prediction_label, confidence)
"""
try:
# Set input tensor
self.interpreter.set_tensor(self.input_details[0]['index'], image_data)
# Run inference
start_time = time.time()
self.interpreter.invoke()
inference_time = time.time() - start_time
# Get output tensor
output_data = self.interpreter.get_tensor(self.output_details[0]['index'])
# Handle quantized vs float output
scale = self.output_details[0]['quantization'][0]
zero_point = self.output_details[0]['quantization'][1]
if scale != 0: # Quantized output
# Dequantize
dequantized_output = scale * (output_data.astype(np.float32) - zero_point)
# Apply sigmoid to get probability
probability = 1 / (1 + np.exp(-dequantized_output[0][0]))
print(f"π’ Quantized output dequantized: {dequantized_output[0][0]:.4f}")
else: # Float output
probability = float(output_data[0][0])
# Determine prediction
prediction_label = "Person" if probability > 0.5 else "Non-person"
confidence = probability if probability > 0.5 else (1 - probability)
print(f"β±οΈ Inference time: {inference_time*1000:.2f}ms")
return probability, prediction_label, confidence
except Exception as e:
print(f"β Error during inference: {e}")
sys.exit(1)
def classify_image(self, image_path):
"""
Complete pipeline: preprocess image and run classification
Args:
image_path (str): Path to input image
Returns:
dict: Classification results
"""
print(f"\nπ Classifying image: {image_path}")
print("=" * 50)
# Preprocess image
image_data = self.preprocess_image(image_path)
# Run inference
probability, prediction_label, confidence = self.predict(image_data)
# Compile results
results = {
'image_path': image_path,
'prediction': prediction_label,
'probability': probability,
'confidence': confidence,
'model_used': self.model_path
}
return results
def print_results(results):
"""Print classification results in a formatted way"""
print("\nπ CLASSIFICATION RESULTS")
print("=" * 50)
print(f"πΌοΈ Image: {results['image_path']}")
print(f"π― Prediction: {results['prediction']}")
print(f"π Probability: {results['probability']:.4f}")
print(f"β
Confidence: {results['confidence']:.1%}")
print(f"π€ Model: {Path(results['model_used']).name}")
print("=" * 50)
def main():
parser = argparse.ArgumentParser(
description="Person Classification using TensorFlow Lite models",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python inference_example.py --model flash --image test_image.jpg
python inference_example.py --model sram --image test_image.jpg
python inference_example.py --model person_classification_flash(448x640).tflite --image test_image.jpg
"""
)
parser.add_argument(
'--model',
required=True,
help='Model to use: "flash", "sram", or path to .tflite file'
)
parser.add_argument(
'--image',
required=True,
help='Path to input image file'
)
args = parser.parse_args()
# Determine model path
if args.model.lower() == 'flash':
model_path = 'person_classification_flash(448x640).tflite'
elif args.model.lower() == 'sram':
model_path = 'person_classification_sram(256x448).tflite'
else:
model_path = args.model
# Check if model file exists
if not os.path.exists(model_path):
print(f"β Model file not found: {model_path}")
sys.exit(1)
# Check if image file exists
if not os.path.exists(args.image):
print(f"β Image file not found: {args.image}")
sys.exit(1)
print("π Person Classification TensorFlow Lite Demo")
print("=" * 50)
# Initialize classifier
classifier = PersonClassifier(model_path)
# Run classification
results = classifier.classify_image(args.image)
# Print results
print_results(results)
def demo_both_models():
"""Demo function to test both models if available"""
print("π Person Classification Demo - Both Models")
print("=" * 50)
models = [
('Flash Model (VGA)', 'person_classification_flash(448x640).tflite'),
('SRAM Model (WQVGA)', 'person_classification_sram(256x448).tflite')
]
# Create a simple test image if none exists
test_image_path = 'test_person.jpg'
if not os.path.exists(test_image_path):
print(f"βΉοΈ Creating test image: {test_image_path}")
# Create a simple test image (colored rectangle)
test_img = Image.new('RGB', (640, 480), color='lightblue')
test_img.save(test_image_path)
for model_name, model_path in models:
if os.path.exists(model_path):
print(f"\nπ Testing {model_name}")
print("-" * 30)
classifier = PersonClassifier(model_path)
results = classifier.classify_image(test_image_path)
print_results(results)
else:
print(f"β οΈ {model_name} not found: {model_path}")
if __name__ == '__main__':
if len(sys.argv) == 1:
# If no arguments provided, run demo
demo_both_models()
else:
main() |