In the closed-loop detection system of the LAMOST, the lens of the fiber view camera system must focus the end of the optical fiber to accurately acquire the fiber position. It is difficult to evaluate the fibers image with a very small proportion of the image with the traditional autofocus algorithm, whether it is recognition of the front illuminated by the fiber ceramic ferrules, or the light-emitting spot of the fibers in the black background, that is, the recognition of the back illumination of the fibers. In this paper, we propose an autofocus determination method for the LAMOST closed-loop control under front and back illumination conditions. Under the condition of front illumination, to greatly reduce the calculation time, the system first pre-recognizes the focusing target through the Faster R-CNN and then uses an optimized contrast algorithm to evaluate the image definition of the corrected focusing target ROI. Then, the algorithm is compared with Tenengrad gradient and Laplacian gradient calculations using the Sobel operator and Laplacian operator in OpenCV. The results show that this method takes only one-ninth of the time required by the other methods to obtain the same accuracy. Under the condition of back illumination, we use the average number and average brightness of spot pixels as the evaluation basis of image sharpness. This method can complete the evaluation of image sharpness in the process of spot recognition and provide initial data for the subsequent detection for closed-loop control. The focus accuracy of the camera is of great significance for thousand-fiber metrology, which will have an important influence on the accuracy of astronomical observations. These focusing methods not only play an important role in the closed-loop control of the LAMOST but also apply to the focusing of closed-loop detection systems of other multi-target optical fiber spectral astronomical telescopes.