On pig farms, the highest mortality rate is observed among nursing piglets. To reduce this mortality rate, farmers need to carefully observe the piglets to prevent accidents such as being crushed and to maintain a proper body temperature. However, observing a large number of pigs individually can be challenging for farmers. Therefore, our aim was to detect the behavior of piglets and sows in real-time using deep learning models, such as YOLOv4-CSP and YOLOv7-E6E, that allow for real-time object detection. YOLOv4-CSP reduces computational cost by partitioning feature maps and utilizing Cross-stage Hierarchy to remove redundant gradient calculation. YOLOv7-E6E analyzes and controls gradient paths such that the weights of each layer learn diverse features. We detected standing, sitting, and lying behaviors in sows and lactating and starving behaviors in piglets, which indicate nursing behavior and movement to colder areas away from the group. We optimized the model parameters for the best object detection and improved reliability by acquiring data through experts. We conducted object detection for the five different behaviors. The YOLOv4-CSP model achieved an accuracy of 0.63 and mAP of 0.662, whereas the YOLOv7-E6E model showed an accuracy of 0.65 and mAP of 0.637. Therefore, based on mAP, which includes both class and localization performance, YOLOv4-CSP showed the superior performance. Such research is anticipated to be effectively utilized for the behavioral analysis of fattening pigs and in preventing piglet crushing in the future.