As the grid coverage rises, foreign objects invade more and more frequently, causing grid failures to rise every year. To address this issue, this paper proposes a deep learning-based transmission line unmanned inspection of foreign objects recognition algorithm. The algorithm is based on YOLOv7 (You Only Look Once) algorithm, combining with hyperparameter optimization based on genetic algorithm (GA) and space-to-depth (SPD) convolution to complete the foreign object recognition of transmission line Unmanned Aerial Vehicle (UAV) images. The proposed method can promptly determine and locate these targets' presence in aerial images. Finally, this paper compares the improved YOLOv7 algorithm with other YOLO series algorithms (Faster-rcnn, Centernet, and other target detection models). The comparison results show that the method has the highest Mean Average Precision (mAP) of 92.2% and the Frames Per Second (FPS) of 19 is second only to Centernet. Compared with the unimproved YOLOv7, the average accuracy in the recognition of tower cranes has increased by 11.9%, which is the most obvious improvement in accuracy compared with other detection targets. Meanwhile, the hyperparameter optimization based on genetic algorithm speeds up the convergence of the model.