Accurate detection and localization of chicken targets in complex environments are the foundation of intelligent free-range chicken farming. However, commercial free-range chicken farming poses several challenges to detection methods, including complex and variable backgrounds, multi-scale targets, aggregated occlusion and changes in the posture of chicken. While there has been significant research on object detection algorithms based on convolutional neural networks, these algorithms have limited effectiveness in detecting small targets and generalizing to new scenes. Therefore, this study proposes a highly accurate and strongly generalizable chicken object detection model called Efficient Multi-scale Chicken DETR (EMSC-DETR), which is based on an improved Real-Time DEtection TRansformer (RT-DETR). To address the issue of easily losing small target features, a new efficient transformer module called space-to-depth transformer module (SDTM) is introduced. SDTM includes space-to-depth (SPD) and BiFormer modules, which facilitate deep interaction between local and global features, and significantly improve the computational efficiency of the transformer. Additionally, to overcome the detection and differentiation difficulties caused by chicken aggregation occlusion, the transformer encoder of EMSC-DETR incorporates a contextual transformer (CoT). CoT utilizes rich contextual information among the nearest neighbors to enhance the feature representation of high-level features, enabling more accurate localization of overlapped chicken targets. To evaluate the proposed model in complex environments, 14 scene data were collected from two fields and divided into training, validation, testing sets and three independent testing sets with unknown scenes. Experimental results show that the proposed EMSC-DETR obtains the best detection performance in the testing set, with an AP50 of 98.6%, surpassing other state-of-the-art models of similar size. In the three independent testing sets, the proposed model shows improvements of 8.2%, 9.2% and 28.3% in the AP50 compared to the original RT-DETR, respectively. Moreover, the model exhibits superior generalization ability for unknown scenes compared to other state-of-the-art models. Robustness and fine-tuning experiments further confirm the reliability and application potential of EMSC-DETR in detecting free-range chickens, which is crucial for complex commercial farming. This study is expected to provide references and new ideas for precision livestock farming.