In order to enhance the storage efficiency of drug traceability code information on the blockchain and improve the extraction capability of drug traceability codes on drug packaging, a detection algorithm based on an enhanced version of YOLOv5 is proposed for the drug production and transportation scenario. The proposed algorithm introduces the SPD-Conv module into the backbone network, thereby enhancing the network’s ability to extract detailed feature information. Additionally, the CA attention mechanism is incorporated into the Neck of the network, providing the network with superior feature fusion capabilities. Furthermore, the activation function in the network is replaced with the LeakyReLU activation function, reducing computational requirements during training and inference. This replacement improves the model’s test accuracy and detection speed. Experimental evaluation, conducted on a self-built dataset, demonstrates the effectiveness of the improved YOLOv5 model. The results indicate a compression of model parameters from 6.14M to 2.71M, an increase in mAP@.5 from 82.4% to 93.7%, and a boost in detection speed from 32FPS to 51FPS. These findings establish the superior performance of the enhanced model compared to the original version.