The present invention provides an automatic focusing method, an electronic device, and a readable storage medium. The automatic focusing method comprises: using a target detection model to detect a target object in an
image frame acquired at an initial
focal length by an
optical imaging device to be focused, so as to acquire a target
object detection result; on the basis of the target
object detection result, determining whether a target object is present in the
image frame; if yes, determining an actual
object distance on the basis of the target
object detection result and the initial
focal length, and on the basis of the actual
object distance and a mapping relationship between the
object distance and an optimal imaging
focal length, determining the optimal imaging focal length; and if not, using a preset
search algorithm to search for a focal length until an image having the highest definition value is found, and using a focal length corresponding to the image having the highest definition value as the optimal imaging focal length. The present invention primarily employs a
deep learning-based automatic focusing method, supplemented by an image definition evaluation method. Compared with traditional passive focusing methods, the present invention greatly improves the focusing efficiency. Compared with traditional active focusing methods, the present invention eliminates a need for adding an additional optical
ranging component, ensuring that the imaging device has a simple structure and a low cost.