-
- -
-

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()
-
-
-
-
-
-
-
-
-[ WARN:0@10.875] global loadsave.cpp:248 findDecoder imread_('102141_2_eng'): can't open/read file: check file path/integrity
-
-
-
-
-
-
-
----------------------------------------------------------------------------
-AttributeError                            Traceback (most recent call last)
-Cell In[5], line 2
-      1 for key in mydict:
-----> 2     mydict[key] = ob.ObjectDetector(mydict[key]).analyse_image()
-
-File ~/work/AMMICO/AMMICO/ammico/objects.py:48, in ObjectDetector.analyse_image(self)
-     42 def analyse_image(self):
-     43     """Perform object detection on the image.
-     44
-     45     Returns:
-     46         dict: The updated dictionary with object detection results.
-     47     """
----> 48     self.subdict = ObjectDetector.od_client.analyse_image(self.subdict)
-     49     return self.subdict
-
-File ~/work/AMMICO/AMMICO/ammico/objects.py:24, in ObjectDetectorClient.analyse_image(self, subdict)
-     15 def analyse_image(self, subdict=None):
-     16     """Localize objects in the given image.
-     17
-     18     Args:
-   (...)
-     22         dict: The updated dictionary with object detection results.
-     23     """
----> 24     return self.detector.analyse_image(subdict)
-
-File ~/work/AMMICO/AMMICO/ammico/objects_cvlib.py:73, in ObjectCVLib.analyse_image(self, subdict)
-     67 def analyse_image(self, subdict):
-     68     """Localize objects in the local image.
-     69
-     70     Args:
-     71         subdict: The dictionary for an image expression instance.
-     72     """
----> 73     objects = self.analyse_image_from_file(subdict["filename"])
-     74     for key in objects:
-     75         subdict[key] = objects[key]
-
-File ~/work/AMMICO/AMMICO/ammico/objects_cvlib.py:64, in ObjectCVLib.analyse_image_from_file(self, image_path)
-     58 def analyse_image_from_file(self, image_path):
-     59     """Localize objects in the local image.
-     60
-     61     Args:
-     62         image_path: The path to the local file.
-     63     """
----> 64     objects = self.detect_objects_cvlib(image_path)
-     65     return objects
-
-File ~/work/AMMICO/AMMICO/ammico/objects_cvlib.py:54, in ObjectCVLib.detect_objects_cvlib(self, image_path)
-     47 """Localize objects in the local image.
-     48
-     49 Args:
-     50     image_path: The path to the local file.
-     51 """
-     52 img = cv2.imread(image_path)
----> 54 _, label, _ = cv.detect_common_objects(img)
-     55 objects = objects_from_cvlib(label)
-     56 return objects
-
-File /opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/cvlib/object_detection.py:77, in detect_common_objects(image, confidence, nms_thresh, model, enable_gpu)
-     66 def detect_common_objects(image, confidence=0.5, nms_thresh=0.3, model='yolov4', enable_gpu=False):
-     67     """A method to detect common objects
-     68     Args:
-     69         image: A colour image in a numpy array
-   (...)
-     74
-     75     """
----> 77     Height, Width = image.shape[:2]
-     78     scale = 0.00392
-     80     global classes
-
-AttributeError: 'NoneType' object has no attribute 'shape'
-
-
-

Convert the dictionary of dictionarys into a dictionary with lists:

-
-
[6]:
-
-
-
outdict = mutils.append_data_to_dict(mydict)
-df = mutils.dump_df(outdict)
-
-
-
-
-
-
-
-
----------------------------------------------------------------------------
-ValueError                                Traceback (most recent call last)
-Cell In[6], line 2
-      1 outdict = mutils.append_data_to_dict(mydict)
-----> 2 df = mutils.dump_df(outdict)
-
-File ~/work/AMMICO/AMMICO/ammico/utils.py:222, in dump_df(mydict)
-    220 def dump_df(mydict: dict) -> DataFrame:
-    221     """Utility to dump the dictionary into a dataframe."""
---> 222     return DataFrame.from_dict(mydict)
-
-File /opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/pandas/core/frame.py:1816, in DataFrame.from_dict(cls, data, orient, dtype, columns)
-   1810     raise ValueError(
-   1811         f"Expected 'index', 'columns' or 'tight' for orient parameter. "
-   1812         f"Got '{orient}' instead"
-   1813     )
-   1815 if orient != "tight":
--> 1816     return cls(data, index=index, columns=columns, dtype=dtype)
-   1817 else:
-   1818     realdata = data["data"]
-
-File /opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/pandas/core/frame.py:736, in DataFrame.__init__(self, data, index, columns, dtype, copy)
-    730     mgr = self._init_mgr(
-    731         data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
-    732     )
-    734 elif isinstance(data, dict):
-    735     # GH#38939 de facto copy defaults to False only in non-dict cases
---> 736     mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
-    737 elif isinstance(data, ma.MaskedArray):
-    738     from numpy.ma import mrecords
-
-File /opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/pandas/core/internals/construction.py:503, in dict_to_mgr(data, index, columns, dtype, typ, copy)
-    499     else:
-    500         # dtype check to exclude e.g. range objects, scalars
-    501         arrays = [x.copy() if hasattr(x, "dtype") else x for x in arrays]
---> 503 return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
-
-File /opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/pandas/core/internals/construction.py:114, in arrays_to_mgr(arrays, columns, index, dtype, verify_integrity, typ, consolidate)
-    111 if verify_integrity:
-    112     # figure out the index, if necessary
-    113     if index is None:
---> 114         index = _extract_index(arrays)
-    115     else:
-    116         index = ensure_index(index)
-
-File /opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/pandas/core/internals/construction.py:677, in _extract_index(data)
-    675 lengths = list(set(raw_lengths))
-    676 if len(lengths) > 1:
---> 677     raise ValueError("All arrays must be of the same length")
-    679 if have_dicts:
-    680     raise ValueError(
-    681         "Mixing dicts with non-Series may lead to ambiguous ordering."
-    682     )
-
-ValueError: All arrays must be of the same length
-
-
-

Check the dataframe:

-
-
[7]:
-
-
-
df.head(10)
-
-
-
-
-
-
-
-
----------------------------------------------------------------------------
-NameError                                 Traceback (most recent call last)
-Cell In[7], line 1
-----> 1 df.head(10)
-
-NameError: name 'df' is not defined
-
-
-

Write the csv file:

-
-
[8]:
-
-
-
df.to_csv("data_out.csv")
-
-
-
-
-
-
-
-
----------------------------------------------------------------------------
-NameError                                 Traceback (most recent call last)
-Cell In[8], line 1
-----> 1 df.to_csv("data_out.csv")
-
-NameError: name 'df' is not defined
-
-
-
-
-

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'
-
-
-
-
[ ]:
-
-
-

-
-
-
-
-
- - -
-