epfl-cs358 / 2024sp-robopong

2 stars 0 forks source link

[CV] Benchmark latency at all/most steps #20

Closed Amene-Gafsi closed 5 months ago

Amene-Gafsi commented 5 months ago

We want to get a feedback on the overall latency involved in processing video frames, tracking objects, and predicting future positions in a real-time application. To achieve this, I integrated in the main method of the code three timestamped print statements that log the current time at key stages of execution:/

Start of Frame Processing: At the beginning of each loop iteration, a print statement logs the time just as the frame processing starts. This helps in measuring the time taken right from the onset of processing each video frame. Post-Tracking: After the ball and paddle positions have been tracked and identified within the frame, another print statement logs the time, providing insight into how long the tracking phase takes. Post-Prediction: Finally, once the future position of the ball is predicted based on its velocity and trajectory, a third print statement logs the time. This gives feedback on the duration of the computational phase that includes predicting the ball’s future position. Together, these time stamps offer a clear timeline of the processing delays and computational overhead involved in each frame's analysis, helping to identify bottlenecks or areas for optimization in real-time video processing tasks.

    while True:
        print("start processing frames...", time.time())

        frame = vs.read()[1]
        if frame is None: break
        process_frame(frame)
        print("tracked", time.time())
        if len(ball_pts) >= 2:
            # Get the last 64 ball positions and times
            observations = [(timestamp, x, y) for timestamp, (x, y) in ball_pts]   

            # Compute last y position prediction
            vx, vy = calculate_velocity(observations)
            _, last_obs_x, last_obs_y = observations[-1]
            y_position_at_end, total_time = predict_final_position_and_time((last_obs_x, last_obs_y), vx, vy)

            # Only consider cases where the difference between the old and new prediction are different enough
            if(abs(last_predicted_position - y_position_at_end) > tolerance):
                print(f"Y-position at end of board: {y_position_at_end} mm, Time to reach end: {total_time} ms")
                last_predicted_position = y_position_at_end
                prediction.appendleft((total_time, y_position_at_end))

            print("ended", time.time())

We obtain results that are approximately as follows:

Image

and we can see that the overall latency to detect ball, track it in real time and predict it's future coordinate is around : 1714482140.071244-1714482140.0263436 = 0.045 = 45 ms which is fast enough for our project