This protocol uses pose estimation by Python to track key body points in paired Drosophila, facilitating the automated quantification of subtle social behaviors, especially in social interaction, as well as heatmaps and locomotor trajectories.
Method Article
2020687117@gzhmu.edu.cn
chenshengq66@163.com
Corresponding Authors: Xingyuan Shi <2020687117@gzhmu.edu.cn>, Shengqiang Chen <chenshengq66@163.com>
* These authors contributed equally
This protocol uses pose estimation by Python to track key body points in paired Drosophila, facilitating the automated quantification of subtle social behaviors, especially in social interaction, as well as heatmaps and locomotor trajectories.
To develop an open-source analytical tool that overcomes the limitations of traditional centroid-based tracking methods, we present a protocol to accurately analyze social interactions in freely behaving Drosophila pairs. Specifically, this protocol is inspired by the high-resolution network (HRNet) framework to simultaneously track five anatomical key points—head, thorax, abdomen, and left and right wing tips —aiming to generate high-resolution coordinate data for subsequent quantitative analysis of social behaviors. This protocol provides a complete workflow based on Python and a graphical user interface (GUI) we designed, including dataset construction, model training, and automated coordinate extraction, along with integrated modules for generating spatial occupancy heatmaps, locomotor trajectories, and social interaction ratios in flies. To validate this framework, it was tested by using two widely used strains, w1118 and Canton-S (CS). Our results demonstrated the stability of key-point detection and revealed genotype-specific differences in spatial utilization and locomotive structure. This methodology provides a scalable foundation for quantitative ethology and automated analysis of social interactions in Drosophila, while reducing manual annotation effort and improving reproducibility.
Drosophila melanogaster is a widely used model organism with powerful genetic tools and well‑mapped neural circuits that enable mechanistic dissection of behavior1. It has a wide range of social behaviors, including courtship and aggression, and a strong learning ability2,3,4,5,6,7,8. Each is composed of distinct motor elements controlled by a compact but extremely well-organized nerve system. Importantly, precise measurement of body posture is essential for understanding the neurological foundations of action selection because many of these behaviors contain fine‑grained postural motifs that encode contextual and internal‑state information9,10.
Despite the need for single-fly resolution to capture individual differences, many high‑throughput behavioral assays in Drosophila still rely on group‑level indices11. High-throughput behavioral assays include T‑maze tests for social preference12,13, social spacing assays that quantify inter‑fly distance, or aggregation/avoidance paradigms under sensory stimulation14 can efficiently capture population trends. However, they inherently involve averaging across individuals, thereby obscuring heterogeneity in action sequences and the temporal structure of social exchanges. Over the years, attempts to move toward per-fly identification have relied on functionally constrained methods, such as rule-based event detection using kinematic thresholds, background subtraction with centroid tracking, and manual video annotation15,16,17,18. As a result, discrete behaviors such as lunging, tapping, or wing threats are often ignored or mislabeled, necessitating significant manual correction. Such constraints have restricted mechanistic studies that require precise temporal alignment between neural activity and individual actions.
A revolutionary solution is now available thanks to recent developments in computer vision and machine learning-based pose estimation. Open-source frameworks like DeepLabCut19 and SLEAP20 enable marker-less body-part detection, scalable classification of discrete actions, and preservation of individual identity even during close interactions. These features overcome the drawbacks of previous group-based and rule-driven approaches and enable repeatable quantification of complex social behaviors.
Individual interactions in Drosophila are characterized by unique motor features that indicate the progression of social interactions. Aggression, for instance, includes typical behaviors like lunging, wing threats, and grappling, all of which represent growing conflict stages21,22. Courtship is similarly defined by orientation, unilateral wing extension with song production, tapping, and attempted copulation, which come together to form the temporal sequence of mating interactions23. Capturing these motifs depends on accurate recognition of body posture and joint movements24,25. In practice, sampling requires high spatial and temporal resolution over extended recording periods to ensure that subtle motor patterns can be precisely detected and aligned with neural activity25,26.
Here, we present an open‑source, vision‑based framework for automated identification of five anatomical key points—head, thorax, abdomen, left and right wing tips27. The system comprises a complete workflow for dataset construction, model training, and coordinate extraction, and outputs high‑resolution time‑series data suitable for customizable behavioral classification. By automating manual annotation processes and standardizing analytical workflows, our method enables fine-scale quantitative analysis of social interaction behaviors, increasing efficiency and reproducibility while significantly reducing the time and labor required for analysis.
To validate the reliability of the system, we applied it to two commonly used laboratory strains, w1118 and Canton-S (CS), and observed stable key-point detection across frames, as well as genotype-specific differences in spatial occupancy, trajectory features, and social interaction indicators based on head distance and orientation angle. Researchers can assess the suitability of this protocol based on the required spatiotemporal resolution (≥30 fps, ≤0.5 mm/pixel) and video recording conditions (illumination and contrast). The pixel size can be directly determined from the camera settings. A fruit fly with a measured body length can be placed inside the imaging field of view for calibration. This protocol is particularly suitable for the following scenarios: needing to simultaneously track two freely interacting Drosophila and distinguish their individual identities; focusing on fine postures (e.g., head angle and distance) rather than only centroid positions; recording duration exceeding 10 min where manual annotation is not feasible; requiring millisecond-level temporal alignment between behavioral events and neural activity (e.g., calcium imaging or electrophysiology) in subsequent analyses.
1. Fly preparation
2. Arena construction

Figure 1: Schematic of the behavioral arena and imaging setup. (A) The construction of a custom observation chamber is depicted in the diagram. The chamber consists of three primary layers: a 3 mm transparent acrylic base plate; a 3 mm polyethylene plate with four 35 mm diameter circular cutouts serving as individual chamber wells; and a second acrylic plate that functions as a removable cover. (B) A 90° adapter clamp is used to install the camera vertically on a 40 cm optic pole. For top-down imaging, the camera is positioned directly above the multi-layered observation arena with its optical axis perpendicular to the chamber surface. Please click here to view a larger version of this figure.
3. Imaging setup
4. Recording procedure
5. Define the social activity panel
6. Dataset preparation
7. Model training
8. Run the detection program
9. Behavioral Quantification
Figure 1 illustrates the arena geometry, illumination configuration, and camera placement used for recording natural social interactions in Drosophila. The setup ensures uniform lighting and stable imaging conditions suitable for pose estimation and coordinate extraction.
Automated pose estimation was applied to all recorded videos to extract two-dimensional coordinates for predefined anatomical key-points of each fly. The model consistently identified head, thorax, abdomen, and wing joints across frames, even during close-range interactions. Figure 2 shows a representative frame with overlaid key-points, demonstrating accurate multi-fly tracking and stable key-point detection throughout the recording. The reliability of this step ensures that the extracted coordinate time series can serve as a robust foundation for downstream quantitative analyses. Figure 3 illustrates the analytical workflow.
Using the extracted coordinates and the proportion of frames for each coordinate, we generated spatial occupancy heatmaps to visualize the cumulative distribution of fly positions within the arena. These heatmaps summarize the probability density of fly locations over time and highlight regions of preferential occupancy. Figure 4 and Figure 5 present representative heatmaps for w1118 and Canton-S (CS) flies. Differences in spatial usage patterns between the central and peripheral zones in the two genotypes are readily observable, reflecting distinct tendencies in arena exploration and zone preference.
In addition to occupancy patterns, we reconstructed locomotor trajectories from the coordinate time series to capture fine-scale movement dynamics. The trajectories were generated by connecting the two-dimensional coordinates of flies from each successive frame without any smoothing. The trajectories shown in Figure 4 and Figure 5 illustrate individual movement paths for w1118 and CS flies, revealing genotype-specific differences in movement range, path complexity, and transitions between high-occupancy zones. Together, the key-point detection results, heatmaps, and trajectory reconstructions demonstrate that the combined pose estimation and coordinate-based analysis pipeline effectively captures and differentiates the behavioral structure of freely interacting Drosophila across genotypes.
We also defined social interaction in each frame based on the two-dimensional coordinates of five anatomical key-points. A pair was considered socially engaged when both criteria were met simultaneously: a head-to-head distance of more than 0.5 mm but less than 20 mm, and an orientation difference of more than 30°28,29,30,31. Accordingly, we measured the proportion of time spent in social interaction for each pair (six pairs per genotype). Our method can reliably measure social behavior in flies, as shown in Figure 6. The mean social ratios for W1118 and CS flies were 0.270 ± 0.001 and 0.252 ± 0.005, respectively, throughout six independent replicate experiments, suggesting strong reproducibility across different experimental groups. It is also consistent with the result that there is no significant difference in the social ratio between the two wild-type fly strains.

Figure 2: Representative key-point detection results. Automatic pose estimation identified all predefined anatomical key-points for each fly across frames. Overlaid markers illustrate accurate multi-fly tracking during natural social interactions. Please click here to view a larger version of this figure.

Figure 3: Workflow of analysis. The main workflow comprises preprocessing, data preparation, model training, and validation, and ultimately yields two-dimensional coordinates of Drosophila, followed by GUI-based analysis. Please click here to view a larger version of this figure.

Figure 4: Representative activity heatmap and locomotor trajectories of w1118 flies. Please click here to view a larger version of this figure.
The heatmap shows cumulative spatial occupancy within the arena over 10 min, revealing preferred activity zones for each w1118 fly. Overlaid trajectories illustrate individual movement paths reconstructed from coordinate time series.

Figure 5: Representative activity heatmap and locomotor trajectories of Canton-S (CS) flies. Please click here to view a larger version of this figure.
The heatmap displays the spatial occupancy distribution of each CS fly during free interaction for 10 min. Reconstructed trajectories highlight movement range and path structure, enabling comparison with w1118 behavioral patterns.

Figure 6: Social ratio in wild-type flies. Quantitation of social behavior after 10 min in the chamber. For six pairings of each genotype in a small arena, to determine the percentage of social contact. Social ratio values for W1118 and CS flies were 0.270 ± 0.001 and 0.252 ± 0.005, respectively. Data are presented as mean ± SD. Statistical comparisons were performed using an unpaired two-tailed Student’s t-test. Please click here to view a larger version of this figure.
Supplementary File 1. All coding files required to execute the protocol.Please click here to download this file.
Supplementary File 2. Standalone GUI applicationPlease click here to download this file.
Supplementary File 3. Python requirementsPlease click here to download this file.
Our study shows that machine-learning-based pose estimation provides a scalable, reliable method for measuring fine-grained social interactions in Drosophila. It overcomes the limitations of centroid-based approaches15,16,17,18 by tracking five anatomical key-points in freely interacting pairs, allowing the acquisition of angular information provided by the body axis and head-to-head distance inferred from head points. Our technique can reconstruct spatial occupancy patterns and motion trajectories by tracking the centroid coordinates of flies in each video frame to get movement routes and using five-point landmarks to estimate head orientation. The stable key-point detection observed across both w1118 and Canton‑S strains indicates that the pipeline is robust to natural variation in movement dynamics, providing a reproducible foundation for downstream behavioral classification. These capabilities are essential for dissecting the fine‑grained motor elements that encode internal states and contextual information in social behaviors8,9.
Several methodological factors were found to be essential for guaranteeing consistent results. The lighting system's heat and illumination intensity significantly affected behavioral expression, consistent with earlier findings that environmental factors affect flies' social and locomotor activity32,33. Imaging parameters, including focal distance, contrast, and gamma, which are adjusted empirically in our experiments, also affected pose‑estimation accuracy, aligning with previous findings that high‑speed behavioral recordings require careful optical calibration to capture rapid movements34,35. Behavioral variability was also influenced by handling procedures: CO₂ anesthesia resulted in quantifiable changes in social interaction and locomotion, consistent with previous publications showing anesthesia-induced behavioral changes36,37,38. This step is particularly critical because when anesthesia is insufficient, flies may regain consciousness and escape after being placed in the chamber; conversely, with adequate anesthesia, flies must be given enough time to recover, as inadequate acclimation reduces their motivation to move later, which will severely affect subsequent analysis.
Even with the pipeline's strength, several limitations remain. Pose‑estimation accuracy is sensitive to annotation quality, so that even small labeling errors can propagate into distortions in reconstructed trajectories. This problem has previously been observed in other pose-estimation frameworks20,34. Domain shifts caused by changes in lighting, background texture, or fly strain may reduce model performance, necessitating periodic retraining or dataset expansion. Additionally, experiments in which more than 5% frames of key points fail to be detected should be excluded or incorporated into iterative model refinement. Strict version control (as outlined in the Supplementary File 3) is also necessary for computational reproducibility, as inconsistent outputs can result from differences in program dependencies.
Future improvements may include automated calibration procedures, domain‑adaptation strategies to enhance model generalization across experimental conditions, and integration of self‑supervised learning to reduce the need for manual annotation. Reproducibility between labs would be further improved by standardizing the reporting of imaging parameters, ambient factors, and model-training settings. When taken as a whole, these factors highlight the advantages and drawbacks of machine learning-based behavioral quantification and offer a roadmap for further improving posture estimation approaches in Drosophila social behavior studies.
The authors declare no financial conflicts of interest.
We acknowledge Dr. Jingyuan Zhang in the Graduate School at Guangzhou Medical University for providing and maintaining the imaging setup.
Funding: Guangzhou Major Medical Disciplines Project (2025-2027)
| Name | Company | Catalog Number | Comments |
|---|---|---|---|
| Agar | BioFroxx | 8211GR500 | |
| camera(MV-CU013-A0UC) | Hikvision | MV-CU013-A0UC | |
| Carbon Dioxide | Messer | 124-38-9 | |
| D(+)-Glucose anhydrous | BioFroxx | 1179GR500 | |
| Drosophila Narrow Vials (Falcon tube) | Biolgix | 51-0500 | |
| Ethanol | Chron Chemicals | 64-17-5 | |
| LED panel | / | / | custom-made |
| Methylparaben | Sigma | 99-76-3 | |
| polyethylene plate | / | / | custom-made |
| Python | / | version 3.6 | |
| Sucrose | DaMao | 57-50-1 | |
| transparent acrylic plates | / | / | custom-made |
| Yeast | Angel | 100004237033 |
Request permission to reuse the text or figures of this JoVE article
Request Permission