Quality management in the candy industry is a vital part of food quality management. Defective candies significantly affect subsequent packaging and consumption, impacting the efficiency of candy manufacturers and the consumer experience. However, challenges exist in candy defect detection on food production lines due to the small size of the targets and defects, as well as the difficulty of batch sampling defects from automated production lines. A high-precision candy defect detection method based on deep learning is proposed in this paper. Initially, pseudo-defective candy images are generated based on Style Generative Adversarial Network-v2 (StyleGAN2), thereby enhancing the authenticity of these synthetic defect images. Following the separation of the background based on the color characteristics of the defective candies on the conveyor belt, a GAN is utilized for negative sample data enhancement. This effectively reduces the impact of data imbalance between complete and defective candies on the model's detection performance. Secondly, considering the challenges brought by the small size and random shape of candy defects to target detection, the efficient target detection method YOLOv7 is improved. The Spatial Pyramid Pooling Fast Cross Stage Partial Connection (SPPFCSPC) module, the C3C2 module, and the global attention mechanism are introduced to enhance feature extraction precision. The improved model achieves a 3.0% increase in recognition accuracy and a 3.7% increase in recall rate while supporting real-time recognition scenery. This method not only enhances the efficiency of food quality management but also promotes the application of computer vision and deep learning in industrial production.