Download example.py from nxp/deepface-emotion-imx: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/nxp/deepface-emotion-imx/resolve/main/example.py
- Command line
-
hf download hf://nxp/deepface-emotion-imx/example.py
-
curl -L -o example.py https://huggingface.co/nxp/deepface-emotion-imx/resolve/main/example.py
1.76 kB
| #!/usr/bin/env python3 | |
| # Copyright 2022-2024,2026 NXP | |
| # SPDX-License-Identifier: MIT | |
| import argparse | |
| import time | |
| import cv2 | |
| import numpy as np | |
| import tensorflow as tf | |
| LABELS = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral'] | |
| parser = argparse.ArgumentParser(description="Deepface emotion inference example") | |
| parser.add_argument("-m", "--model", | |
| default="original_model/emotion_uint8_float32.tflite", | |
| type=str, | |
| help="Path to the TFLite model file.") | |
| parser.add_argument("-i", "--input", default="example_input.jpg", type=str, | |
| help="Path to a grayscale face crop image (48x48).") | |
| args = parser.parse_args() | |
| interpreter = tf.lite.Interpreter(args.model) | |
| interpreter.allocate_tensors() | |
| input_details = interpreter.get_input_details() | |
| output_details = interpreter.get_output_details() | |
| print("Loaded model:", args.model) | |
| # Load grayscale image and resize to 48x48. | |
| im = cv2.imread(args.input, cv2.IMREAD_GRAYSCALE) | |
| if im is None: | |
| raise FileNotFoundError(f"Could not open image: {args.input}") | |
| start = time.time() | |
| im = cv2.resize(im, (48, 48)) | |
| # Shape: (1, 48, 48, 1), normalized to [0, 1]. | |
| im = im[None, ..., None].astype(np.float32) / 255.0 | |
| # Quantize input: scale from float to uint8 using model quantization params. | |
| input_scale, input_zero_point = input_details[0]["quantization"] | |
| im_q = im / input_scale + input_zero_point | |
| im_q = im_q.astype(np.uint8) | |
| interpreter.set_tensor(input_details[0]['index'], im_q) | |
| interpreter.invoke() | |
| out = interpreter.get_tensor(output_details[0]['index']) | |
| end = time.time() | |
| print("Output tensor:", out) | |
| print("Recognized emotion:", LABELS[int(out.argmax())]) | |
| print(f"Inference time: {(end - start) * 1000:.2f} ms") | |