Objects recognition

This notebooks shows how to detect objects quickly using cvlib and the YOLOv4 model. This library detects faces, people, and several inanimate objects; we currently have restricted the output to person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, cell phone.

The first cell is only run on google colab and installs the ammico package.

After that, we can import ammico and read in the files given a folder path.

[1]:
# if running on google colab
# flake8-noqa-cell
import os

if "google.colab" in str(get_ipython()):
    # update python version
    # install setuptools
    # %pip install setuptools==61 -qqq
    # install ammico
    %pip install git+https://github.com/ssciwr/ammico.git -qqq
    # mount google drive for data and API key
    from google.colab import drive

    drive.mount("/content/drive")
[2]:
import ammico
from ammico import utils as mutils
from ammico import display as mdisplay
import ammico.objects as ob

Set an image path as input file path.

[3]:
# Here you need to provide the path to your google drive folder
# or local folder containing the images
images = mutils.find_files(
    path="data/",
    limit=10,
)
[4]:
mydict = mutils.initialize_dict(images)

Detect objects and directly write to csv

You can directly carry out the analysis and export the result into a csv. This may take a while depending on how many images you have loaded.

[5]:
for key in mydict:
    mydict[key] = ob.ObjectDetector(mydict[key]).analyse_image()
  0% |                                                                        |
Downloading yolov4.cfg from https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg
Downloading yolov4.weights from https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
 99% |####################################################################### |
Downloading yolov3_classes.txt from https://github.com/arunponnusamy/object-detection-opencv/raw/master/yolov3.txt
100% |                                                                        |

Convert the dictionary of dictionarys into a dictionary with lists:

[6]:
outdict = mutils.append_data_to_dict(mydict)
df = mutils.dump_df(outdict)

Check the dataframe:

[7]:
df.head(10)
[7]:
filename person bicycle car motorcycle airplane bus train truck boat traffic light cell phone
0 data/106349S_por.png yes no no no no no no no no no yes
1 data/102141_2_eng.png yes no no no no no no no no no no
2 data/102730_eng.png yes no no no no no no yes no no no

Write the csv file:

[8]:
df.to_csv("data_out.csv")

Manually inspect what was detected

To check the analysis, you can inspect the analyzed elements here. Loading the results takes a moment, so please be patient. If you are sure of what you are doing, you can directly export a csv file in the step above. Here, we display the object detection results provided by the above library. Click on the tabs to see the results in the right sidebar. You may need to increment the port number if you are already running several notebook instances on the same server.

[9]:
analysis_explorer = mdisplay.AnalysisExplorer(mydict, identify="objects")
analysis_explorer.run_server(port=8056)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 analysis_explorer = mdisplay.AnalysisExplorer(mydict, identify="objects")
      2 analysis_explorer.run_server(port=8056)

TypeError: __init__() got an unexpected keyword argument 'identify'
[ ]: