To address the challenge of automatic recognition of electronic components on an assembly line, an improved YOLOv5 was used to implement instance segmentation of four categories of electronic components. Firstly, multi-channel histogram equalization was used for image preprocessing. Then, the YOLOv5 was improved: Segmentation head was added; Sequeeze-and-excitation net(SE-Net) channel attention module was embedded to enhance the feature extraction capability and to compress the useless information without increasing the model complexity; GhostNet was used to make the model lightweight; and BiFPN was used to enhance model feature fusion capability. Finally, experimental results showed that the mAP of the proposed method could reach 96.7% and the detection time of a single image was 45.5 ms. The results prove that proposed method has superior performance than that based on mask region-based conventional neural network(Mask RCNN) and initial YOLOv5, and has practical significance for automatic detection of electronic components.