Добавление дополнительных функций в базовый скрипт отслеживания движений на PythonPython

Программы на Python
Гость
Добавление дополнительных функций в базовый скрипт отслеживания движений на Python

Сообщение Гость »


For my dissertation project I am looking at tadpole movement patterns. I have a python script written by a previous PhD student. Currently the script takes a video and tracks the tadpole as it swims around the tank, at the end it outputs a text file with the total distance travelled in pixles. At the start of the script I get prompted with a window to designate the tracking arena.(Изображение). To designate the tracking area I can drag around a square to the place I want. The draggable square has a numbered grid inside it. Then I get another window to draw a square around an indiviudal tadpole which is used to set the minimum and maximum size that the script detects when tracking the movement.
For my dissertation I need to modify the script to track distance travelled within the outside 16 squares (The numbered squares which are seen when designating the tracking arena) and the distance travelled within the middle 9 squares.
The whole script is split into 2 files, one which I run using command line and the other contains some of the needed functions.
This is the main code which I run.

Код: Выделить всё

#Importing all the packages needed and the tracking functions
import cv2
import os
import numpy as np
from collections import deque
import Tracking_functions1 as tc

# Information to change
sub_varThreshold = 90 #values usually work between 90-150 - the pixel threshold value to be counted as a "moving tadpole"  object
sub_learn_rate = -1 # -1 is the default, 0.05 also works well
blur_kernal = (1, 1) #use smaller kernal sizes (1,1) for small tadpoles and larger kernals (5,5) for big tadpoles
min_area = 1 #initalise min area as a number before setting by drawing a rectangle
max_area = 1 #initalise max area as a number before setting by drawing a rectangle
large_movement_threshold = 1 #initalise threshold as a number before setting by drawing a rectangle
start_position = 2 #where we start
start_frame = start_position  #the videos are fps so if start position is in seconds, this takes us to frames
min_area_multiplier = 0.3 #the min area we will track will be 0.4 of the rectangle drawn around the individual
max_area_multiplier = 1.5 #the max area we will track will be 0.4 of the rectangle drawn around the individual
key = None #initalise
cap = None #initalise
frame_count = 0  # Variable to keep track of the frame number

# Global variables for tracking arena dimensions
arena_width = 900
arena_height = 900
grid_rows = 5
grid_cols = 5
grid_width = arena_width // grid_cols
grid_height = arena_height // grid_rows

# Create dictionaries to store window statuses
windows = {
'Draw Tracking Arena': False,
'Draw Area to Track':  False
}

# Create a function to create and show windows
def create_window(window_name, image):
cv2.imshow(window_name, image)
windows[window_name] = True

# Create a function to destroy windows
def destroy_window(window_name):
cv2.destroyWindow(window_name)
windows[window_name] = False

# Function to handle mouse events for dragging the square
def drag_square(event, x, y, flags, param):
global top_left_pt1, dragging, offset_x, offset_y, bottom_right_pt1, arena_width, arena_height

if event == cv2.EVENT_LBUTTONDOWN:
if top_left_pt1[0] 

Источник: [url]https://stackoverflow.com/questions/78129830/adding-additional-functionality-to-a-basic-movement-tracking-script-in-python[/url]

Вернуться в «Python»