Skip to main content

CamTrapML is a Python library for Detecting, Classifying, and Analysing Wildlife Camera Trap Imagery.

Project description

CamTrapML

CamTrapML is a Python library for Detecting, Classifying, and Analysing Wildlife Camera Trap Imagery.

Installation

$ pip install camtrapml

Features

Loading Data

Search for images in a directory, load an image and create a thumbnail.

%load_ext autoreload
%autoreload

from camtrapml.dataset import ImageDataset
from camtrapml.image.utils import load_image, thumbnail

imageset = ImageDataset(
    name="Test Images",
    path="test/fixtures/images",
)

image_paths = list(imageset.enumerate_images())

thumbnail(load_image(image_paths[0]))

EXIF Extraction

EXIF extraction is a common task in gathering the metadata such as each image's timestamp, camera model, focal length, etc. Some researchers write labelling into the EXIF data. CamTrapML doesn't assist with writing to EXIF. However, there is functionality for extracting it for analysis and building datasets for training new models from previously labelled images.

ExifTool is required for this package to work. Installation instructions can be found here.

Three methods are available for extracting EXIF data from images. Each with different performance characteristics.

Method 1: Individual Images

from camtrapml.image.exif import extract_exif

exif = extract_exif(image_paths[0])
exif

Method 2: Multiple Images

extract_multiple_exif passes a list of image paths to ExifTool and returns a list of dictionaries containing the EXIF data. This is faster than extract_exif when multiple images are being processed as it only passes the list of image paths to ExifTool once, rather than spawning a new process for each image.

from camtrapml.image.exif import extract_multiple_exif

exif = extract_multiple_exif(image_paths)
exif[0]

Method 3: Multiple Images, Multiple Processes

When processing large datasets, it's apparent that the bottleneck in extracting the EXIF information tends to be the CPU. This method spawns multiple versions of ExifTool in parallel, each with a batch of image paths. This is faster than extract_multiple_exif when processing large datasets as it allows for multiple processes to be spawned and the data extracted in parallel.

from camtrapml.image.exif import extract_multiple_exif_fast

exif = extract_multiple_exif_fast(image_paths)
exif[0]

Detection

Various Detection models are available in the camtrapml.detection subpackage. These currently include MegaDetector (v4.1, v3 and v2) and support for loading in custom Tensorflow v1.x Object Detection Frozen models.

Detection with MegaDetector v4.1

from camtrapml.detection.models.megadetector import MegaDetectorV4_1
from camtrapml.detection.utils import render_detections

with MegaDetectorV4_1() as detector:
    detections = detector.detect(image_paths[0])

thumbnail(
    render_detections(image_paths[0], detections, class_map=detector.class_map)
)

Detection with a custom Tensorflow v1.x Object Detection Frozen model

!cp ~/.camtrapml/models/megadetector/v4.1.0/md_v4.1.0.pb example-custom-model.pb

from camtrapml.detection.models.tensorflow import TF1ODAPIFrozenModel
from camtrapml.detection.utils import render_detections
from pathlib import Path

with TF1ODAPIFrozenModel(
    model_path=Path("example-custom-model.pb").expanduser(),
    class_map={
        1: "animal",
    },
) as detector:
    detections = detector.detect(image_paths[1])

thumbnail(
    render_detections(image_paths[1], detections, class_map=detector.class_map)
)

Extract Detections

from camtrapml.detection.models.megadetector import MegaDetectorV4_1
from camtrapml.detection.utils import extract_detections_from_image

with MegaDetectorV4_1() as detector:
    detections = detector.detect(image_paths[0])

list(extract_detections_from_image(load_image(image_paths[0]), detections))[0]

Remove Humans from Images

In order to reduce the risks of identification of humans in line with GDPR, CamTrapML provides the ability to remove humans from images. This is achieved by using the MegaDetector v3+ models to detect humans in the image, and then replacing all pixels in each human detection.

from camtrapml.detection.models.megadetector import MegaDetectorV4_1
from camtrapml.detection.utils import remove_detections_from_image
from camtrapml.image.utils import load_image, thumbnail
from pathlib import Path

ct_image_with_humans = Path("test/fixtures/human_images/IMG_0254.JPG").expanduser()

with MegaDetectorV4_1() as detector:
    detections = detector.detect(ct_image_with_humans)

human_class_id = 2

thumbnail(
    remove_detections_from_image(
        load_image(ct_image_with_humans),
        [
            detection
            for detection in detections
            if detection["category"] == human_class_id and detection["conf"] > 0.5
        ],
    )
)


          

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

camtrapml-0.4.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

camtrapml-0.4.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file camtrapml-0.4.1.tar.gz.

File metadata

  • Download URL: camtrapml-0.4.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.13 Linux/5.15.0-48-generic

File hashes

Hashes for camtrapml-0.4.1.tar.gz
Algorithm Hash digest
SHA256 51140b5828cf373623962e9f2c3c1ab29744ec4960dbc2974374fc2413c4f1c2
MD5 90c5ce74418e434d33bedb3c3a517a85
BLAKE2b-256 8e8f3278410978725316004ff0e1adb077d8f60365863a1a97171a1b3f0174a3

See more details on using hashes here.

File details

Details for the file camtrapml-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: camtrapml-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.13 Linux/5.15.0-48-generic

File hashes

Hashes for camtrapml-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1cff04303b0c66cfd3371b3432363d96540aec01fb8373e17f29cc2d3893a66
MD5 635ff63d14ea0549665f6b576041c8ca
BLAKE2b-256 ccb24e899aab88d022f62e53cafcb18ad8c528719431d673a72bcbae21a1cda2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page