This paper proposes a new method based on monocular vision to detect and range obstacles in front of the vehicle for anti-collision warning during driving. Firstly, the deep learning object detection YOLOv4 algorithm is used to detect various obstacles in front of the vehicle to obtain the category and location information of the obstacles. Then an improved edge detection algorithm is used to adjust the position of the detection frame to improve the object positioning accuracy of the detection algorithm. Next, according to the camera imaging principle and geometric relationship, conversion model from the three-dimensional coordinates of the road surface to the two-dimensional coordinates of the image plane is obtained and distance measurement is performed. Finally, the cubic curve fitting of the obtained measurement data is performed, and the distance measurement process and algorithms are optimized to improve the distance measurement accuracy. The average error in the range of 50m is 0.54m, and the average error in the range of 80m is 0.78m. Through experimental analysis and comparison, the results show that the method in this paper can achieve accurate and effective monocular vision ranging.