4. Case 23: Face-tracking with TPBot#

4.1. Purpose#


  • Build a face-tracking TPBot with the Smart AI Lens.

4.2. Materials required#


../../../_images/TPBot_tianpeng_case_20_01.png

../../../_images/TPBot_tianpeng_case_20_02.png

4.3. Hardware Connections#


Connect the AI Lens to the IIC port on TPBot.

../../../_images/TPBot_tianpeng_case_20_03.png

4.4. Software#


MicroSoft MakeCode

4.5. MakeCode Programming#


  • Click “Advanced” to see more choices in the MakeCode drawer.

../../../_images/TPBot_tianpeng_case_20_04.png

  • We need to add a package for programming. Click “Extensions” in the bottom of the drawer and seach with “tpbot” in the dialogue box to download it.

../../../_images/TPBot_tianpeng_case_20_05.png

  • We need to add a package for programming the AI Lens kit. Click “Extensions” in the bottom of the drawer and seach with https://github.com/elecfreaks/pxt-PlanetX-AI in the dialogue box to download it.

../../../_images/TPBot_tianpeng_case_20_06.png

Sample Code#

  • Initialize the AI lens kit in the On start brick and switch its function to face recognition.

../../../_images/TPBot_tianpeng_case_23_07.png

  • In forever brick, get an image from the AI lens and adjust TPBot’ direction by the face recognized in the lens.

../../../_images/TPBot_tianpeng_case_23_08.png

  • Complete code:

../../../_images/TPBot_tianpeng_case_23_09.png

Reference#

4.6. Python Programming#


Add TPBot extension: https://www.elecfreaks.com/learn-cn/microbitKit/TPbot_tianpeng/TPbot-python.html

Add AI Lens extension: https://www.elecfreaks.com/learn-cn/microbitplanetX/ai/Plant-X-EF05035-python.html

Code#

# Add your Python code here. E.g.
from microbit import *
from AILens import *
from TPBot import *

tp = TPBOT()
ai = AILENS()
# Set the function of AI Lens in face recognition
ai.switch_function(Face)

while True:
    # Get an image
    ai.get_image()

    buff = ai.get_face_data()
    # "buff[0]"means the X coordinates data of the face detected in AI lens
    i = buff[0]
    if ( i < 80):
        tp.set_motors_speed(-30,0)  
    elif ( i > 144):
        tp.set_motors_speed(0,-30)
    else:
        tp.set_motors_speed(0,0)

Result#

The TPBot changes its heading direction in accordance with the face the AI Lens detects.

4.7. Exporation#


4.8. FAQ#


Q: TPBot doesn’t work with the sample code.
A: It is probably due to the lack of battery power, please try adding the speed of the TPBot or replacing with new batteries.
Q: The AI Lens is not working, and it does not go to the function page with the sample code.
A: Please try replacing with new batteries.

4.9. Relevant File#