agentmorris / MegaDetector

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

Crop MegaDetector json #40

Closed agentmorris closed 1 year ago

agentmorris commented 1 year ago

Hi, I've just started using megadetector and I was wondering if you could recommend any good code to crop my images using the boxes identified? Thanks!


Issue cloned from Microsoft/CameraTraps, original issue posted by PatWright on Dec 16, 2020.

agentmorris commented 1 year ago

Try this?

https://github.com/microsoft/CameraTraps/blob/master/classification/crop_detections.py

Let us know how it goes!

-Dan


(Comment originally posted by agentmorris)

agentmorris commented 1 year ago

Thanks. I didn't realise there was one available here. I'm fairly new with python and seem to be doing something wrong with crop_detections. This path is creating an error I think ('E:/Other_projects/AI_Cameratrapping/Test_crop\E:'), but I'm not sure where my code is wrong.

Thanks again!

`python CameraTraps/detection/run_tf_detector_batch.py E:/Other_projects/AI_Cameratrapping/md_v4.1.0.pb E:/Other_projects/AI_Cameratrapping/Bushnell_13_b/ E:/Other_projects/AI_Cameratrapping/Test_file.json #seems to work fine

python CameraTraps/classification/crop_detections.py E:/Other_projects/AI_Cameratrapping/Test_file.json E:/Other_projects/AI_Cameratrapping/Test_crop --detector-version "4.1" --images-dir E:/Other_projects/AI_Cameratrapping/Bushnell_13_b --threshold 0.8 --save-full-images --square-crops --threads 50 --logdir E:/Other_projects/AI_Cameratrapping Getting bbox info for 57 images... 100%|███████████████████████████████████████████████████████████████████████████████████| 57/57 [00:00<00:00, 2590.76it/s] Reading/downloading 57 images and cropping... E:/Other_projects/AI_Cameratrapping/Bushnell_13_b\EM_site13_Bu_b (41).JPG - generated OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'E:/Other_projects/AI_Cameratrapping/Test_crop\E:' E:/Other_projects/AI_Cameratrapping/Bushnell_13_b\EM_site13_Bu_b (14).JPG - generated OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'E:/Other_projects/AI_Cameratrapping/Test_crop\E:' E:/Other_projects/AI_Cameratrapping/Bushnell_13_b\EM_site13_Bu_b (7).JPG - generated OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'E:/Other_projects/AI_Cameratrapping/Test_crop\E:' E:/Other_projects/AI_Cameratrapping/Bushnell_13_b\EM_site13_Bu_b (53).JPG - generated OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'E:/Other_projects/AI_Cameratrapping/Test_crop\E:' E:/Other_projects/AI_Cameratrapping/Bushnell_13_b\EM_site13_Bu_b (5).JPG - generated OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'E:/Other_projects/AI_Cameratrapping/Test_crop\E:' E:/Other_projects/AI_Cameratrapping/Bushnell_13_b\EM_site13_Bu_b (55).JPG - generated OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'E:/Other_projects/AI_Cameratrapping/Test_crop\E:' 100%|████████████████████████████████████████████████████████████████████████████████████| 57/57 [00:00<00:00, 197.00it/s] Downloaded 0 images. Made 0 new crops. 6 images failed to download or crop.`


(Comment originally posted by PatWright)

agentmorris commented 1 year ago

I think the problem is that when you do this:

python CameraTraps/detection/run_tf_detector_batch.py E:/Other_projects/AI_Cameratrapping/md_v4.1.0.pb E:/Other_projects/AI_Cameratrapping/Bushnell_13_b/ E:/Other_projects/AI_Cameratrapping/Test_file.json

...the .json file you get has absolute paths in it, and when you run the second command, it's trying to append those paths to the folder you specified for image_dir:

E:/Other_projects/AI_Cameratrapping/Bushnell_13_b/

...which gives you paths that aren't quite right, like this:

E:/Other_projects/AI_Cameratrapping/Bushnell_13_b/E:/Other_projects/AI_Cameratrapping/Bushnell_13_b/image001.jpg

The solution is to add --output_relative_filenames to your invocation of run_tf_detector_batch, like this:

python CameraTraps/detection/run_tf_detector_batch.py E:/Other_projects/AI_Cameratrapping/md_v4.1.0.pb E:/Other_projects/AI_Cameratrapping/Bushnell_13_b/ E:/Other_projects/AI_Cameratrapping/Test_file.json --output_relative_filenames

Let us know how that goes!


(Comment originally posted by agentmorris)

agentmorris commented 1 year ago

That works! Thanks.


(Comment originally posted by PatWright)

agentmorris commented 1 year ago

Glad to hear it... closing this issue, drop us a line (cameratraps@microsoft.com) if you get a chance to let us know how you're using MegaDetector, or share at aiforconservation.slack.com . Thanks!


(Comment originally posted by agentmorris)