Objects Expression recognition

This notebooks shows some preliminary work on detecting objects expressions with cvlib. It is mainly meant to explore its capabilities and to decide on future research directions. We package our code into a misinformation package that is imported here:

[1]:
import misinformation
from misinformation import utils as mutils
from misinformation import display as mdisplay
import misinformation.objects as ob

Set an image path as input file path.

[2]:
images = mutils.find_files(
    path="data/",
    limit=10,
)
[3]:
mydict = mutils.initialize_dict(images)

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.

[4]:
mdisplay.explore_analysis(mydict, identify="objects")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[4], line 1
----> 1 mdisplay.explore_analysis(mydict, identify="objects")

File ~/work/AMMICO/AMMICO/misinformation/display.py:72, in explore_analysis(mydict, identify)
     70 # Register the handler and trigger it immediately
     71 image_select.observe(switch, names=("value",), type="change")
---> 72 switch(None)
     74 # Show the combined widget
     75 return ipywidgets.HBox([image_select, image_widget, output])

File ~/work/AMMICO/AMMICO/misinformation/display.py:64, in explore_analysis.<locals>.switch(_)
     61 # This output widget absorbes print statements that are messing with
     62 # the widget output and cannot be disabled through the API.
     63 with faces.NocatchOutput():
---> 64     mydict[image_select.value] = identify_dict[identify](
     65         mydict[image_select.value]
     66     ).analyse_image()
     67 with output:
     68     display(JSONContainer(mydict[image_select.value]))

File ~/work/AMMICO/AMMICO/misinformation/objects.py:36, in ObjectDetector.analyse_image(self)
     35 def analyse_image(self):
---> 36     self.subdict = ObjectDetector.od_client.analyse_image(self.subdict)
     37     return self.subdict

File ~/work/AMMICO/AMMICO/misinformation/objects.py:22, in ObjectDetectorClient.analyse_image(self, subdict)
     15 def analyse_image(self, subdict=None):
     16     """Localize objects in the local image.
     17
     18     Args:
     19     subdict: The dictionary for an image expression instance.
     20     """
---> 22     return self.detector.analyse_image(subdict)

File ~/work/AMMICO/AMMICO/misinformation/objects_cvlib.py:79, in ObjectCVLib.analyse_image(self, subdict)
     73 def analyse_image(self, subdict):
     74     """Localize objects in the local image.
     75
     76     Args:
     77     subdict: The dictionary for an image expression instance.
     78     """
---> 79     objects = self.analyse_image_from_file(subdict["filename"])
     80     for key in objects:
     81         subdict[key] = objects[key]

File ~/work/AMMICO/AMMICO/misinformation/objects_cvlib.py:70, in ObjectCVLib.analyse_image_from_file(self, image_path)
     64 def analyse_image_from_file(self, image_path):
     65     """Localize objects in the local image.
     66
     67     Args:
     68     image_path: The path to the local file.
     69     """
---> 70     objects = self.detect_objects_cvlib(image_path)
     71     return objects

File ~/work/AMMICO/AMMICO/misinformation/objects_cvlib.py:59, in ObjectCVLib.detect_objects_cvlib(self, image_path)
     54 img = cv2.imread(image_path)
     55 # preimg = Image.open(image_path).convert("RGB")
     56 # preimg2 = np.asarray(preimg)
     57 # img = cv2.cvtColor(preimg2, cv2.COLOR_BGR2RGB)
---> 59 _, label, _ = cv.detect_common_objects(img)
     60 # output_image = draw_bbox(im, bbox, label, conf)
     61 objects = objects_from_cvlib(label)

File /opt/hostedtoolcache/Python/3.9.16/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'

Detect objects and directly write to csv

[5]:
for key in mydict:
    mydict[key] = ob.ObjectDetector(mydict[key]).analyse_image()
libpng error: bad parameters to zlib
---------------------------------------------------------------------------
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/misinformation/objects.py:36, in ObjectDetector.analyse_image(self)
     35 def analyse_image(self):
---> 36     self.subdict = ObjectDetector.od_client.analyse_image(self.subdict)
     37     return self.subdict

File ~/work/AMMICO/AMMICO/misinformation/objects.py:22, in ObjectDetectorClient.analyse_image(self, subdict)
     15 def analyse_image(self, subdict=None):
     16     """Localize objects in the local image.
     17
     18     Args:
     19     subdict: The dictionary for an image expression instance.
     20     """
---> 22     return self.detector.analyse_image(subdict)

File ~/work/AMMICO/AMMICO/misinformation/objects_cvlib.py:79, in ObjectCVLib.analyse_image(self, subdict)
     73 def analyse_image(self, subdict):
     74     """Localize objects in the local image.
     75
     76     Args:
     77     subdict: The dictionary for an image expression instance.
     78     """
---> 79     objects = self.analyse_image_from_file(subdict["filename"])
     80     for key in objects:
     81         subdict[key] = objects[key]

File ~/work/AMMICO/AMMICO/misinformation/objects_cvlib.py:70, in ObjectCVLib.analyse_image_from_file(self, image_path)
     64 def analyse_image_from_file(self, image_path):
     65     """Localize objects in the local image.
     66
     67     Args:
     68     image_path: The path to the local file.
     69     """
---> 70     objects = self.detect_objects_cvlib(image_path)
     71     return objects

File ~/work/AMMICO/AMMICO/misinformation/objects_cvlib.py:59, in ObjectCVLib.detect_objects_cvlib(self, image_path)
     54 img = cv2.imread(image_path)
     55 # preimg = Image.open(image_path).convert("RGB")
     56 # preimg2 = np.asarray(preimg)
     57 # img = cv2.cvtColor(preimg2, cv2.COLOR_BGR2RGB)
---> 59 _, label, _ = cv.detect_common_objects(img)
     60 # output_image = draw_bbox(im, bbox, label, conf)
     61 objects = objects_from_cvlib(label)

File /opt/hostedtoolcache/Python/3.9.16/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/misinformation/utils.py:99, in dump_df(mydict)
     97 def dump_df(mydict: dict) -> DataFrame:
     98     """Utility to dump the dictionary into a dataframe."""
---> 99     return DataFrame.from_dict(mydict)

File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/pandas/core/frame.py:1764, in DataFrame.from_dict(cls, data, orient, dtype, columns)
   1758     raise ValueError(
   1759         f"Expected 'index', 'columns' or 'tight' for orient parameter. "
   1760         f"Got '{orient}' instead"
   1761     )
   1763 if orient != "tight":
-> 1764     return cls(data, index=index, columns=columns, dtype=dtype)
   1765 else:
   1766     realdata = data["data"]

File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/pandas/core/frame.py:664, in DataFrame.__init__(self, data, index, columns, dtype, copy)
    658     mgr = self._init_mgr(
    659         data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
    660     )
    662 elif isinstance(data, dict):
    663     # GH#38939 de facto copy defaults to False only in non-dict cases
--> 664     mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
    665 elif isinstance(data, ma.MaskedArray):
    666     import numpy.ma.mrecords as mrecords

File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/pandas/core/internals/construction.py:493, in dict_to_mgr(data, index, columns, dtype, typ, copy)
    489     else:
    490         # dtype check to exclude e.g. range objects, scalars
    491         arrays = [x.copy() if hasattr(x, "dtype") else x for x in arrays]
--> 493 return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)

File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/pandas/core/internals/construction.py:118, in arrays_to_mgr(arrays, columns, index, dtype, verify_integrity, typ, consolidate)
    115 if verify_integrity:
    116     # figure out the index, if necessary
    117     if index is None:
--> 118         index = _extract_index(arrays)
    119     else:
    120         index = ensure_index(index)

File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/pandas/core/internals/construction.py:666, in _extract_index(data)
    664 lengths = list(set(raw_lengths))
    665 if len(lengths) > 1:
--> 666     raise ValueError("All arrays must be of the same length")
    668 if have_dicts:
    669     raise ValueError(
    670         "Mixing dicts with non-Series may lead to ambiguous ordering."
    671     )

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