Detect_animals

Evaluate if an image contains moving objects (animals).

The main routine of the code will iterate through every folders present in the [SRCDIR] directory and analyze found images, ultimately looking for moving objects.

The following module level attributes are passed via the command line interface.

detect_animals.srcdir

str – The directory containing folders (one or many) with images to analyze.

detect_animals.resultsdir

str – The location where to create the FoxMaskResults directory.

class detect_animals.Getimagesinfos(folder)

Get all images to be analyzed and their metadata

getimageslist()

Generate a list of all images under folder

Returns:imglist. All images under the folder being analyzed.
Return type:list
getimagesmeta()

Generate a list of all images time of creation.

The time of creation is taken from the DateTimeOriginal key present in every image metadata. If the method can not read the DateTimeOriginal key, it will look in the Comment key trying to find the time of creation. Method will gracefully exit if not time of creation can be found.

Example of format returned:

>>> datetime.datetime(2014, 8, 6, 16, 5, 55)
Returns:timeofcreation. Datetime objects representing the exact time of creation of each image.
Return type:list
getimpg()

Group images based on their time of creation

This method will group images based on the differences in time between each shot.

Parameters:maxgap (int) – This represent the maximum gap, , in seconds for two consecutive images to be considered as being part of the same group.
Returns:impg. Number of images in each group.
Return type:list
sortimages()

Sort images based on their time of creation.

This method makes sure images are appropriately sorted. We do not want to rely on images file name to sort them.

Parameters:timeofcreation (date) – A date object representing the time of creation of each image.
Returns:sortedimglist. Sorted time of creation of all images to analyze.
Return type:list
class detect_animals.Imagesanalysis(folder)

Analyze images to detect moving objects.

Parent:
Getimagesinfos
bgfgestimation(sortedimglist, impg, srcdir)

Estimate background model and perform foreground segmentation.

The method will iterate over each item of the impg list, and performs the analysis on each groups. The trcd value, which is influencing significantly the outcome of the analysis, is pass to the ForegroundSegmentation code. To pass this value to the cpp code, it is written to a file /tmp/params.txt which is then read by the cpp code at runtime.

Images to analyze are resized (for performances issues on non server grade hardware) and then saved to a temporary directory that is deleted after each run. The results of this method are black and white masks that are written to disks in srcdir/MasksResults.

Note

This code will only run successfully if the input images are named as the following: whatever-name_[4 digits].jpg. It is a requirement of the ForegroundSegmentation code. We do not think that FoxMask should handle the naming of the images to be analyzed. This should be done beforehand, by a prepossessing task.

  • trcd : Threshold for cosine distance. This value is fed to the ForegroundSegmentation code. It is a static value, but it could be made more dynamic using, for example, the average light in the image, as shown in the code below:
if avgB[0] < 100.0:
    print 'Low light', avgB
    tfcd.write(str(0.001))
else:
    print 'High light', avgB
    tfcd.write(str(0.005))
Parameters:
  • sortedimglist (list) – The images to be analyzed
  • impg (list) – Groups of images on which to run the analysis.
  • scrdir (string) – The top level directory of the analysis.
getmaskslist(srcdir)

Create a sorted list of generated masks.

This method will create a list of all the images present in srcdir/MasksResults. Theses images are the black and white masks created by the foxmask.Imagesanalysis.bgfgestimation() method. Before creating the list, masks with the prefix EstBG are removed.

Parameters:srcdir (str) – Top level directory containing directories of images.
Returns:maskslist. All masks to analyze.
Return type:list
masks_analysis()

Analyze masks to detect moving objects

This method will analyze created masks, taking the masks list to analyze from foxmask.Imagesanalysis.getmaskslist(). The area of all white objects in each masks are calculated. If the area is smaller than parameters.minsize, the object is not considered as an animal.

Returns:resultslist. Results of the masks analysis.
Return type:list
writeresults(item, resultsdir)

Write results of the mask analysis to file

Parameters:
  • item (str) – The name of the class analyzed, representing the name of the folder analyzed. This is used to name the table in which the results will be written.
  • resultsdir (str) – The top directory where to write the final results.
detect_animals.getfolders(srcdir)

Get the list of all folders in srcdir

FoxMask needs a list of folders containing images to analyze. Each folder must strictly contain a set of images. No sub folders are allowed. Code will gracefully exit if the srcdir argument does not exist.

Parameters:srcdir (str) – Top level folder containing all folders to analyze.
Returns:folderslist. Folders to analyze.
Return type:list
detect_animals.makeresultsfolder(resultsdir)

Make all needed folders to store the final results.

Parameters:resultsdir (str) – Top level folder for storing results.
Returns:None

Gathering folders to analyze

detect_animals.getfolders(srcdir)

Get the list of all folders in srcdir

FoxMask needs a list of folders containing images to analyze. Each folder must strictly contain a set of images. No sub folders are allowed. Code will gracefully exit if the srcdir argument does not exist.

Parameters:srcdir (str) – Top level folder containing all folders to analyze.
Returns:folderslist. Folders to analyze.
Return type:list

Creating results folders

detect_animals.makeresultsfolder(resultsdir)

Make all needed folders to store the final results.

Parameters:resultsdir (str) – Top level folder for storing results.
Returns:None

Getting images information

FoxMask will create a dictionary of classes containing all methods and attributes needed to analyze images in each found directory. Thus, the foxmask.Getimagesinfo class will be stored in a dictionary, one class for each directory being analyzed. This class is the parent of foxmask.Imagesanalysis

class detect_animals.Getimagesinfos(folder)

Get all images to be analyzed and their metadata

__init__(folder)

Initialize attributes used across methods.

Parameters:
  • folder (str) – Actual folder containing images to analyze.
  • imglist (list) – Images to analyze.
  • timeofcreation (list) – Images time of creation.
getimageslist()

Generate a list of all images under folder

Returns:imglist. All images under the folder being analyzed.
Return type:list
getimagesmeta()

Generate a list of all images time of creation.

The time of creation is taken from the DateTimeOriginal key present in every image metadata. If the method can not read the DateTimeOriginal key, it will look in the Comment key trying to find the time of creation. Method will gracefully exit if not time of creation can be found.

Example of format returned:

>>> datetime.datetime(2014, 8, 6, 16, 5, 55)
Returns:timeofcreation. Datetime objects representing the exact time of creation of each image.
Return type:list
getimpg()

Group images based on their time of creation

This method will group images based on the differences in time between each shot.

Parameters:maxgap (int) – This represent the maximum gap, , in seconds for two consecutive images to be considered as being part of the same group.
Returns:impg. Number of images in each group.
Return type:list
sortimages()

Sort images based on their time of creation.

This method makes sure images are appropriately sorted. We do not want to rely on images file name to sort them.

Parameters:timeofcreation (date) – A date object representing the time of creation of each image.
Returns:sortedimglist. Sorted time of creation of all images to analyze.
Return type:list

Analyzing images

The following class constitue the core code of the image analysis. It uses two external cpp libraries, coded by Vikkas Reddy:

The two libraries have been modified to be intragrated with FoxMask and to be able to run successful analysis on a small set of images (e.g. 5). The EstimateBackground code was performing well on a small set, but we could not get the ForegroundSegmentation code, which does it’s own background estimation, to perform well on small sets. So we used the EstimateBackround code to feed the ForegroundSegmentation in it’s background estimation. We accomplish that simply by duplicating the detected backround and configured the ForegroundSegmentation code to use theses images as training images.

class detect_animals.Imagesanalysis(folder)

Analyze images to detect moving objects.

Parent:
Getimagesinfos
bgfgestimation(sortedimglist, impg, srcdir)

Estimate background model and perform foreground segmentation.

The method will iterate over each item of the impg list, and performs the analysis on each groups. The trcd value, which is influencing significantly the outcome of the analysis, is pass to the ForegroundSegmentation code. To pass this value to the cpp code, it is written to a file /tmp/params.txt which is then read by the cpp code at runtime.

Images to analyze are resized (for performances issues on non server grade hardware) and then saved to a temporary directory that is deleted after each run. The results of this method are black and white masks that are written to disks in srcdir/MasksResults.

Note

This code will only run successfully if the input images are named as the following: whatever-name_[4 digits].jpg. It is a requirement of the ForegroundSegmentation code. We do not think that FoxMask should handle the naming of the images to be analyzed. This should be done beforehand, by a prepossessing task.

  • trcd : Threshold for cosine distance. This value is fed to the ForegroundSegmentation code. It is a static value, but it could be made more dynamic using, for example, the average light in the image, as shown in the code below:
if avgB[0] < 100.0:
    print 'Low light', avgB
    tfcd.write(str(0.001))
else:
    print 'High light', avgB
    tfcd.write(str(0.005))
Parameters:
  • sortedimglist (list) – The images to be analyzed
  • impg (list) – Groups of images on which to run the analysis.
  • scrdir (string) – The top level directory of the analysis.
getmaskslist(srcdir)

Create a sorted list of generated masks.

This method will create a list of all the images present in srcdir/MasksResults. Theses images are the black and white masks created by the foxmask.Imagesanalysis.bgfgestimation() method. Before creating the list, masks with the prefix EstBG are removed.

Parameters:srcdir (str) – Top level directory containing directories of images.
Returns:maskslist. All masks to analyze.
Return type:list
masks_analysis()

Analyze masks to detect moving objects

This method will analyze created masks, taking the masks list to analyze from foxmask.Imagesanalysis.getmaskslist(). The area of all white objects in each masks are calculated. If the area is smaller than parameters.minsize, the object is not considered as an animal.

Returns:resultslist. Results of the masks analysis.
Return type:list
writeresults(item, resultsdir)

Write results of the mask analysis to file

Parameters:
  • item (str) – The name of the class analyzed, representing the name of the folder analyzed. This is used to name the table in which the results will be written.
  • resultsdir (str) – The top directory where to write the final results.