SunFounder PiCar-X Kit
(continued from previous page)
# change the pan-tilt angle for track the object
x_angle +=(coordinate_x*10/640)-5
x_angle = clamp_number(x_angle,-35,35)
px.set_cam_pan_angle(x_angle)
y_angle -=(coordinate_y*10/480)-5
y_angle = clamp_number(y_angle,-35,35)
px.set_cam_tilt_angle(y_angle)
sleep(0.05)
else :
pass
sleep(0.05)
if __name__ == "__main__":
try:
main()
finally:
px.stop()
print("stop and exit")
sleep(0.1)
How it works?
These lines of code in while True make the camera follow the face.
while True:
if Vilib.detect_obj_parameter['human_n']!=0:
coordinate_x = Vilib.detect_obj_parameter['human_x']
coordinate_y = Vilib.detect_obj_parameter['human_y']
# change the pan-tilt angle for track the object
x_angle +=(coordinate_x*10/640)-5
x_angle = clamp_number(x_angle,-35,35)
px.set_cam_pan_angle(x_angle)
y_angle -=(coordinate_y*10/480)-5
y_angle = clamp_number(y_angle,-35,35)
px.set_cam_tilt_angle(y_angle)
1. Check if there is a detected human face
Vilib.detect_obj_parameter['human_n'] != 0
2. If a human face is detected, obtain the coordinates ( coordinate_x and coordinate_y ) of the detected face.
3. Calculate new pan and tilt angles ( x_angle and y_angle ) based on the detected face’s position and adjust them
to follow the face.
4. Limit the pan and tilt angles within the specified range using the clamp_number function.
5. Set the camera’s pan and tilt angles using px.set_cam_pan_angle() and px.set_cam_tilt_angle() .
74 Chapter 4. Play with Python