SunFounder picar-x
How it works?
The eSpeak software is used to implement the functions of TTS.
Import the TTS module in robot_hat, which encapsulates functions that convert text to speech.
from robot_hat import TTS
Create a string list words , then create an instantiated object of the TTS() class tts_robot , and finally use the
tts_robot.say() function to speak the words in the list in speech.
words = ["Hello", "Hi", "Good bye", "Nice to meet you"]
tts_robot = TTS()
for i in words:
print(i)
tts_robot.say(i)
4.7 Computer Vision
This next project will officially enter the field of computer vision!
To perform the next four experiments, make sure to have completed the remote_desktop. A remote connection via
SSH will not display the camera images.
Run the Code
Note:
• This project requires access to the Raspberry Pi desktop to view the footage taken by the camera module.
• You can connect a screen to the PiCar-X or refer to the tutorial remote_desktop to access it with VNC or XRDP.
• Once inside the Raspberry Pi desktop, open Terminal and type the following command to run it, or just open
and run it with a Python editor.
cd /home/pi/picar-x/example
sudo python3 computer_vision.py
After the code is run, you will see a window open on your desktop showing the shot.
Code
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
with PiCamera() as camera:
camera.resolution = (640, 480)
camera.framerate = 24
rawCapture = PiRGBArray(camera, size=camera.resolution)
time.sleep(2)
for frame in camera.capture_continuous(rawCapture, format="bgr",use_video_
˓→port=True): # use_video_port=True
(continues on next page)
60 Chapter 4. Play with Python