Camera Tutorial

mPython Core 2.0 has a built-in camera that can be used for AI application programming education including face detection, face recognition, QR code recognition, and other image-based applications.

  • Model types:

    • Face Detection: A model for detecting faces. It can detect multiple faces simultaneously and obtain the position of faces in the frame, as well as the coordinates of key facial features such as eyes, nose, and mouth.

    • Face Recognition: An AI model for identifying specific individuals. It can also detect the position of faces in the frame and obtain the coordinates of key facial features such as eyes, nose, and mouth.

    • QR Code Recognition: Used to recognize QR codes or barcodes, capable of obtaining QR code content and barcode content.

    • Motion Detection: Used to detect whether there is a person moving in the frame or whether the scene is changing.

    • Cat and Dog Detection: A model for detecting whether the subject is a cat or a dog. It can recognize multiple cats or dogs simultaneously.

Face Detection

Print the number of faces to the console.

Graphical code

../../../../../_images/face_detection.png

Graphical code

Python code

# Import required libraries
from mpython import *
import AIcamera
import time

# Set detection flag variable for data update
isDetect = False

# Detection callback
def cb(_):
    global isDetect
    isDetect = True # Set flag to True when a face is detected

# Set up the model
AIcamera.init(AIcamera.FACE_DETECTION, cb)

while True:
    if isDetect: # When flag is True, detection occurred
        # Get detection result and assign to res variable
        res = AIcamera.get_result()
        print('Face count: ' + str(len(res)))
        isDetect = False # Clear detection flag
    time.sleep(0.1)

Note

Get the detection result including the bounding box position and keypoint coordinates (left mouth corner, right mouth corner, nose, left eye, right eye) of detected faces. The result is a tuple type.
Structure: ({‘box’: (x1, y1, x2, y2), ‘keypoint’: (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)}, …)
box is the face position, with the top-left corner coordinates (x1, y1) and bottom-right corner coordinates (x2, y2) of the face bounding box
keypoint contains keypoints: left mouth corner (a0, a1), right mouth corner (a2, a3), nose (a4, a5), left eye (a6, a7), right eye (a8, a9)

Face Recognition

Print the face name (assuming Zhang San and Li Si are enrolled), face confidence, and face position to the console.

Graphical code

[1] Enrolling faces

../../../../../_images/face_recognition_enter.png

Graphical code

[2] Recognizing faces

../../../../../_images/face_recognition.png

Graphical code

Python code

[1] Enrolling faces

# Import required libraries
from mpython import *
import AIcamera

# Set flag variable for data update
isDetect = False

# Set up the model
AIcamera.init(AIcamera.FACE_RECOGNITION, lambda _: None)

# Set button A callback to enroll face data when pressed
def on_button_a_pressed(_):
# Perform face enrollment
AIcamera.send_command(AIcamera.ENROLL)

# Set button B callback to delete face data when pressed
def on_button_b_pressed(_):
# Perform face deletion
AIcamera.send_command(AIcamera.DELETE)

# Bind A/B button events
button_a.event_pressed = on_button_a_pressed
button_b.event_pressed = on_button_b_pressed

Note

A face must be detected in the frame before enrolling or deleting faces
Face data IDs increment from 1 when enrolling
Face data is deleted from the last entry backwards until 0
Enrolled data is retained in memory and will not be cleared when the program stops. (It is recommended to delete all data before each enrollment)

[2] Recognizing faces

# Import required libraries
from mpython import *
import AIcamera
import time

# Set flag variable for data update
isDetect = False

# Recognition callback
def cb(_):
    global isDetect
    isDetect = True # Set flag to True when a face is recognized

# Set up the model
AIcamera.init(AIcamera.FACE_RECOGNITION, cb)

while True:
    # Run recognition
    AIcamera.send_command(AIcamera.RECOGNIZE)
    if isDetect: # When flag is True, recognition occurred
        # Get recognition result and assign to res variable
        res = AIcamera.get_result()
        face_id = res.get('id')
        if face_id == 1:
            print('This is Zhang San')
        elif face_id == 2:
            print('This is Li Si')
        print('Confidence: ' + str(res.get('Similarity')))
        print('Face position: ' + str(res.get('box')))
        isDetect = False # Clear recognition flag
    time.sleep(0.1)

Note

Get the recognition result including the detected face ID, confidence, bounding box position, and keypoint coordinates (left mouth corner, right mouth corner, nose, left eye, right eye). The result is a dictionary type.
Structure: {‘box’: (x1, y1, x2, y2), ‘Similarity’: sim, ‘keypoint’: (a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)}
box is the face position, with the top-left corner coordinates (x1, y1) and bottom-right corner coordinates (x2, y2) of the face bounding box
Similarity is the confidence level
keypoint contains keypoints: left mouth corner (a0, a1), right mouth corner (a2, a3), nose (a4, a5), left eye (a6, a7), right eye (a8, a9)

QR Code Recognition

Control a light on and off by recognizing QR codes.

Graphical code

../../../../../_images/qr_recognition.png

Graphical code

Python code

# Import required libraries
from mpython import *
import AIcamera
import time

# Set recognition flag variable for data update
isDetect = False

# Recognition callback
def cb(_):
    global isDetect
    isDetect = True # Set flag to True when a QR code is recognized

# Set up the model
AIcamera.init(AIcamera.CODE_SCANNER, cb)

while True:
    if isDetect: # When flag is True, recognition occurred
        try:
            # Get recognition result and assign to qr_txt variable
            qr_txt = AIcamera.get_result()
            if qr_txt == 'open':
                print('Light on')
            elif qr_txt == 'close':
                print('Light off')
        except: pass
        isDetect = False # Clear recognition flag
    time.sleep(0.1)

Note

Get the recognition result. The result is the QR code content as a string type.

Motion Detection

Detect whether there is motion and print the motion status to the console.

Graphical code

../../../../../_images/motion_detection.png

Graphical code

Python code

# Import required libraries
from mpython import *
import AIcamera
import time

# Set detection flag variable for data update
isDetect = False

# Detection callback
def cb(_):
    global isDetect
    isDetect = True # Set flag to True when motion is detected

# Set up the model
AIcamera.init(AIcamera.MOTION_DEECTION, cb)

while True:
    if isDetect: # When flag is True, motion detected
        print('Motion detected')
        isDetect = False # Clear detection flag
    else:
        print('No motion')
    time.sleep(0.1)

Cat and Dog Detection

Print the number of cat faces to the console.

Graphical code

../../../../../_images/cat_detection.png

Graphical code

Python code

# Import required libraries
from mpython import *
import AIcamera
import time

# Set detection flag variable for data update
isDetect = False

# Detection callback
def cb(_):
    global isDetect
    isDetect = True # Set flag to True when a cat face is detected

# Set up the model
AIcamera.init(AIcamera.CAT_FACE_DETECTION, cb)

while True:
    if isDetect: # When flag is True, detection occurred
        # Get detection result and assign to res variable
        res = AIcamera.get_result()
        print('Cat face count: ' + str(len(res)))
        isDetect = False # Clear detection flag
    time.sleep(0.1)

Note

Get the detection result including the bounding box position of detected cat/dog faces. The result is a tuple type.
Structure: ({‘box’: (x1, y1, x2, y2), …}, …)
box is the cat/dog face position, with the top-left corner coordinates (x1, y1) and bottom-right corner coordinates (x2, y2) of the face bounding box