In order to improve the stability of the hexapod robot walking in different terrains, this paper adopts the strategy corresponding to the terrain environment and the motion gait, that is, the robot selects the gait that keeps the robot walk safer and more stable according to the identified terrain. In this paper, using the self-established data set, Terrain6, for terrain classification, the feature extraction of terrain images for hexapod robot is realized firstly based on the Convolutional Neural Networks and the transfer learning. Secondly, according to the stacking fusion method, a terrain recognition model with higher precision is obtained by integrating three terrain classification models include the support vector machine, the naive Bayes and the random forest algorithm. Finally, the experiments show that the hexapod robot selects suitable gait based on the result of terrain recognition to cross complex environment, and the stable and efficient motion of the robot verifies the validity of the research results.