Skip to content

Commit

Permalink
Update notebooks and add options in display for text model names (#133)
Browse files Browse the repository at this point in the history
* updated notebooks for the new interface

---------

Co-authored-by: Petr Andriushchenko <[email protected]>
  • Loading branch information
iulusoy and piterand authored Jul 12, 2023
1 parent 009ea31 commit e11d4c0
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 253 deletions.
3 changes: 2 additions & 1 deletion ammico/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
except ImportError:
# Running on pre-3.8 Python; use importlib-metadata package
import importlib_metadata as metadata # type: ignore
from ammico.cropposts import crop_media_posts
from ammico.cropposts import crop_media_posts, crop_posts_from_refs
from ammico.display import AnalysisExplorer
from ammico.faces import EmotionDetector
from ammico.multimodal_search import MultimodalSearch
Expand All @@ -18,6 +18,7 @@

__all__ = [
"crop_media_posts",
"crop_posts_from_refs",
"AnalysisExplorer",
"EmotionDetector",
"MultimodalSearch",
Expand Down
60 changes: 58 additions & 2 deletions ammico/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def __init__(self, mydict: dict) -> None:
State("left_select_id", "value"),
State("Dropdown_select_Detector", "value"),
State("setting_Text_analyse_text", "value"),
State("setting_Text_model_names", "value"),
State("setting_Text_revision_numbers", "value"),
State("setting_Emotion_emotion_threshold", "value"),
State("setting_Emotion_race_threshold", "value"),
State("setting_Color_delta_e_method", "value"),
Expand Down Expand Up @@ -177,6 +179,48 @@ def _create_setting_layout(self):
["Analyse text"],
id="setting_Text_analyse_text",
),
html.Div(
[
html.Div(
"Select models for text_summary, text_sentiment, text_NER or leave blank for default:",
style={
"height": "30px",
"margin-top": "5px",
},
),
dcc.Input(
type="text",
id="setting_Text_model_names",
style={"height": "auto", "margin-bottom": "auto"},
),
],
style={
"width": "33%",
"display": "inline-block",
"margin-top": "10px",
},
),
html.Div(
[
html.Div(
"Select model revision number for text_summary, text_sentiment, text_NER or leave blank for default:",
style={
"height": "30px",
"margin-top": "5px",
},
),
dcc.Input(
type="text",
id="setting_Text_revision_numbers",
style={"height": "auto", "margin-bottom": "auto"},
),
],
style={
"width": "33%",
"display": "inline-block",
"margin-top": "10px",
},
),
],
),
html.Div(
Expand Down Expand Up @@ -414,7 +458,9 @@ def _right_output_analysis(
all_img_options: dict,
current_img_value: str,
detector_value: str,
settings_text_analyse_text: bool,
settings_text_analyse_text: list,
settings_text_model_names: str,
settings_text_revision_numbers: str,
setting_emotion_emotion_threshold: int,
setting_emotion_race_threshold: int,
setting_color_delta_e_method: str,
Expand Down Expand Up @@ -448,8 +494,18 @@ def _right_output_analysis(

identify_function = identify_dict[detector_value]
if detector_value == "TextDetector":
analyse_text = (
True if settings_text_analyse_text == ["Analyse text"] else False
)
detector_class = identify_function(
image_copy, analyse_text=settings_text_analyse_text
image_copy,
analyse_text=analyse_text,
model_names=[settings_text_model_names]
if (settings_text_model_names is not None)
else None,
revision_numbers=[settings_text_revision_numbers]
if (settings_text_revision_numbers is not None)
else None,
)
elif detector_value == "EmotionDetector":
detector_class = identify_function(
Expand Down
10 changes: 5 additions & 5 deletions ammico/multimodal_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,15 +935,15 @@ def image_text_match_reordering(
return itm_scores2, image_gradcam_with_itm

def show_results(
self, query: dict, itm=False, image_gradcam_with_itm=False
self, query: dict, itm: bool = False, image_gradcam_with_itm: dict = {}
) -> None:
"""
Show results of search.
Args:
query (dict): query.
itm (bool): use itm model. Default: False.
image_gradcam_with_itm (bool): use gradcam. Default: False.
image_gradcam_with_itm (dict): use gradcam. Default: empty.
"""
if "image" in query.keys():
pic = Image.open(query["image"]).convert("RGB")
Expand Down Expand Up @@ -972,11 +972,11 @@ def show_results(
):
if s[1][current_querry_rank] is None:
break
if image_gradcam_with_itm is False:
p1 = Image.open(s[1]["filename"]).convert("RGB")
else:
if bool(image_gradcam_with_itm) is True and itm is True:
image = image_gradcam_with_itm[list(query.values())[0]][s[0]]
p1 = Image.fromarray(image.astype("uint8"), "RGB")
else:
p1 = Image.open(s[1]["filename"]).convert("RGB")
p1.thumbnail((400, 400))
display(
"Rank: "
Expand Down
16 changes: 7 additions & 9 deletions ammico/notebooks/colors_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
"metadata": {},
"outputs": [],
"source": [
"import ammico\n",
"from ammico import utils as mutils\n",
"from ammico import display as mdisplay\n"
"import ammico"
]
},
{
Expand All @@ -66,7 +64,7 @@
"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",
"images = ammico.find_files(\n",
" path=\"/content/drive/MyDrive/misinformation-data/\",\n",
" limit=10,\n",
")\n"
Expand All @@ -86,7 +84,7 @@
"metadata": {},
"outputs": [],
"source": [
"mydict = mutils.initialize_dict(images)"
"mydict = ammico.initialize_dict(images)"
]
},
{
Expand All @@ -104,7 +102,7 @@
"metadata": {},
"outputs": [],
"source": [
"analysis_explorer = mdisplay.AnalysisExplorer(mydict)\n",
"analysis_explorer = ammico.AnalysisExplorer(mydict)\n",
"analysis_explorer.run_server(port = 8057)"
]
},
Expand Down Expand Up @@ -140,8 +138,8 @@
"metadata": {},
"outputs": [],
"source": [
"outdict = mutils.append_data_to_dict(mydict)\n",
"df = mutils.dump_df(outdict)"
"outdict = ammico.append_data_to_dict(mydict)\n",
"df = ammico.dump_df(outdict)"
]
},
{
Expand Down Expand Up @@ -195,7 +193,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.11.3"
}
},
"nbformat": 4,
Expand Down
26 changes: 10 additions & 16 deletions ammico/notebooks/cropposts.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "b25986d7",
"metadata": {},
Expand All @@ -10,7 +9,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c8a5a491",
"metadata": {},
Expand Down Expand Up @@ -55,16 +53,14 @@
"metadata": {},
"outputs": [],
"source": [
"import ammico.cropposts as crpo\n",
"import ammico.utils as utils\n",
"import ammico\n",
"import matplotlib.pyplot as plt\n",
"import cv2\n",
"import importlib_resources\n",
"pkg = importlib_resources.files(\"ammico\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e7b8127f",
"metadata": {},
Expand All @@ -81,23 +77,22 @@
"source": [
"# load ref view for cropping the same type social media posts images.\n",
"# substitute the below paths for your samples\n",
"path_ref = pkg / \"data\" / \"ref\" / \"ref-00.png\"\n",
"path_ref = \"/content/ref/ref-00.png\"\n",
"ref_view = cv2.imread(path_ref)\n",
"RGB_ref_view = cv2.cvtColor(ref_view, cv2.COLOR_BGR2RGB)\n",
"plt.figure(figsize=(10, 15))\n",
"plt.imshow(RGB_ref_view)\n",
"plt.show()\n",
"\n",
"path_post = pkg / \"data\" / \"test-crop-image.png\"\n",
"view = cv2.imread(path_post)\n",
"view = cv2.imread(path_post.as_posix())\n",
"RGB_view = cv2.cvtColor(view, cv2.COLOR_BGR2RGB)\n",
"plt.figure(figsize=(10, 15))\n",
"plt.imshow(RGB_view)\n",
"plt.show()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "49a11f61",
"metadata": {},
Expand All @@ -114,14 +109,13 @@
"source": [
"# crop a posts from reference view, check the cropping \n",
"# this will only plot something if the reference is found on the image\n",
"crop_view = crpo.crop_posts_from_refs(\n",
"crop_view = ammico.crop_posts_from_refs(\n",
" [ref_view], view, \n",
" plt_match=True, plt_crop=True, plt_image=True,\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "1929e549",
"metadata": {},
Expand All @@ -139,14 +133,14 @@
"outputs": [],
"source": [
"\n",
"crop_dir = \"../ammico/data/\"\n",
"ref_dir = \"../ammico/data/ref\"\n",
"save_crop_dir = \"data/crop/\"\n",
"crop_dir = \"/content/drive/MyDrive/misinformation-data/\"\n",
"ref_dir = \"/content/ref/\"\n",
"save_crop_dir = \"/content/drive/MyDrive/misinformation-data/crop/\"\n",
"\n",
"files = utils.find_files(path=crop_dir,limit=10,)\n",
"ref_files = utils.find_files(path=ref_dir, limit=100)\n",
"files = ammico.find_files(path=crop_dir,limit=10,)\n",
"ref_files = ammico.find_files(path=ref_dir, limit=100)\n",
"\n",
"crpo.crop_media_posts(files, ref_files, save_crop_dir, plt_match=True, plt_crop=False, plt_image=False)\n",
"ammico.crop_media_posts(files, ref_files, save_crop_dir, plt_match=True, plt_crop=False, plt_image=False)\n",
"print(\"Batch cropping images done\")"
]
},
Expand Down
18 changes: 8 additions & 10 deletions ammico/notebooks/facial_expressions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@
"metadata": {},
"outputs": [],
"source": [
"import ammico\n",
"from ammico import utils as mutils\n",
"from ammico import display as mdisplay"
"import ammico"
]
},
{
Expand All @@ -75,7 +73,7 @@
"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",
"images = ammico.find_files(\n",
" path=\"/content/drive/MyDrive/misinformation-data/\",\n",
" limit=10,\n",
")"
Expand All @@ -97,7 +95,7 @@
"metadata": {},
"outputs": [],
"source": [
"mydict = mutils.initialize_dict(images)"
"mydict = ammico.initialize_dict(images)"
]
},
{
Expand All @@ -117,7 +115,7 @@
"metadata": {},
"outputs": [],
"source": [
"analysis_explorer = mdisplay.AnalysisExplorer(mydict)\n",
"analysis_explorer = ammico.AnalysisExplorer(mydict)\n",
"analysis_explorer.run_server(port = 8050)"
]
},
Expand All @@ -138,7 +136,7 @@
"outputs": [],
"source": [
"for key in mydict.keys():\n",
" mydict[key] = ammico.faces.EmotionDetector(mydict[key]).analyse_image()"
" mydict[key] = ammico.EmotionDetector(mydict[key]).analyse_image()"
]
},
{
Expand All @@ -157,8 +155,8 @@
"metadata": {},
"outputs": [],
"source": [
"outdict = mutils.append_data_to_dict(mydict)\n",
"df = mutils.dump_df(outdict)"
"outdict = ammico.append_data_to_dict(mydict)\n",
"df = ammico.dump_df(outdict)"
]
},
{
Expand Down Expand Up @@ -224,7 +222,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.11.3"
},
"vscode": {
"interpreter": {
Expand Down
Loading

0 comments on commit e11d4c0

Please sign in to comment.