movenet_singlepose_detection.py

# هذا العمل مرخص بموجب ترخيص MIT.
# حقوق النشر (c) 2013-2026 لشركة OpenMV LLC. جميع الحقوق محفوظة.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# يوضح هذا المثال نموذج MoveNet من Google لتقدير الوضعية.
#
# ملاحظة: يتطلب هذا المثال كاميرا OpenMV تحتوي على وحدة معالجة عصبية (NPU) مثل AE3 أو N6 للعمل بشكل فوري.

import csi
import time
import ml
from ml.postprocessing.mediapipe import MoveNet

# تهيئة المستشعر.
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.VGA)

# تحميل نموذج اكتشاف الوضعية المدمج
model = ml.Model("/rom/movenet_singlepose_192.tflite", postprocess=MoveNet(threshold=0.4))
print(model)

# خطوط الاتصال بين مفاصل الجسم لرسم هيكل الجسم العظمي.
body_lines = ((0, 1), (0, 2), (1, 3), (2, 4), (0, 5), (0, 6), (5, 6), (5, 7),
              (7, 9), (6, 8), (8, 10), (5, 11), (6, 12), (11, 12), (11, 13), (13, 15),
              (12, 14), (14, 16))


# إزالة النقاط المميزة ذات الثقة المنخفضة وخطوط الهيكل العظمي المتصلة بها.
def filter_keypoints(keypoints, threshold=0.4):
    valid = {i for i, kp in enumerate(keypoints) if kp[2] > threshold}
    remap = {old: new for new, old in enumerate(sorted(valid))}
    f_keypoints = [kp for i, kp in enumerate(keypoints) if i in valid]
    f_body_lines = [(remap[a], remap[b]) for a, b in body_lines if a in valid and b in valid]
    return f_keypoints, f_body_lines


clock = time.clock()
while True:
    clock.tick()
    img = csi0.snapshot()

    # joints هي قائمة من مجموعات ((x, y, w, h), score, keypoints)
    joints = model.predict([img])

    # رسم مربعات إحاطة حول الأشخاص المكتشفين والنقاط المميزة.
    for r, score, keypoints in joints:
        ml.utils.draw_predictions(img, [r], ("person",), ((0, 0, 255),), format=None)

        # keypoints: مصفوفة ndarray (17, 3) لمفاصل الجسم (x, y, score)
        # تتبع الفهارس اصطلاح COCO:
        # 0: الأنف
        # 1: العين اليسرى، 2: العين اليمنى
        # 3: الأذن اليسرى، 4: الأذن اليمنى
        # 5: الكتف الأيسر، 6: الكتف الأيمن
        # 7: المرفق الأيسر، 8: المرفق الأيمن
        # 9: المعصم الأيسر، 10: المعصم الأيمن
        # 11: الورك الأيسر، 12: الورك الأيمن
        # 13: الركبة اليسرى، 14: الركبة اليمنى
        # 15: الكاحل الأيسر، 16: الكاحل الأيمن
        f_keypoints, f_body_lines = filter_keypoints(keypoints)
        ml.utils.draw_skeleton(img, f_keypoints, f_body_lines,
                               kp_color=(255, 0, 0), line_color=(0, 255, 0))

    print(clock.fps(), "fps")

results matching ""

    No results matching ""