Code Monkey home page Code Monkey logo

animal-tracking's Introduction

Animal Tracking

Object tracking with OpenCV in an open field behavioral test


Origins

Input sample video is rat.avi placed in the root directory.

Output video demonstration is placed on the link below.

Open Field Test (Animal Tracking with OpenCV)

After processing the input video(s) by the script track.py you will get new directory <current date>_distance with CSV table

Animal Distance Run Time
ratN 56.38 420.1
... ... ...

and two subdirectories

  1. timing with processed video(s) as above
  2. tracks with images of animal(s) track(s) as on the left side of processed video

Prerequisites

Code Environment

Packages

Tracking is based on very simple image preprocessing and processing algorithms implemented in OpenCV, therefore OpenCV and Numpy are the only requirement. To install it on your python environment type pip install -r requirements.txt in your terminal.

Codecs

Initially the script takes no arguments, scans its own directory and searches all AVI files encoded with H.264, but it is up to you which file extension the script will search. To make a choice just replace all *.avi strings in the source code by your own extension.

Camera Environment

Since the pixels of an animal contour are nested to the floor and the floor contour is nested to the box walls (animal ⊂ floor ⊂ walls), the floor have to be as contrast to its neighbors in this triplet as possible for better distinction between all of them. For example: white walls, black floor, and white animal or vice versa is the best case.

Example of contrast

Because it's hard to fit the floor of the box within camera frame in the laboratory, the script makes some preprocessing. The script takes a certain part of each frame of the videos. It takes a right square frame[:, w-h:w] of a frame with sides equal to height h of a frame.

Crop right square of the frame

That's why the box with an animal have to be placed in the right area of camera field of view.

(You are free to change this behavior replacing frame[:, w-h:w] (right square) by frame[:, 0:w-h] (left square), or just flip horizontally your video if you want to leave the script untouched.)

User Interface

If requirements are complied then after starting the script you will get 1st frame of the video to be proccesed

Good fit

The right half of the window is the ordinary frame with the floor highlighted by the red quad. This highlighted area is stretched by homographic transform up to the square area on the left left half of the window.

  • If you're satisfied the look of the floor you can either press any key except ESC or click right mouse button to continue.
  • Else you are free to determine the right way of highlighting the area of the floor. To do that click left button on each corner of the floor starting with any of them.

Fitting

Immediately after click on the 4th corner you can either

  • press Enter and the processing will start or
  • repeate the loop and realign the contour of the floor again.

animal-tracking's People

Contributors

colinlaney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

animal-tracking's Issues

IndexError: list index out of range

I used an avi file of Morris water maze (circular arena) and I encountered an error. I attached the video I used.

After lesion.zip

Traceback (most recent call last):
File "track.py", line 266, in
floorCrop(filename)
File "track.py", line 120, in floorCrop
tetragonVertices = tetragons[0]
IndexError: list index out of range

And I would like to know if wanted to use mp4 with h.264 codec what should I change in track.py?

Calibration

How to calibrate the arena for estimation of distance covered by the animal?

Distance from centre evaluation

A common use of the open field test, is to measure how much time the animal spends at the periphery vs. the center. Currently there isn't any way to do this via your script. I would be looking to implement this.

  • Would you be willing to pull if I do?
  • Do you have any suggestions on how this can be most easily calculated from variables which you already define?

Issues with the code

I there! As i said on youtube, im having some problems with the code. I already replaced avi for mp4 in all the places where it should. But now im not quiet sure where im supose to put the path to my video. Best regards

Issue with tkinter gui

the thing is I checked if the cv2.imwrite works or not by adding this:

if not cv2.imwrite(RELATIVE_DESTINATION_PATH + 'traces/' + name + '[distance]=%.2f' % distance +
'[time]=%.1fs' % t + '.png', cv2.resize(imgTrack, (max(HD), max(HD)))):
raise Exception("Could not write image")

and the exception is always raised

and If the problem is that I'm passing file path and not just the file name to the trace method then how to get the .mp4 part of the path from "GUI.filepath" only ?

The program shows me just half of my field

When i run the program , it shows me just half of my field ( but I’m using rat in a smaller box)
I’ve tried to change it with changing “. frame[:,w-h:w ]”
And also I’ve put 1 to HALF_AREA=1hh .
I’m using mp4 video in 30 FPS and 1280 to 720.
And also I want to count average speed every 20 Seconds.
Is it possible to use your method in real-time.?
Thank you for answer !

Originally posted by @amirprobuetmir in #1 (comment)

Documentation/usage examples

Hi,

I am interested in using and contributing to your software. I am quite expereinced with python, and I would be very happy to do my animal tracking analysis in an open source environment.

Sadly, I find it a bit difficult to get a clear understanding of your code. Would you mind adding a bit more documentation (in the form of docstrings/comments) or maybe a few usage examples?

How is the distance calculated ?

Hi, I wanted to ask how the distance covered is calculated because I want to make the results as accurate to the actual results (specially since I know the size of my test environment) as possible since I'm using this program in my thesis.

Thanks :)

Can I change the way the distance is calculated ?

Hi, regarding the last issue (which was really helpful in understanding the code), I wanted to ask if there's a way I can change that, meaning to have a pop up appear and ask the user for the dimensions of the area of the video that they selected in order to make the results as accurate as possible ?

Thanks :D

ValueError: need more than 2 values to unpack

import track.py

Traceback (most recent call last):
File "", line 1, in
File "track.py", line 252, in
floorCrop(filename)
File "track.py", line 91, in floorCrop
_, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack

I'm testing the module with your rat.avi video. But I get value error. How to fix the error?

Taking the right side square of the camera environment

Hi I am trying to change the behavior of the code where it does not take the right side square of the video (frame[:, w-h:w]) or the left side for that matter and I want it to take the full video without any section of pixels cut off so that I can crop the full test environment (in my case it's a Morris Water Maze test if you're wondering).

Two errors that I came across

After pulling the latest version and running the code on the sample I came across this error

Traceback (most recent call last):
File "track.py", line 252, in <module>
floorCrop(filename)
File "track.py", line 91, in floorCrop
contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack (expected 2)

Adding _, and a space before line 91, like this
_, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)`

I achieve another error sadly.This error comes after drawing a box around the rat,

Segmentation fault (core dumped)

Description of my problem about running long play video file

Q: Specify input data (codec, resolution), processing time and resources, output errors, and whether you have tried to trim a long video.

A: Codec: Timed metadata, H.264, MPEG-4 AAC
Resolution: 1080 * 1920
Processing time: 10 minutes
Output errors:

Traceback (most recent call last):
  File "/Users/hyuk-hoon/Desktop/Animal/animal-tracking-master/track.py", line 287, in <module>
    trace(filename)
  File "/Users/hyuk-hoon/Desktop/Animal/animal-tracking-master/track.py", line 236, in trace
    imgContour = cv2.add(imgPoints, imgTrack)
cv2.error: OpenCV(4.7.0) /Users/runner/work/opencv-python/opencv-python/opencv/modules/core/src/arithm.cpp:650:
 error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array'
 (where arrays have the same size and the same number of channels), nor 'array op scalar',
 nor 'scalar op array' in function 'arithm_op'

Code not running properly

Hi! Thank you again for all the help till now. I run the code and it lets me select the ROI, but when i press space ou enter to confirm the window just vanishes. On the console i can see the code running, but it doesnt do anything.

I want to remove the normal colored frame

Hi,
1- I want to make a modification and only show the "frameGray" in both the drawing of the cropping polygon at the beginning and the analysis phase, I tried to remove "frame" from line 133: imgFloorCorners = np.hstack([frame,frameGray]) and it worked in the cropping phase but when it came time to processing the video it didn't even begin:
Screenshot 2021-07-09 142951
Screenshot 2021-07-09 143116

2-There was a previous issue that I made about a GUI I made (a demo one) and I forked my repo can you look at it ?

Thanks :)

Improving CLI functionality

Hello, I finally got around to cloning, testing, and having a closer look at this. Your code looks fairly simple and straightforward, especially compared to e.g. Pyper.

Ideally, though, in order to accommodate more functionalities (e.g. explicit target file specification, batch processing, different evaluation metrics) , the script functionality would be covered by a CLI.

This would require a bit of code rearrangement.

I'm basically writing to ask whether you would be interested to pull. If not, I might instead want to fork your code into behaviopy.

An added GUI with tkinter

Hi I made a GUI and here's it's code:
from os import close
from tkinter import *
from tkinter import filedialog,messagebox
import track

FotaGui = Tk()
FotaGui.geometry('600x600')

def file_path():

global filepath
filepath = StringVar()
#Fetch the file path of the mp4 file browsed.
if(filepath == ""):
    filepath = filedialog.askopenfilename(title = "select a file", filetypes = [("mp4 files", "*.mp4")])
else:
    filepath = filedialog.askopenfilename( initialdir=filepath, title = "select a file", filetypes = [("mp4 files", "*.mp4")])

#Morris Water Maze
def MWM():

#Validation of entry fields, if left empty.
if filepath == "":
    messagebox.showinfo('Info','Please select a video')
else:
    filepathlabel.config(text=filepath)
    exec("track")

Browsebutton = Button(FotaGui,width = 18,text= "Browse",command = file_path)
Browsebutton.pack()

MWMbutton = Button(FotaGui,width = 18,text="Morris Water Maze Test",command = MWM)
MWMbutton.pack()

filepathlabel = Label(FotaGui,text = "mp4 file path:",font = ('Times 10'))
filepathlabel.pack()

FotaGui.mainloop()

and I changed the last couple of lines in track.py to this:
#for filename in glob.glob('.mp4'):
#floorCrop(filename)
#for filename in glob.glob('
.mp4'):
#file = open(RELATIVE_DESTINATION_PATH + 'distances.csv', 'a')
#trace(filename)

floorCrop(GUI.filepath)
file = open(RELATIVE_DESTINATION_PATH + 'distances.csv', 'a')
trace(GUI.filepath)

now there are 2 problems:
1-when the track.py file finishes execution, the .csv file erases previous results and overwrites it with new ones
2-there are no tracing images saved in the trace folder, even though in the normal code it did save tracing images

Track.py Code Issue

Traceback (most recent call last):
  File "~/animal-tracking/track.py", line 283, in <module>
    floorCrop(filename)
  File "~/animal-tracking/track.py", line 97, in floorCrop
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack (expected 2)

The cropping method

Hi it's me again :D, I wanted to know how you did the cropping method (as in moving the mouse and clicking 4 times to crop a dynamic 4 sided shape):
1-I want to know how it is done and if there's any resources that could help me in that
2-If I can crop more than one ROI (region of field) and have multiple viewable traces for the same video
3-And I know this is a little off topic from what this repo does is to count how many times the mouse entered each ROI and the amount of time spent in it each time it did (I'm doing the Y-Maze Test next to give you some clarification)

Thanks in advance :)

Open field test for circular arena

I'm interested in assessing the behaviour of animals by open field test in a circular arena.Is it possible to track their movements using animal-tracking in a circular arena? It would also be useful in Morris Water Maze.

Tracking is not working

Hello,

I'm running the code but the tracking is not working even with the video attached to the zip file.
It seems to follow the bright spots but rarely the rat as you can see in the video.

Untitled.Project.mp4

I've tried to play with the threshold but without success. Any suggestions?

Thank you a lot for your effort!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.