Water quality detection method based on deep learning and zebra fish behaviors

By using deep learning and multi-target tracking algorithms to perform real-time tracking of zebrafish and water quality detection, the problem of insufficient target localization error and robustness in complex scenarios in existing zebrafish behavior analysis technologies has been solved, and high-precision water quality detection has been achieved.

CN121884432APending Publication Date: 2026-04-17XIAN TECH UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
XIAN TECH UNIV
Filing Date
2025-11-25
Publication Date
2026-04-17

AI Technical Summary

Technical Problem

Existing zebrafish behavior analysis technologies suffer from large target localization errors and insufficient robustness in complex scenarios, especially when multiple fish overlap and their postures change, making accurate identification and tracking difficult and affecting the accuracy of water quality testing.

Method used

We employ deep learning methods, using the YOLOV8 algorithm and multi-target tracking algorithm to track zebrafish in real time. We combine Labelimg for pixel-level bounding box annotation, and use Focal Loss, CIoU Loss and BCE Loss for model training. We combine optimal path and optimal target selection algorithms for continuous target tracking and identity preservation, and analyze the behavior and trajectory of zebrafish to determine water quality.

Benefits of technology

It enables precise target localization and behavioral analysis of multiple zebrafish in complex scenarios, improving the accuracy and stability of water quality testing. It can record and visualize zebrafish trajectory and behavioral data in real time, supporting the judgment of water quality conditions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121884432A_ABST
    Figure CN121884432A_ABST
Patent Text Reader

Abstract

The invention discloses a water quality detection method based on deep learning and zebra fish behaviors. The method comprises the following steps: acquiring a plurality of zebra fish continuous videos; adopting a pre-training enabling frame-by-frame interception technology to disassemble the plurality of zebra fish continuous videos in a time dimension direction to obtain a plurality of single-frame static images; performing pixel-level frame labeling on the obtained single-frame static image by adopting Labelimg, and recording spatial parameters and category information of the zebra fish in an image coordinate system; inputting the plurality of single-frame static images marked by the Label into a YoV8 algorithm for iterative training, and verifying generalization performance; performing real-time tracking on the plurality of zebra fish targets through the combination of the trained YoV8 algorithm and a multi-target tracking algorithm, and collecting trajectory data and behavior data of the plurality of zebra fish targets; and analyzing and judging the water quality condition according to the trajectory data and the behavior data of the plurality of zebra fish targets, and visually displaying the trajectory data, the behavior data and the water quality condition of the plurality of zebra fish targets.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of image processing technology, and in particular to a water quality detection method based on deep learning and zebrafish behavior. Background Technology

[0002] In the current field of zebrafish behavioral research and application, behavioral analysis techniques are mainly divided into two core categories: traditional computer vision methods and early deep learning methods. While these two approaches differ in their technical paths and implementation logic, they are also related, jointly supporting the recognition and analysis of basic zebrafish behavior. Early deep learning methods, on the other hand, have made key optimizations to the framework of traditional methods, with the core improvement being the upgrade of the target detection module. This method retains the core process of "feature extraction - behavior classification" in traditional techniques, but abandons the step of relying on manual experience to set thresholds for target segmentation—a step that is prone to inaccurate segmentation and missed target detection in scenarios such as overlapping zebrafish (e.g., multiple zebrafish clustering, occlusion), drastic changes in posture (e.g., changing from horizontal to vertical when rolling over), or motion blur caused by rapid swimming. Early deep learning methods, by incorporating mainstream object detection networks such as YOLO, You Only LookOnce, Faster R-CNN, and Region-based Convolutional Neural Networks, leveraged the powerful automatic feature learning capabilities of deep learning models to accurately locate the position and outline of each zebrafish in complex scenes. Even when multiple fish overlapped or their poses dynamically changed, they effectively reduced localization errors. Building upon this foundation, subsequent feature extraction and classification modules significantly improved the accuracy and stability of zebrafish target tracking in complex experimental scenarios, providing a more reliable basis for subsequent behavior analysis. Although these two existing technologies have met the needs of basic zebrafish behavior analysis and achieved a key leap from manual observation to automated recognition, significant technical shortcomings still exist in practical research and application scenarios. Among these, the insufficient robustness of traditional computer vision methods is particularly prominent. Summary of the Invention

[0003] Purpose of the invention: In order to overcome the shortcomings of the existing technology, the present invention provides a water quality detection method based on deep learning and zebrafish behavior. The method uses deep learning analysis and multi-target tracking algorithm to record the trajectory and behavior of multiple zebrafish in real time, and detects the water quality in the water where the zebrafish are located based on the trajectory and behavior of multiple zebrafish.

[0004] Technical Solution: To achieve the above objectives, this invention provides a water quality detection method based on deep learning and zebrafish behavior. The method involves acquiring several continuous zebrafish videos; using a pre-trained, frame-by-frame extraction technique to decompose the continuous zebrafish videos along the time dimension, resulting in several single-frame static images; using Labelimg to annotate the obtained single-frame static images with pixel-level bounding boxes, recording the spatial parameters and category information of the zebrafish in the image coordinate system; inputting the Labelimg-annotated single-frame static images into a YOLOV8 algorithm for iterative training and verification of generalization performance; combining the trained YOLOV8 algorithm with a multi-target tracking algorithm to track multiple zebrafish targets in real time, and collecting trajectory and behavioral data of the multiple zebrafish targets; analyzing and judging the water quality based on the trajectory and behavioral data of the multiple zebrafish targets, and visually displaying the trajectory data, behavioral data, and water quality of the multiple zebrafish targets.

[0005] Furthermore, Labelimg is used to perform pixel-level bounding box annotation on the obtained single-frame static image. This includes setting a pixel coordinate system with the top left corner of the image as the origin (0,0), the X-axis pointing horizontally to the right, and the Y-axis pointing vertically downwards; using a rectangular bounding box to select the zebrafish from the top left edge pixel to the bottom right edge pixel to obtain the initial bounding box of the zebrafish; fine-tuning the initial bounding box of the zebrafish to ensure that the initial bounding box covers all zebrafish pixels to obtain the zebrafish bounding box image; capturing the pixel coordinates of the top left and bottom right corner pixels of the zebrafish bounding box image in real time, and annotating the obtained pixel coordinates and category information on the zebrafish bounding box image to obtain the annotated single-frame static image; and saving the annotated single-frame static image in VOC XML format.

[0006] Furthermore, several single-frame static images annotated with Labelimg are input into the YOLOv8 algorithm for iterative training and to verify the generalization performance; this includes the following steps:

[0007] Step 1: Convert the labeled single-frame static image from VOC XML format to TXT format and normalize it;

[0008] Step 2: Divide the labeled single-frame static images into training set, validation set and test set according to the proportion;

[0009] Step 3: Perform data augmentation operations on the images in the training set, validation set, and test set according to the specific scenario, scale all images to a uniform pixel size, and perform pixel value normalization to ensure uniform distribution of input data;

[0010] Step 4: Using the training set, validation set, and test set obtained in Step 3, the YOLOV8 algorithm is iteratively trained, its generalization performance is verified, and its final performance is tested.

[0011] Furthermore, the training set is input into the iterative training of the YOLOv8 algorithm, including weighted summation of Focal Loss (classification loss), CIoU Loss (localization loss), and BCE Loss (target confidence loss) to obtain a composite loss function to guide parameter optimization; the calculation process of the composite loss function is as follows:

[0012]

[0013] In the formula, Loss F Focal Loss is the classification loss, Loss C For CIoU Loss, locate the loss, Loss B is the BCELoss target confidence loss; W1, W2, and W3 are the weights of Focal Loss (classification loss), CIoU Loss (localization loss), and BCELoss target confidence loss, respectively.

[0014] Furthermore, the multi-target tracking algorithm employs both the optimal path algorithm and the optimal target selection algorithm to continuously track and maintain the identity of multiple zebrafish targets. The optimal path algorithm selects the best motion path between frames for each zebrafish target. The optimal target selection algorithm selects detection boxes that match the real zebrafish targets based on detection confidence and appearance feature similarity.

[0015] Furthermore, the zebrafish behavior includes their swimming speed, undulation frequency, and loose group size; corresponding normal threshold ranges are set for each of these parameters; the water quality is assessed based on the trajectory and behavioral data of multiple zebrafish targets, including the following steps:

[0016] Step 1-1: Based on the trajectory and behavioral data of multiple zebrafish targets, determine whether the zebrafish exhibit any abnormal behavior other than changes in swimming speed, oscillation frequency, and loose group size.

[0017] Steps 1-2: When zebrafish exhibit abnormal behavior, it is determined that there is abnormal pollution in the water where the zebrafish are located.

[0018] Steps 1-3: If no abnormal behavior is observed in the zebrafish, determine whether the zebrafish's swimming speed, undulation frequency, and loose group size exceed the normal threshold range.

[0019] Steps 1-4: When any one of the following zebrafish behaviors—swimming speed, oscillation frequency, and loose group size—exceeds the set normal threshold range, the water in which the zebrafish are located is considered to have ordinary pollution; when the zebrafish's swimming speed, oscillation frequency, and loose group size are all within the normal threshold range, the water in which the zebrafish are located is not considered to have pollution.

[0020] Furthermore, in steps 1-2, when zebrafish exhibit abnormal behavior, the source of pollution in the water is determined based on the type of abnormal behavior; when zebrafish exhibit intermittent twitching and sideways swimming, heavy metal pollution is determined to be present in the water where the zebrafish are located; when zebrafish exhibit frequent surfacing and swallowing, and alternating disordered swimming and stagnation, organic pollution is determined to be present in the water where the zebrafish are located; when zebrafish exhibit rapid swimming in the initial stage and stillness at the bottom in the later stage, ammonia nitrogen pollution exceeding the standard is determined to be present in the water where the zebrafish are located; when zebrafish exhibit jumping and spinning during the excited period and paralysis and tilting during the inhibited period, pesticide pollution is determined to be present in the water where the zebrafish are located.

[0021] Beneficial effects: The water quality detection method based on deep learning and zebrafish behavior of this invention, by labeling zebrafish targets and inputting them into the YOLOv8 algorithm for training, enables the YOLOv8 algorithm to distinguish multiple individual zebrafish to avoid behavioral confusion; the optimal target selection algorithm solves the target loss problem caused by zebrafish occlusion, the optimal path algorithm solves the trajectory selection of previously lost targets after target loss, and the physical parameters of the analyzed targets are better increased and adjusted by tracking and analyzing independent files. Attached Figure Description

[0022] Figure 1 This is a schematic diagram of a water quality detection method based on deep learning and zebrafish behavior. Detailed Implementation

[0023] The invention will now be further described with reference to the accompanying drawings.

[0024] like Figure 1As shown, a water quality detection method based on deep learning and zebrafish behavior is proposed. The method involves acquiring several continuous zebrafish videos; using a pre-trained, frame-by-frame extraction technique, the videos are decomposed along the time dimension to obtain several single-frame static images; Labelimg is used to annotate the obtained single-frame static images with pixel-level bounding boxes, recording the spatial parameters and category information of the zebrafish in the image coordinate system; the Labelimg-annotated single-frame static images are input into a YOLOV8 algorithm for 200 iterations of training, and the generalization performance is verified; the trained YOLOV8 algorithm is combined with a multi-target tracking algorithm to track multiple zebrafish targets in real time, collecting trajectory and behavioral data of the targets; the water quality is analyzed based on the trajectory and behavioral data of the zebrafish targets, and the trajectory, behavioral, and water quality data are visualized.

[0025] By employing a pre-trained, frame-by-frame extraction technique, several continuous zebrafish videos are decomposed along the time dimension to obtain several single-frame static images. By decomposing the continuous zebrafish videos, the dynamic features such as posture, displacement, and morphology of the zebrafish at each moment in its movement cycle can be completely captured.

[0026] Labelimg is used to annotate the obtained single-frame static image with pixel-level bounding boxes. This includes setting a pixel coordinate system with the top left corner of the image as the origin (0,0), the X-axis pointing horizontally to the right, and the Y-axis pointing vertically downward. A rectangular bounding box is used to select the zebrafish from the top left edge pixel to the bottom right edge pixel to obtain the initial bounding box of the zebrafish. The initial bounding box of the zebrafish is fine-tuned to ensure that the initial bounding box covers all zebrafish pixels, resulting in a zebrafish bounding box image. The pixel coordinates of the top left and bottom right corners of the zebrafish bounding box image are captured in real time, and the obtained pixel coordinates and category information are marked on the zebrafish bounding box image to obtain an annotated single-frame static image. The annotated single-frame static image is saved in VOC XML format or directly in YOLO TXT format, and the pixel coordinates are normalized to the range [0,1] using xnorm=Wcx, ynorm=Hcy, wnorm=Ww, and hnorm=Hh.

[0027] The pixel coordinates (xmin, ymin) of the top left corner and the pixel coordinates (xmax, ymax) of the bottom right corner of the zebrafish frame are captured in real time. The width and height of the zebrafish frame are then calculated, with width w = xmax − xmin and height h = ymax − ymin. The center point coordinates (cx, cy) are calculated using cx = 2xmin + xmax and cy = 2ymin + ymax. These pixel coordinates are all labeled on a single frame of static image of a zebrafish. The spatial location information of these labels is crucial for subsequent training. As a core supervisory signal, it helps the model distinguish zebrafish from the background to achieve accurate target localization. It guides the model to focus on the target region through ROI pooling to complete feature alignment and avoids the mis-extraction of background features. It can support the calculation of dynamic behavioral parameters such as swimming speed through the center point coordinates of consecutive frames. For example, swimming speed v=Δt(cx2-cx1)2+(cy2-cy1)2, where Δt is the frame interval, to achieve individual differentiation of multiple zebrafish and avoid behavioral confusion. It can also serve as a benchmark for calculating loss functions such as IoU to optimize the model's localization accuracy, ultimately providing reliable data support for zebrafish behavior analysis and water quality correlation judgment.

[0028] In deep learning model training, the accuracy of labeled data is a core factor determining model performance. Manual, coarse labeling suffers from defects such as bounding box offsets and ambiguous category labels, while unlabeled training relies on unsupervised learning paradigms, making it difficult for the model to accurately capture the semantic features of the target. High-precision labeled data generated by LabelImg provides clear supervision signals for the YOLO algorithm model. The one-to-one correspondence between the input image and the target location and category allows the model to precisely adjust network parameters during backpropagation based on joint optimization of classification losses such as cross-entropy loss and localization losses such as IoU loss. This improves the localization accuracy and classification accuracy of zebrafish targets, laying a solid data foundation for high-performance model training. Network parameters include convolutional layer weights and biases, and localization accuracy is the Intersection over Union (IoU) between the bounding box and the ground truth target.

[0029] As an iterative version of the YOLO series, the YoloV8 algorithm model incorporates improvements in network architecture, including Backbone feature extraction, Neck feature fusion, and Head detection. In terms of loss functions, it introduces Distribution Focal Loss to optimize classification and CIoU Loss to optimize localization. Data augmentation strategies such as Mosaic and MixUp enhance sample diversity, resulting in stronger feature extraction and object detection capabilities. During iterative training, the YOLOV8 algorithm model repeatedly traverses the training dataset, minimizing prediction errors through gradient descent. Through 200 parameter optimizations, the model parameters gradually converge to the optimal state that balances classification and localization errors. The test set is an independently partitioned dataset from the labeled data, not used in training, to validate the model's performance on unseen samples. Generalization performance refers to the model's adaptability to new scene samples. Validation on the validation set assesses the model's recognition stability under different swimming scenarios, such as variations in zebrafish movement speed or background complexity, reducing recognition errors caused by the offset between the training set and the actual scene sample distribution, ensuring the model maintains high accuracy in complex real-world scenarios.

[0030] Several single-frame static images labeled with Labellimg are input into the YOLOv8 algorithm for iterative training and to verify its generalization performance. Taking zebrafish detection as an example, the training of the YOLOv8 algorithm model is a multi-stage collaborative optimization process, which can be broken down into the following operations: First, the image data is refined, including the following steps:

[0031] Step 1: Convert the labeled single-frame static image from VOC XML format to the TXT format specific to the YoloV8 algorithm, and perform normalization processing to ensure the consistency of annotation for images of different sizes; the normalization processing is to compress the ratio of pixel coordinates to image width and height in the labeled single-frame static image to the range of [0, 1].

[0032] Step 2: Divide the labeled single-frame static images into training set, validation set and test set according to the ratio; divide the labeled single-frame static images into training set, validation set and test set according to the ratio of 7:2:1. The training set is used for the algorithm model to learn features, the validation set is used to evaluate the generalization performance in real time, and the test set is used for final performance verification.

[0033] Step 3: Perform data augmentation operations on the images in the training set, validation set, and test set according to the specific scenario, scale all images to a uniform pixel size, and perform pixel value normalization to ensure uniform distribution of input data;

[0034] For example, data augmentation operations are performed in special scenarios such as a high proportion of small targets, water reflection interference, or frequent fish overlap. These data augmentation operations include geometric transformation, pixel transformation, and mosaic enhancement. For geometric transformation, random scaling of 0.5-1.5x, rotation of -15° to 15°, horizontal or vertical flipping, and center cropping are used to adapt to different shooting angles, while preserving the main fish body during cropping. For pixel transformation, brightness or contrast adjustments of ±20% and Gaussian blur of σ=0.1-1.0 are used to simulate lighting changes and motion blur. For mosaic enhancement, four images containing zebrafish are randomly stitched together, forcing the model to learn the characteristics of multi-fish overlapping scenes. When a special scenario with a high proportion of small targets occurs, geometric transformation is used for adjustment; when a special scenario with water reflection interference occurs, pixel transformation is used for adjustment; and when a special scenario with frequent fish overlap occurs, mosaic enhancement is used, enabling the YoloV8 algorithm model to learn the characteristics of multi-fish overlapping scenes. To scale all images to a uniform pixel size, you can scale all images to 640×640 pixels or adjust them to 512×512 pixels depending on the size of the fish. To normalize the pixel values, simply divide 640×640 pixels by 255.

[0035] Step 4: Using the training set, validation set, and test set obtained in Step 3, the YOLOV8 algorithm is iteratively trained, its generalization performance is verified, and its final performance is tested.

[0036] In the initialization phase of the YoloV8 algorithm model, weights pre-trained on the YOLO dataset, such as yolov8n.pt, are loaded. Convergence is accelerated using transfer learning. Its core structure has a clear division of labor: the Backbone module uses an improved version of CSPDarknet-53, extracting multi-scale features layer by layer through five cross-stage partially connected CSP modules, outputting feature maps at three resolutions: 80×80 small zebrafish, 40×40 medium-sized zebrafish, and 20×20 large near-field zebrafish, balancing detail and semantic information; the Neck module uses a PAN-FPN architecture, enhancing the feature representation of small zebrafish targets through top-down and bottom-up bidirectional fusion. Top-down fusion transfers deep, high-semantic features to shallow layers, while bottom-up fusion transfers high-resolution features from shallow layers to deep layers; the Head module uses an anchor-free design, directly predicting the target's center point coordinates, width and height offsets, confidence level, and class probability, avoiding the limitations of traditional anchor boxes in adapting to the dynamic morphology of zebrafish.

[0037] The training set is input into the iterative training of the YOLOV8 algorithm. This includes using Focal Loss (classification loss), CIoULoss (localization loss), and BCE Loss (target confidence loss) to perform a weighted summation to obtain a composite loss function that guides parameter optimization and drives the algorithm model to learn. The calculation process of the composite loss function is shown below:

[0038]

[0039] In the formula, Loss F Focal Loss is the classification loss, Loss C For CIoU Loss, locate the loss, Loss B BCELoss target confidence loss; W1, W2, and W3 are the weights of Focal Loss (classification loss), CIoU Loss (localization loss), and BCELoss target confidence loss, respectively; among them, localization loss has the highest weight, accounting for more than 50%.

[0040] The Focal Loss classification loss reduces the weight of easily classified samples and focuses on difficult-to-classify samples, alleviating class imbalance. Easily classified samples include pure water backgrounds, while difficult-to-classify samples include blurred or partially occluded zebrafish. The CIoULoss localization loss comprehensively considers bounding box overlap (IoU), center point distance, and aspect ratio to ensure a high degree of consistency between the predicted and manually labeled bounding boxes. The BCE Loss target confidence loss optimizes the accuracy of determining whether a box contains a zebrafish. The optimizer used is AdamW, with an initial learning rate of 0.001, coupled with a cosine annealing strategy: the first three epochs are linearly warmed up to the target learning rate to stabilize the initial training phase, and subsequent epochs decay according to a cosine curve to 1e-5 to avoid parameter oscillations in the later stages.

[0041] When zebrafish swim rapidly, the rate of change in target position between adjacent frames is large. When zebrafish occlude each other, the target may be partially or completely covered by other targets. Traditional tracking algorithms are prone to target loss due to limitations in motion model assumptions and insufficient target re-identification capabilities in occluded scenarios, making it difficult to collect the trajectory and behavior of the same zebrafish. For example, simple tracking algorithms based on Kalman filtering assume linear target motion, which is difficult to adapt to rapid nonlinear motion. Therefore, a YOLOv8 algorithm trained on dynamic zebrafish images and a multi-target tracking algorithm are used to track multiple zebrafish in real time, thereby obtaining trajectory and behavior data of multiple zebrafish targets. The multi-target tracking algorithm uses an optimal path algorithm and an optimal target selection algorithm to continuously track and maintain the identity of multiple zebrafish targets. The optimal path algorithm selects the best motion path between frames for each zebrafish target. The optimal path algorithm, such as the Hungarian algorithm based on graph theory, can solve the optimal matching problem between detection results and targets. In the multi-target detection results, the most likely motion path between frames is selected for each zebrafish target. The optimal target selection algorithm selects detection boxes that match real zebrafish targets based on detection confidence and appearance feature similarity; appearance feature similarity includes indicators such as feature matching of target contour and texture.

[0042] The trained YOLOv8 algorithm and a multi-object tracking algorithm are used to track multiple zebrafish targets. When the trained YOLOv8 algorithm tracks multiple zebrafish targets and a zebrafish target is lost, it first uses an optimal target selection algorithm to select the detection box of the most suitable real zebrafish target based on confidence and appearance feature similarity. After selecting the most suitable real zebrafish target detection box, the trained YOLOv8 algorithm compares the image data before the zebrafish target was lost with the image of the selected real zebrafish target detection box. When the comparison results obtained by the trained YOLOV8 algorithm are inconsistent, the optimal target selection algorithm re-selects the detection boxes of the most suitable real zebrafish targets until the comparison results obtained by the trained YOLOV8 algorithm are consistent. Then, the optimal path algorithm is used to infer the swimming trajectory of the target zebrafish, complete the movement trajectory of the zebrafish target, and thus realize continuous target tracking and identity maintenance for multiple zebrafish targets. Finally, the trajectory data and behavioral data of multiple zebrafish targets are obtained, where behavioral data includes images and continuous video for behavioral data analysis. The YOLOV8 algorithm continuously identifies the tracked target, while the optimal target selection algorithm and optimal path algorithm of the multi-target tracking algorithm track the identified targets and correct for lost targets.

[0043] Simultaneously, relevant thresholds can be adjusted, such as the matching threshold controlling the similarity judgment standard between the detection box and the target, and the confidence threshold filtering low-quality detection results, to adapt to scenarios with different motion intensities and occlusion levels, greatly improving the accuracy and robustness of tracking, enabling the system to stably achieve continuous target tracking and identity maintenance even in complex scenarios where zebrafish swim quickly and occlude with each other.

[0044] The trained YOLOv8 algorithm and a multi-target tracking algorithm are used to track multiple zebrafish targets. If a zebrafish target is lost, an optimal target selection algorithm is employed. Based on a set matching threshold, a circle is drawn with the location of the lost zebrafish target as the center, and the zebrafish within the circle are selected to find the most suitable target. Then, the trained YOLOv8 algorithm is used to compare the selected zebrafish target with the target before it was lost to determine if they match. If they do not match, the optimal target selection algorithm is used again to select the most suitable zebrafish target from the circled zebrafish. The trained YOLOV8 algorithm compares the selected zebrafish targets with the zebrafish targets before they were lost, and determines whether they are consistent based on a confidence threshold. This process is repeated until the trained YOLOV8 algorithm determines that the selected zebrafish targets are consistent with the zebrafish targets before they were lost. When it is determined that the selected zebrafish targets are consistent with the zebrafish targets before they were lost, the optimal path algorithm is used to calculate the swimming trajectory of the zebrafish targets, supplementing the swimming trajectory of the zebrafish targets from the time they were lost to the time they were found. Finally, the trained YOLOV8 algorithm and a multi-target tracking algorithm are used together to continue tracking multiple zebrafish targets.

[0045] For example, in the YOLOv8 algorithm model for detecting zebrafish, the zebrafish's movement trajectory is first represented as a trajectory node. The center point coordinates (xt, yt) of each frame's static image of the zebrafish target are extracted, where t is the frame index. The center point coordinates of each frame's static image are used as nodes. The Euclidean distance between nodes in adjacent frames is used as the edge weight to construct the temporal trajectory graph of the zebrafish target, such as nodes ((x1, y1) → (x2, y2) → ... → (xn, yn)). When the zebrafish is occluded or multiple zebrafish overlap, if there are no detection results in some frames from t=5 to t=8, the trajectory is broken. In this case, the path needs to be completed in the broken interval from t=5 to t=8. The starting point is (x5, y5) of the last valid node before the break at t=5, and the ending point is (x8, y8) of the first valid node after the break at t=8. The optimal path is searched using the best path algorithm. The heuristic function is set as the straight-line distance between two points, and the actual cost of the edge is set as the deviation between the assumed path and the zebrafish's motion inertia. This ensures that the completed path conforms to the physical laws of zebrafish swimming, such as not suddenly reversing or experiencing sudden changes in speed. After completion, a continuous trajectory (x5, y5) → (x6, y6) → (x7, y7) → (x8, y8) is obtained, which is used for subsequent behavior analysis, such as calculating the average swimming speed.

[0046] The zebrafish behavior includes their swimming speed, undulation frequency, and loose group size; corresponding normal threshold ranges are set for each of these parameters; water quality is assessed based on the trajectory and behavioral data of multiple zebrafish targets, including the following steps:

[0047] Step 1-1: Based on the trajectory and behavioral data of multiple zebrafish targets, determine whether the zebrafish exhibit any abnormal behavior other than changes in swimming speed, oscillation frequency, and loose group size.

[0048] Steps 1-2: When zebrafish exhibit abnormal behavior, it is determined that there is abnormal pollution in the water where the zebrafish are located.

[0049] Steps 1-3: If no abnormal behavior is observed in the zebrafish, determine whether the zebrafish's swimming speed, undulation frequency, and loose group size exceed the normal threshold range.

[0050] Steps 1-4: When the swimming speed, wiggling frequency, and loose group size of multiple zebrafish targets exceed any set normal threshold range, the water in which the zebrafish are located is considered to have moderate pollution. When the swimming speed, wiggling frequency, and loose group size of the zebrafish are all within the normal threshold range, the water in which the zebrafish are located is considered to have no pollution. When the swimming speed, wiggling frequency, and loose group size of more than three zebrafish targets exceed any set normal threshold range, the water in which the zebrafish are located is considered to have moderate pollution.

[0051] The swimming speed and loose group size of zebrafish can be calculated from the trajectory points of zebrafish swimming trajectory data. The swimming speed is calculated from the center point coordinates of two adjacent frames of each zebrafish, and the loose group size is determined by the distance between the center point coordinates of multiple zebrafish. The wobbling frequency of the zebrafish is calculated by analyzing multiple single-frame still images of the same zebrafish.

[0052] In steps 1-2, when zebrafish exhibit abnormal behavior, the source of pollution in the water is determined based on the type of abnormal behavior. When zebrafish exhibit intermittent twitching and sideways swimming, heavy metal pollution is determined to be present in the water where the zebrafish are located. When zebrafish exhibit frequent surfacing and swallowing, and alternating disordered swimming and stagnation, organic pollution is determined to be present in the water where the zebrafish are located. When zebrafish exhibit rapid swimming in the early stage and stillness at the bottom in the later stage, ammonia nitrogen pollution exceeding the standard is determined to be present in the water where the zebrafish are located. When zebrafish exhibit jumping and spinning during the excited period and paralysis and tilting during the inhibited period, pesticide pollution is determined to be present in the water where the zebrafish are located.

[0053] In steps 1-4, the degree of pollution of the water in which the zebrafish are located is initially determined by the number of zebrafish behaviors that exceed the normal threshold range in terms of swimming speed, wiggling frequency, and loose group size. Finally, the degree of pollution of the water in which the zebrafish are located is determined by the number of zebrafish whose swimming speed, wiggling frequency, and loose group size exceed the normal threshold range among multiple zebrafish targets.

[0054] Normal threshold ranges are set for zebrafish swimming speed, oscillation frequency, and loose group size. The normal threshold range for swimming speed includes an upper limit and a lower limit. The upper limit can be 4 cm / s and the lower limit can be 2 cm / s. The normal threshold range for oscillation frequency includes an upper limit and a lower limit. The upper limit can be 2 times / second and the lower limit can be 1 time / second. The normal threshold range for loose group size includes an upper limit and a lower limit for distance. The upper limit can be 3 times the body length and the lower limit can be 2 times the body length.

[0055] If a zebrafish exhibits a behavior exceeding its corresponding normal threshold range in one of the following three categories—swimming speed, wiggling frequency, or loose group size—then the ordinary pollution is initially classified as slight pollution. Next, the number of zebrafish exhibiting this behavior exceeding its corresponding normal threshold range is determined. If this number exceeds a set zebrafish count threshold H1, the ordinary pollution is classified as moderate pollution. If the number of zebrafish does not exceed the set zebrafish count threshold H1, the ordinary pollution is still classified as slight pollution.

[0056] If each zebrafish exhibits two behaviors exceeding its corresponding normal threshold range in swimming speed, wiggling frequency, and loose group size, the pollution is initially classified as moderate pollution. Next, the number of zebrafish exhibiting these behaviors is counted. If this number exceeds a set zebrafish count threshold H2, the pollution is classified as severe pollution. If the number of zebrafish does not exceed the set zebrafish count threshold H2, the pollution remains moderate pollution. If the number of zebrafish does not exceed the set zebrafish count threshold H2, and is also lower than the threshold H2 by one threshold H2, the pollution is classified as slight pollution.

[0057] When a zebrafish exhibits three behaviors exceeding its corresponding normal threshold range—swimming speed, wiggling frequency, and loose group size—it is initially classified as severe pollution. Next, the number of zebrafish exhibiting these behaviors exceeding the threshold range is determined. If this number exceeds a set zebrafish count threshold H3, the pollution is classified as extremely severe. If the number of zebrafish does not exceed the threshold H3, the pollution remains severe. If the number of zebrafish is below the threshold H3 but still one threshold H3 lower than the threshold H3, the pollution is classified as moderate. The pollution level is then displayed, and relevant personnel are notified to take action to mitigate or eliminate pollution in the water where the zebrafish are located.

[0058] The coordinate sequences in the collected trajectory data can constitute the movement trajectory of zebrafish, the number of consecutive frames can reflect the dwell time, and the distribution of multi-target coordinates can reflect the cluster density. MATLAB is a scientific computing platform with powerful numerical computation and visualization capabilities. MATLAB's plotting toolbox can transform quantitative parameters such as trajectory and behavioral data into intuitive visualizations. Compared to simple numerical lists, visualization allows researchers to quickly capture zebrafish behavioral patterns, such as the presence of phototaxis / fluidotaxis, the temporal patterns of group aggregation and dispersion, and the coordination between individual and group movements. Simultaneously, MATLAB's statistical toolkit can conduct further quantitative analysis based on the visualization results, providing an efficient technical path for exploring the neural mechanisms and environmental response mechanisms of zebrafish behavior, accelerating the transformation from experimental data to scientific conclusions.

[0059] In steps 1-2, when the zebrafish exhibits intermittent twitching and sideways swimming, heavy metal pollution is determined to be present in the water. The concentration of heavy metal pollutants is then determined based on the number of intermittent twitches. A threshold for the number of twitches can be set at 1 twitch / second. When the number of intermittent twitches is lower than the threshold, the concentration of heavy metal pollutants in the water is considered low. When the number of intermittent twitches is higher than or equal to the threshold, the concentration of heavy metal pollutants in the water is considered high. Furthermore, for every additional threshold twitch, the concentration of heavy metal pollutants in the water increases by N times, where N can be twice.

[0060] When zebrafish exhibit frequent surfacing for food, alternating erratic swimming and stillness, organic pollution is identified in the water. The amount of organic pollution is determined by the duration of surfacing during these surfacing and swallowing phases. The end time of one zebrafish surfacing phase and the end time of the next surfacing phase are calculated to obtain one zebrafish swimming cycle. The percentage of time the zebrafish spends surfacing within this swimming cycle is calculated. When this percentage exceeds a set threshold of 30%, pollution is identified in the water. Furthermore, for every 5% increase in the zebrafish surfacing percentage beyond the set threshold, the amount of organic pollution is increased by 1%.

[0061] When zebrafish exhibit initial rapid swimming followed by stillness at the bottom, it indicates excessive ammonia nitrogen pollution in the water. The initial rapid swimming speed indicates the ammonia nitrogen concentration; a higher speed indicates a higher concentration. When zebrafish exhibit jumping and spinning during the excited phase and paralysis and tilting during the inhibited phase, it indicates pesticide pollution. The frequency of jumping and spinning during the excited phase indicates the pesticide concentration; a higher frequency indicates a higher concentration. However, if there is no jumping or spinning during the excited phase, only paralysis and tilting during the inhibited phase, it indicates extremely high pesticide concentrations.

[0062] The above description is merely a preferred embodiment of the present invention. Those skilled in the art can make several modifications and optimizations based on the above disclosure without departing from the basic principles described above. These modifications and optimizations should be considered as being within the scope of protection of the present invention.

Claims

1. A water quality detection method based on deep learning and zebrafish behavior, characterized in that: Several continuous zebrafish videos were acquired. A pre-trained, frame-by-frame cropping technique was used to decompose these videos along the time dimension, resulting in several single-frame static images. Labelimg was used to annotate the obtained single-frame static images with pixel-level bounding boxes, recording the spatial parameters and category information of the zebrafish in the image coordinate system. The Labelimg-annotated single-frame static images were then input into the YOLOV8 algorithm for iterative training and generalization performance verification. The trained YOLOV8 algorithm was combined with a multi-target tracking algorithm to track multiple zebrafish targets in real time, and trajectory and behavioral data of these targets were collected. The water quality is assessed by analyzing the trajectory and behavioral data of multiple zebrafish targets, and the trajectory, behavioral, and water quality data of the multiple zebrafish targets are then visualized.

2. The water quality detection method based on deep learning and zebrafish behavior according to claim 1, characterized in that: Labelimg is used to perform pixel-level bounding box annotation on the obtained single-frame static image. This includes setting a pixel coordinate system with the top left corner of the image as the origin (0,0), the X-axis pointing horizontally to the right, and the Y-axis pointing vertically downward. A rectangular bounding box is used to select the zebrafish from the top left edge pixel to the bottom right edge pixel to obtain the initial bounding box of the zebrafish. The initial bounding box of the zebrafish is fine-tuned to ensure that the initial bounding box covers all zebrafish pixels, resulting in a zebrafish bounding box image. The pixel coordinates of the top left and bottom right corners of the zebrafish bounding box image are captured in real time, and the obtained pixel coordinates and category information are annotated on the zebrafish bounding box image to obtain an annotated single-frame static image. The annotated single-frame static images are saved in VOC XML format.

3. The water quality detection method based on deep learning and zebrafish behavior according to claim 1, characterized in that: Several single-frame static images annotated with Labelimg are input into the YOLOv8 algorithm for iterative training and to verify generalization performance; including the following steps: Step 1: Convert the labeled single-frame static image from VOC XML format to TXT format and normalize it; Step 2: Divide the labeled single-frame static images into training set, validation set and test set according to the proportion; Step 3: Perform data augmentation operations on the images in the training set, validation set, and test set according to the specific scenario, scale all images to a uniform pixel size, and perform pixel value normalization to ensure uniform distribution of input data; Step 4: Using the training set, validation set, and test set obtained in Step 3, the YOLOV8 algorithm is iteratively trained, its generalization performance is verified, and its final performance is tested.

4. The water quality detection method based on deep learning and zebrafish behavior according to claim 1, characterized in that: The training set is input into the iterative training of the YOLOV8 algorithm, including weighted summation of Focal Loss (classification loss), CIoU Loss (localization loss), and BCE Loss (target confidence loss) to obtain a composite loss function to guide parameter optimization; the calculation process of the composite loss function is as follows: In the formula, Loss F Focal Loss is the classification loss, Loss C For CIoU Loss, locate the loss, Loss B BCE Loss is the target confidence loss; W1, W2, and W3 are the weights of Focal Loss (classification loss), CIoU Loss (localization loss), and BCE Loss (target confidence loss), respectively.

5. The water quality detection method based on deep learning and zebrafish behavior according to claim 1, characterized in that: The multi-target tracking algorithm employs both the optimal path algorithm and the optimal target selection algorithm to continuously track and maintain the identity of multiple zebrafish targets; the optimal path algorithm selects the best motion path between frames for each zebrafish target. The optimal target selection algorithm selects detection boxes that match real zebrafish targets based on detection confidence and appearance feature similarity.

6. The water quality detection method based on deep learning and zebrafish behavior according to claim 1, characterized in that: The zebrafish behavior includes their swimming speed, undulation frequency, and loose group size; corresponding normal threshold ranges are set for each of these parameters; water quality is assessed based on the trajectory and behavioral data of multiple zebrafish targets, including the following steps: Step 1-1: Based on the trajectory and behavioral data of multiple zebrafish targets, determine whether the zebrafish exhibit abnormal behavior other than changes in swimming speed, oscillation frequency, and loose group size. Steps 1-2: When zebrafish exhibit abnormal behavior, it is determined that there is abnormal pollution in the water where the zebrafish are located. Steps 1-3: If no abnormal behavior is observed in the zebrafish, determine whether the zebrafish's swimming speed, undulation frequency, and loose group size exceed the normal threshold range. Steps 1-4: When any one of the following zebrafish behaviors—swimming speed, oscillation frequency, and loose group size—exceeds the set normal threshold range, the water in which the zebrafish are located is considered to have ordinary pollution; when the zebrafish's swimming speed, oscillation frequency, and loose group size are all within the normal threshold range, the water in which the zebrafish are located is not considered to have pollution.

7. The water quality detection method based on deep learning and zebrafish behavior according to claim 1, characterized in that: In steps 1-2, when zebrafish exhibit abnormal behavior, the source of pollution in the water is determined based on the type of abnormal behavior; when zebrafish exhibit intermittent twitching or sideways swimming, it is determined that the water in which the zebrafish is located is contaminated with heavy metals. When zebrafish exhibit frequent surfacing and swallowing, and erratic alternation between swimming and stagnation, it indicates organic pollution in the water. When zebrafish initially swim rapidly and then remain still at the bottom, it indicates excessive ammonia nitrogen pollution in the water. When zebrafish exhibit jumping and spinning during the excited phase and paralysis and tilting during the inhibited phase, it indicates pesticide pollution in the water.