1import numpy as np
2import onnxruntime as ort
3import cv2
4
5# モデルのロード
6session = ort.InferenceSession("posture_classifier.onnx")
7
8# 前処理
9MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(3, 1, 1)
10STD = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(3, 1, 1)
11CLASSES = ["good", "slouch", "chin_rest", "stretch"]
12
13def preprocess(bgr_frame):
14 rgb = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB)
15 resized = cv2.resize(rgb, (224, 224))
16 tensor = resized.astype(np.float32) / 255.0
17 tensor = tensor.transpose(2, 0, 1) # HWC → CHW
18 tensor = (tensor - MEAN) / STD
19 return tensor[np.newaxis, ...] # (1, 3, 224, 224)
20
21# 推論
22frame = cv2.imread("test_image.jpg")
23input_tensor = preprocess(frame)
24logits = session.run(["logits"], {"image": input_tensor})[0]
25
26# Softmax → クラス予測
27exp = np.exp(logits - np.max(logits, axis=-1, keepdims=True))
28probs = exp / exp.sum(axis=-1, keepdims=True)
29class_idx = int(np.argmax(probs[0]))
30print(f"Predicted: {CLASSES[class_idx]} ({probs[0][class_idx]:.2%})")
1@misc{kanden_posture_model_2025,
2 title = {Posture Classifier: ResNet18-based Engineer Fatigue Posture Detection},
3 author = {Team NANIWA-Factory},
4 year = {2025},
5 url = {https://huggingface.co/SeiyaCM/KandenAiHackathonPostureModel},
6 note = {Kanden AI Hackathon — Space AI Brain Project}
7}