To detect capsule depression defects in the process of filling pharmaceutical granules with medicinal hollow capsules, we consider using stochastic gradient descent and deep learning to defect detection for the medicinal hollow capsule. Firstly, A background-weakening method based on image filtering is introduced for the surface defects of medicinal capsules under complex background. Then, combined with the acquisition and feature extraction of capsule images in the actual production process, we propose a YOLO-OL object detection method based on image contour extraction method. The proposed method is validated on an online detection platform under a complex background to verity their accuracy and practicability. Experimental results show that the proposed method is more accurate than the YOLOv3 and traditional edge extraction algorithms, and the detection method has good robustness.