nn_stm32cubeai.py
# هذا العمل مرخص بموجب ترخيص MIT.
# حقوق النشر (c) 2013-2023 OpenMV LLC. جميع الحقوق محفوظة.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# مثال STM32 CUBE.AI لـ MNIST على OpenMV
# راجع https://github.com/openmv/openmv/blob/master/src/stm32cubeai/README.MD
import csi
import time
import nn_st
csi0 = csi.CSI()
csi0.reset() # إعادة ضبط المستشعر وتهيئته.
csi0.contrast(3)
csi0.brightness(0)
csi0.auto_gain(True)
csi0.auto_exposure(True)
csi0.pixformat(csi.GRAYSCALE) # تعيين صيغة البكسل إلى Grayscale
csi0.framesize(csi.QQQVGA) # تعيين حجم الإطار إلى 80x60
csi0.snapshot(time=2000) # انتظر حتى تدخل الإعدادات حيز التنفيذ.
clock = time.clock() # أنشئ كائن ساعة (clock) لتتبع معدل الإطارات (FPS).
# [CUBE.AI] تهيئة الشبكة
net = nn_st.loadnnst("network")
nn_input_sz = 28 # إدخال الشبكة العصبية هو 28x28
while True:
clock.tick() # تحديث ساعة معدل الإطارات (FPS).
img = csi0.snapshot() # التقاط صورة وإرجاعها.
# القص من المنتصف (يتجنب تظليل الحواف vignetting)
img.crop(
(
img.width() // 2 - nn_input_sz // 2,
img.height() // 2 - nn_input_sz // 2,
nn_input_sz,
nn_input_sz,
)
)
# تحويل الصورة إلى ثنائية
img.midpoint(2, bias=0.5, threshold=True, offset=5, invert=True)
# [CUBE.AI] تشغيل الاستدلال
out = net.predict(img)
print("Network argmax output: {}".format(out.index(max(out))))
img.draw_string((0, 0), str(out.index(max(out))))
print(
"FPS {}".format(clock.fps())
) # ملاحظة: تعمل كاميرا OpenMV بحوالي نصف السرعة عند الاتصال