In modern industry, deep learning algorithms have increasingly replaced manual sorting for detecting workpieces and enabling sorting by robots or robotic arms. However, the detection of stacked workpieces presents a challenge to automated detection. In this paper, the YOLOv5-RES algorithm is proposed to solve the problem of detecting stacked workpieces based on the YOLOv5 algorithm. The receptive field block (RFB) replaces the original fast pyramid pooling layer (SPPF) of YOLOv5 to enable the network to better acquire feature information of stacked workpieces. The EIOU loss is introduced to improve the network's accuracy in regression, and a flexible prediction box selection algorithm (Soft-NMS) is utilized to reduce the possibility of incorrectly removing stacked workpiece targets. The effectiveness of YOLOv5-RES is verified on a homemade workpiece dataset. On the test set, the precision for all classes is 93.3%, the mAP@.5 is 91.4%, and the detection speed is 65.4 FPS (Frames Per Second), which meets the real-time requirement. And the detection results show that YOLOv5-RES greatly avoids the problems of incorrect detection and missed detection of YOLOv5. Moreover, YOLOv5-RES achieves the optimal effect in both the comparison experiment and the ablation experiment, indicating its effectiveness and potential for application.