The classification of silkworm cocoons is essential prior to silk reeling and serves as a key step in improving the quality of raw silk. At present, cocoon classification mainly relies on manual sorting, which is labor-intensive and inefficient. In this paper, a cocoon detection algorithm S-YOLOv8_c based on the cooperation of MobileSAM and YOLOv8 for the mountage cocoons was proposed. The MobileSAM with a designed area thresholding algorithm was used for the semantic segmentation of mountage cocoon images, which could mitigate the effect of complex backgrounds and maximize the discriminability of cocoon features. Subsequently, the BiFPN was added to the neck of YOLOv8 to improve the multiscale feature fusion capability. The loss function was replaced with the WIoU, and a dynamic non-monotonic focusing mechanism was introduced to improve the generalization ability. In addition, the GAM was incorporated into the head to focus on detailed cocoon information. Finally, the S-YOLOv8_c achieved a good detection accuracy on the test set, with a mAP of 95.8%. Furthermore, to experimentally validate the sorting ability, we deployed the proposed model onto the self-developed Cartesian coordinate automatic cocoon harvester, which indicated that it would effectively meet the requirements of accurate and efficient cocoon sorting.