Track position and movement of the projectile

3 views
Skip to first unread message

Paul Wandendeya

unread,
Jun 27, 2024, 6:51:24 PM (5 days ago) Jun 27
to Elliot Garbus, kivy-...@googlegroups.com
This code interface the app with camera sensors and track the projectile's position and movement
Is it correct?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.vector import Vector
import numpy as np
import cv2

class ProjectileTracker(BoxLayout):
    def __init__(self, **kwargs):
        super(ProjectileTracker, self).__init__(**kwargs)
        self.orientation = 'vertical'

        # Labels to display projectile position and movement
        self.position_label = Label(text='Projectile Position: (0, 0)', font_size=24)
        self.velocity_label = Label(text='Projectile Velocity: (0, 0)', font_size=24)
        self.acceleration_label = Label(text='Projectile Acceleration: (0, 0)', font_size=24)

        # Buttons to start and stop tracking
        self.start_button = Button(text='Start Tracking', on_press=self.start_tracking)
        self.stop_button = Button(text='Stop Tracking', on_press=self.stop_tracking)

        self.add_widget(self.position_label)
        self.add_widget(self.velocity_label)
        self.add_widget(self.acceleration_label)
        self.add_widget(self.start_button)
        self.add_widget(self.stop_button)

        # Initialize projectile properties
        self.position = np.array([0, 0])
        self.velocity = np.array([0, 0])
        self.acceleration = np.array([0, -9.81])  # Assuming gravity is the only force
        self.is_tracking = False
        self.clock_event = None

        # Initialize camera
        self.camera = cv2.VideoCapture(0)
        self.camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        self.camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

    def start_tracking(self, instance):
        self.is_tracking = True
        self.clock_event = Clock.schedule_interval(self.update_projectile, 0.01)
        self.clock_event = Clock.schedule_interval(self.update_from_camera, 0.01)

    def stop_tracking(self, instance):
        self.is_tracking = False
        Clock.unschedule(self.clock_event)
        self.camera.release()

    def update_projectile(self, dt):
        # Update position, velocity, and acceleration
        self.position += self.velocity * dt
        self.velocity += self.acceleration * dt

        # Update the labels
        self.position_label.text = f'Projectile Position: ({self.position[0]:.2f}, {self.position[1]:.2f})'
        self.velocity_label.text = f'Projectile Velocity: ({self.velocity[0]:.2f}, {self.velocity[1]:.2f})'
        self.acceleration_label.text = f'Projectile Acceleration: ({self.acceleration[0]:.2f}, {self.acceleration[1]:.2f})'

    def update_from_camera(self, dt):
        # Capture a frame from the camera
        ret, frame = self.camera.read()

        # Perform image processing to detect the projectile
        # and update the position, velocity, and acceleration
        # based on the detected projectile position

        # Update the labels with the new values
        self.position_label.text = f'Projectile Position: ({self.position[0]:.2f}, {self.position[1]:.2f})'
        self.velocity_label.text = f'Projectile Velocity: ({self.velocity[0]:.2f}, {self.velocity[1]:.2f})'
        self.acceleration_label.text = f'Projectile Acceleration: ({self.acceleration[0]:.2f}, {self.acceleration[1]:.2f})'

class ProjectileApp(App):
    def build(self):
        return ProjectileTracker()

if __name__ == '__main__':
    ProjectileApp().run()
```

The main changes are:

1. Added the `update_from_camera` method, which is called every 0.01 seconds to capture a frame from the camera, perform image processing to detect the projectile, and update the position, velocity, and acceleration of the projectile based on the detected position.
2. Initialized the camera with the desired frame size (640x480 in this example) in the `__init__` method.
3. Scheduled the `update_from_camera` method in the `start_tracking` method, and released the camera in the `stop_tracking` method.

Note that the actual image processing and detection of the projectile is not implemented in this code snippet, as it would depend on the specific use case and the type of projectile being tracked. You would need to add the necessary computer vision and image processing logic to detect the projectile and update its position, velocity, and acceleration accordingly.

Also, keep in mind that this is a simplified example, and you may need to add additional features, error handling, and optimization to make it suitable for a real-world application.
Reply all
Reply to author
Forward
0 new messages