agentmorris / MegaDetector

MegaDetector is an AI model that helps conservation folks spend less time doing boring things with camera trap images.
MIT License
117 stars 26 forks source link

JSON file retaining images below set threshold #66

Closed agentmorris closed 1 year ago

agentmorris commented 1 year ago

Hi,

I have been trying to process camera trap data using Mega Detector python code.

For detection my code is:

python detection\run_detector_batch.py "c:\megadetector\md_v4.1.0.pb" "c:\original_image_folder" "c:\megadetector\test_output.json" --output_relative_filenames --recursive --threshold 0.25 --checkpoint_frequency 10000

For visualization my code is: python visualization\visualize_detector_output.py "c:\megadetector\test_output.json" "c:\Box" --confidence 0.25 --images_dir "c:\original_image_folder"

The resultant JSON file as well as the visualization image folder are retaining all images, including the ones below the set threshold of 0.25.

Is this an issue with the code? Any help will be most appreciated.

Thanks


Issue cloned from Microsoft/CameraTraps, original issue posted by Divj-G on Jul 12, 2022.

agentmorris commented 1 year ago

If you only want to render images with above-threshold detections, I think you are looking for the "render_detections_only" option to visualize_detector_output.

FYI we would typically recommend only using this script (visualize_detector_output.py) for quickly previewing how MegaDetector is working, and in that case the blanks are as important as the detections. If you have a threshold you are satisfied with and you want to separate blanks from non-blanks "in production", I would recommend separate_detections_into_folders.py.

Hope that helps!


(Comment originally posted by agentmorris)

agentmorris commented 1 year ago

Thank you, this will do just fine!

However, I have not been able to correctly figure out the function arguments using 'separate_detections_into_folders.py' to get it to start working on my JSON files. Requesting to please send the code structure for this function.

Thank you very much again, Divyajyoti

On Tue, 12 Jul 2022 at 15:53, Dan Morris @.***> wrote:

If you only want to render images with above-threshold detections, I think you are looking for the "render_detections_only" option to visualize_detector_output.

FYI we would typically recommend only using this script (visualize_detector_output.py) for quickly previewing how MegaDetector is working, and in that case the blanks are as important as the detections. If you have a threshold you are satisfied with and you want to separate blanks from non-blanks "in production", I would recommend separate_detections_into_folders.py.

Hope that helps!

— Reply to this email directly, view it on GitHub https://github.com/microsoft/CameraTraps/issues/301#issuecomment-1181587561, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2AV2HBV3YUOIHC4AAQDKQLVTVBQTANCNFSM53KIX5GQ . You are receiving this because you authored the thread.Message ID: @.***>

-- Divyajyoti Ganguly (he/him), PG program in Wildlife Biology and Conservation, National Centre for Biological Sciences, Bangalore, India


(Comment originally posted by Divj-G)

agentmorris commented 1 year ago

For example, if your MegaDetector results file (the result of running run_detector_batch.py) is located at:

c:\megadetector_results\my_results.json

And your images are in:

c:\my_images

And you want to create the "separated" image folder in:

c:\separated_images

...with a threshold of 0.15 (that's a typical threshold for MDv5, for MDv4 you would use a higher threshold), you would run:

python separate_detections_into_folders.py "c:\megadetector_results\my_results.json" "c:\my_images" "c:\separated_images" --animal_threshold 0.15 --human_threshold 0.15 --vehicle_threshold 0.15

The following option may also make things go a little faster by doing multiple copies at once:

--n_threads 4

Hope that helps!

-Dan


(Comment originally posted by agentmorris)

agentmorris commented 1 year ago

Thank you!

I have run into another error now. The following error shows up when trying to execute separate_detections_into_folders.py:

python: can't open file 'C:\Users\MFS\GitHub\CameraTraps\api\batch_processing\separate_detections_into_folders.py': [Errno 2] No such file or directory

I can see the file in the designated folder and am not sure why this error keeps coming up. I tried running this in the three environments that are available to me: base, cameratraps (this is where the Git repo was cloned) and cameratraps-detector

On Wed, 3 Aug 2022 at 19:43, Dan Morris @.***> wrote:

For example, if your MegaDetector results file (the result of running run_detector_batch.py) is located at:

c:\megadetector_results\my_results.json

And your images are in:

c:\my_images

And you want to create the "separated" image folder in:

c:\separated_images

...with a threshold of 0.15 (that's a typical threshold for MDv5, for MDv4 you would use a higher threshold), you would run:

python separate_detections_into_folders.py "c:\megadetector_results\my_results.json" "c:\my_images" "c:\separated_images" --animal_threshold 0.15 --human_threshold 0.15 --vehicle_threshold 0.15

The following option may also make things go a little faster by doing multiple copies at once:

--n_threads 4

Hope that helps!

-Dan

— Reply to this email directly, view it on GitHub https://github.com/microsoft/CameraTraps/issues/301#issuecomment-1204006660, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2AV2HA5KTM7DWETYQ675STVXJ5BLANCNFSM53KIX5GQ . You are receiving this because you authored the thread.Message ID: @.***>

-- Divyajyoti Ganguly (he/him), PG program in Wildlife Biology and Conservation, National Centre for Biological Sciences, Bangalore, India


(Comment originally posted by Divj-G)

agentmorris commented 1 year ago

I figured out my mistake with this one and separate_detections_into_folders.py is up and running!

One more question, the output images do not have the bounding boxes. Is it possible to add another argument to the code to have the bounding boxes as well.

Best, Divyajyoti

On Thu, 4 Aug 2022 at 15:27, Divyajyoti Ganguly < @.***> wrote:

Thank you!

I have run into another error now. The following error shows up when trying to execute separate_detections_into_folders.py:

python: can't open file 'C:\Users\MFS\GitHub\CameraTraps\api\batch_processing\separate_detections_into_folders.py': [Errno 2] No such file or directory

I can see the file in the designated folder and am not sure why this error keeps coming up. I tried running this in the three environments that are available to me: base, cameratraps (this is where the Git repo was cloned) and cameratraps-detector

On Wed, 3 Aug 2022 at 19:43, Dan Morris @.***> wrote:

For example, if your MegaDetector results file (the result of running run_detector_batch.py) is located at:

c:\megadetector_results\my_results.json

And your images are in:

c:\my_images

And you want to create the "separated" image folder in:

c:\separated_images

...with a threshold of 0.15 (that's a typical threshold for MDv5, for MDv4 you would use a higher threshold), you would run:

python separate_detections_into_folders.py "c:\megadetector_results\my_results.json" "c:\my_images" "c:\separated_images" --animal_threshold 0.15 --human_threshold 0.15 --vehicle_threshold 0.15

The following option may also make things go a little faster by doing multiple copies at once:

--n_threads 4

Hope that helps!

-Dan

— Reply to this email directly, view it on GitHub https://github.com/microsoft/CameraTraps/issues/301#issuecomment-1204006660, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2AV2HA5KTM7DWETYQ675STVXJ5BLANCNFSM53KIX5GQ . You are receiving this because you authored the thread.Message ID: @.***>

-- Divyajyoti Ganguly (he/him), PG program in Wildlife Biology and Conservation, National Centre for Biological Sciences, Bangalore, India

-- Divyajyoti Ganguly (he/him), PG program in Wildlife Biology and Conservation, National Centre for Biological Sciences, Bangalore, India


(Comment originally posted by Divj-G)

agentmorris commented 1 year ago

Sorry, we don't have a script that will both (a) render bounding boxes and (b) preserve folder structure.

Typically bounding boxes are only rendered as a test/debug step, i.e. to get a subjective sense for how well MegaDetector is working and tinker with confidence thresholds, for which postprocess_batch_results.py is the most typical approach. That script produces a "preview" page with images, but they are downsized and renamed, and not intended for "production" image review.

For "production" image review, nearly all users either (a) load the .json results file into a tool like Timelapse (which renders bounding boxes) or (b) use separate_detections_into_folders.py as a way of maintaining their folder structure but eliminating lots of images (typically blanks).


(Comment originally posted by agentmorris)

agentmorris commented 1 year ago

This feature (rendering bounding boxes) has been added to separate_detections_into_folders.py, via the --render_boxes option.

Note that because this is no longer a simple copy operation, if you specify --render_boxes, the script makes a best effort to copy EXIF metadata from the source images, but we can't guarantee that EXIF data is perfectly preserved, and the script makes no attempt to copy IPTC or XMP metadata. There's no easy way around this; once we're rendering bounding boxes, we're no longer just adding something to the images, we're generating new images and trying to copy metadata over.

But relative paths are still preserved and almost everything we've seen people store in camera trap data is in EXIF metadata, so I don't think the risk of metadata loss will be an issue for the use cases for which folks have requested this.

Let us know if anything looks off!


(Comment originally posted by agentmorris)