{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objects Expression recognition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# if running on google colab\n", "# flake8-noqa-cell\n", "import os\n", "\n", "if \"google.colab\" in str(get_ipython()):\n", " # update python version\n", " # install setuptools\n", " !pip install setuptools==61 -qqq\n", " # install misinformation\n", " !pip install git+https://github.com/ssciwr/misinformation.git -qqq\n", " # mount google drive for data and API key\n", " from google.colab import drive\n", "\n", " drive.mount(\"/content/drive\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import misinformation\n", "from misinformation import utils as mutils\n", "from misinformation import display as mdisplay\n", "import misinformation.objects as ob" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set an image path as input file path." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Here you need to provide the path to your google drive folder\n", "# or local folder containing the images\n", "images = mutils.find_files(\n", " path=\"/content/drive/MyDrive/misinformation-data/\",\n", " limit=10,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mydict = mutils.initialize_dict(images)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Detect objects and directly write to csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key in mydict:\n", " mydict[key] = ob.ObjectDetector(mydict[key]).analyse_image()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert the dictionary of dictionarys into a dictionary with lists:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outdict = mutils.append_data_to_dict(mydict)\n", "df = mutils.dump_df(outdict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check the dataframe:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.head(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the csv file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_csv(\"./data_out.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Manually inspect what was detected\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mdisplay.explore_analysis(mydict, identify=\"objects\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def localize_objects(path):\n", " \"\"\"Localize objects in the local image.\n", "\n", " Args:\n", " path: The path to the local file.\n", " \"\"\"\n", " from google.cloud import vision\n", "\n", " client = vision.ImageAnnotatorClient()\n", "\n", " with open(path, \"rb\") as image_file:\n", " content = image_file.read()\n", " image = vision.Image(content=content)\n", "\n", " objects = client.object_localization(image=image).localized_object_annotations\n", "\n", " print(\"Number of objects found: {}\".format(len(objects)))\n", " for object_ in objects:\n", " print(\"\\n{} (confidence: {})\".format(object_.name, object_.score))\n", " print(\"Normalized bounding polygon vertices: \")\n", " for vertex in object_.bounding_poly.normalized_vertices:\n", " print(\" - ({}, {})\".format(vertex.x, vertex.y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.environ[\n", " \"GOOGLE_APPLICATION_CREDENTIALS\"\n", "] = \"../../misinformation-notes/seismic-bonfire-329406-412821a70264.json\"\n", "localize_objects(\"/home/iulusoy/Desktop/102141_2_eng.png\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.5" }, "vscode": { "interpreter": { "hash": "f1142466f556ab37fe2d38e2897a16796906208adb09fea90ba58bdf8a56f0ba" } } }, "nbformat": 4, "nbformat_minor": 4 }