Detects changes in the current screen and determines whether or not there is movement of objects in the detection area.
from m5stack import *
from m5ui import *
from uiflow import *
import unit
setScreenColor(0x222222)
V_0 = unit.get(unit.V_FUNCTION, unit.PORTB)
motion = V_0.init(V_0.MOTION)
motion.setMotionDetectThreshold(0)
motion.setDetectMode(0)
whileTrue:
if (motion.getMaxDiff()) >= 50:
print('Moved')
else:
print('Not Move')
wait_ms(2)
API
import unit
V_0 = unit.get(unit.V_FUNCTION, unit.PORTB)
motion = V_0.init(V_0.MOTION)
Initialization
motion.setMotionDetectThreshold(0)
Setting the rate of change threshold.
Pixels with a change smaller than this value are not considered to have changed, and their change is not included in the screen variable rate.
motion.setDetectMode(0)
Setting the detection mode:
dynamic(0): Dynamic Detection Mode, when configured, will continuously take pictures and compare the changes between the two frames before and after.
static(1): In static detection mode, a reference picture will be taken and saved after execution, and subsequent images will be compared with this picture continuously. If you need to take a new reference picture, you need to switch back to the dynamic detection mode first, and then execute the static detection mode setting again.
motion.getRateOfDiff()
Getting the rate of change of the screen:
Frame rate of change: Detects the amount of change in the pixels that have changed between the two frames before and after the comparison. Assuming that two pixels have changed, pixel A has changed by 27 and pixel B has changed by 10, the change value is 27+10=37. The difference between the R.G.B components of the two pixels is summed up as the amount of change.
motion.getMaxDiff()
Getting the highest rate of change:
Highest rate of change:The amount of change in the pixel with the most dramatic change.
motion.setScanInterval(0, 0)
Sets the scanning interval on the x-axis as well as the y-axis.
print(motion.getBoxNumber())
Gets the number of bounding box counts generated by pixel changes.
print(motion.getBoxDetail(1))
Get x bounding box information.
The details of the x bounding box are returned in a list, including the number of pixels that have changed within the bounding box, the x-axis coordinates of the bounding box, the y-axis coordinates of the bounding box, the width of the bounding box, and the height of the bounding box.
Return
list[0] number of pixels changed
list[1] x
list[2] y
list[3] width
list[4] height
Target Track
Example
Set the tracking target and get the position information of the target object in the screen in real time.
from m5stack import *
from m5ui import *
from uiflow import *
import time
import unit
setScreenColor(0x222222)
V_0 = unit.get(unit.V_FUNCTION, unit.PORTB)
target = V_0.init(V_0.TARGET_TRACK)
target.setTrackAreaCoordinate(50, 50, 30, 30)
whileTrue:
print((str('X :') + str(((str((target.getBoxDetail())[0]) + str(((str('Y :') + str((target.getBoxDetail())[2])))))))))
wait(1)
wait_ms(2)
API
import unit
V_0 = unit.get(unit.V_FUNCTION, unit.PORTB)
target = V_0.init(V_0.TARGET_TRACK)
Initialization
target.setTrackAreaCoordinate(50, 50, 30, 30)
Set tracking frame coordinates x y Tracking frame, width, height
Set the target box, the parameter is the position of the current target on the image (as far as possible to select the target with significant color characteristics)
target.getBoxDetail()
Get tracking frame track details
Retrieve the coordinates of the image where the target box is located, Return is a list containing the coordinates of the upper-left corner of the box, x, y, as well as the width and height of the box.
Return:
list[0] x
list[1] y
list[2] width
list[3] height
Color Track
Example
Set the LAB color threshold, track the target that meets the threshold in the screen, and obtain the position information of the target object in the screen in real time.
import unit
V_0 = unit.get(unit.V_FUNCTION, unit.PORTB)
color_track = V_0.init(V_0.COLOR_TRACK)
Initialization
color_track.setTrackColorByLAB(0, 0, 0, 0, 0, 0)
Set the LAB threshold for tracking (the value of a color in the LAB color space, out of which colors will be filtered).
color_track.setScanInterval(0, 0)
Set the scanning interval on the x-axis and y-axis, [0, 40], set to 0 to turn off bounding box detection.
color_track.setBoxMergeThreshold(0)
Sets the bounding box merge threshold.
color_track.setBoxWidthThreshold(0, 0)
Set border width threshold 0 height threshold 0
print(color_track.getBoxNumber())
Get the number of borders
print(color_track.getBoxDetail(1))
Get the details of the bounding box, including the number of changing pixels in the bounding box, the coordinates of the x-axis of the bounding box, the coordinates of the y-axis of the bounding box, the width of the bounding box, and the height of the bounding box.
Return:
list[0] number of pixels changed
list[1] x
list[2] y
list[3] width
list[4] height
Face Detect
Example
Recognize the face information in the screen, and return the number of recognition, object coordinates, confidence rate.
import unit
V_0 = unit.get(unit.V_FUNCTION, unit.PORTB)
face = V_0.init(V_0.FACE_DETECT)
Initialization
face.getFaceNumber()
Read the number of recognized faces
face.getFaceDetail(1)
Retrieve the details of the face with the specified number, and return a list containing the coordinates, length, width, and confidence rate of the face.
Return:
list[0] face probability
list[1] x
list[2] y
list[3] width
list[4] height
QR Code
Example
Recognizes the QR code on the screen, and returns the result, as well as the version. Use firmware Find code
from m5stack import *
from m5ui import *
from uiflow import *
import unit
setScreenColor(0x222222)
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
qr = V_1.init(V_1.QR_CODE)
whileTrue:
print((str('QRcode version: ') + str((qr.getQRversion()))))
print((str('Code text: ') + str((qr.getQRtext()))))
wait_ms(2)
API
import unit
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
qr = V_1.init(V_1.QR_CODE)
Initialization
qr.getQRtext()
Read the content of the recognized QR code
qr.getQRversion()
Read the recognized version of the QR code
Apriltag Code
Example
Identify the Apriltag code in the screen (only Tag36H11 type is supported), and get the offset of its position. Use firmware Find code
from m5stack import *
from m5ui import *
from uiflow import *
import unit
setScreenColor(0x222222)
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
tag = V_1.init(V_1.APRILTAG)
whileTrue:
print((str('AprilTag location: ') + str((tag.getAprilTagcodeLocation()))))
print((str('AprilTag rotation: ') + str((tag.getAprilTagcodeRotation()))))
print((str('AprilTag translation: ') + str((tag.getAprilTagcodeTranslation()))))
wait_ms(2)
API
import unit
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
tag = V_1.init(V_1.APRILTAG)
Initialization
tag.getAprilTagcodeLocation()
Read the boxed coordinates, center coordinates, length and width of the recognized AprilTag code, Return as a list.
tag.getAprilTagcodeRotation()
Returns the spin of AprilTag in radians (int)
tag.getAprilTagcodeTranslation()
Read the positional offset of the Apriltag code
Bar Code
Example
Recognizes barcodes on the screen and returns the result, as well as the version. Using firmware Find code
from m5stack import *
from m5ui import *
from uiflow import *
import unit
setScreenColor(0x222222)
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
bar = V_1.init(V_1.BARCODE)
whileTrue:
print((str('Code text: ') + str((bar.getBARcodeText()))))
print((str('Code type: ') + str((bar.getBARcodeRotation()))))
print((str('Code rotation: ') + str((bar.getBARcodeType()))))
print((str('Code location: ') + str((bar.getBARcodeLocation()))))
wait_ms(2)
API
import unit
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
bar = V_1.init(V_1.BARCODE)
Initialization
bar.getBARcodeLocation()
Read the boxed coordinates of the recognized barcode, the length and width of the barcode, and Return as a list.
bar.getBARcodeRotation()
Read the rotation angle of the recognized barcode
bar.getBARcodeText()
Read the contents of the recognized bar code
bar.getBARcodeType()
Read recognized barcode categories
Datamatrix Code
Example
Recognizes the Datamatrix code in the screen, and returns the recognition result, as well as the code rotation angle and coordinate data. Use firmware Find code.
import unit
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
dm = V_1.init(V_1.DATAMATRIX)
Initialization
dm.getDMcodeLocation()
Read the boxed coordinates of the recognized Datamatrix code, length width, Return as a list of
dm.getDMcodeRotation()
Read the recognized Datamatrix code rotation angle
dm.getDMcodeText()
Read the contents of the recognized Datamatrix code
Line Tracker
Example
Detects a line of the specified color in the screen and returns the offset angle.
from m5stack import *
from m5ui import *
from uiflow import *
import unit
setScreenColor(0x222222)
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
line_tracker = V_1.init(V_1.LINE_TRACKER)
whileTrue:
print((str('Line angle') + str((line_tracker.getAngle()))))
wait_ms(2)
API
import unit
V_1 = unit.get(unit.V_FUNCTION, unit.PORTB)
line_tracker = V_1.init(V_1.LINE_TRACKER)
Initialization
line_tracker.getAngle()
Get line offset angle
line_tracker.setLineAreaWeight(0, 0, 0)
Setting the weights of the line areas: the three weights correspond to the contribution of each of the three areas of the figure to the angle. For example, if the value of weight_2 is set to a larger value, the angle will change more drastically when turning.
line_tracker.setLineColorByLAB(0, 0, 0, 0, 0, 0)
Set the LAB threshold for tracking (the value of a color in the LAB color space, out of which colors will be filtered).
Tag Reader
Example
Detects the tag card in the screen and returns the binary sequence. Note: Only the fixed label card format is recognized.