Skip to content

Commit

Permalink
fixed errors in documentation (#163)
Browse files Browse the repository at this point in the history
* fixed errors in documentation

* fixed croppost.py
  • Loading branch information
piterand authored Nov 13, 2023
1 parent f2293a2 commit fcd6d19
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 35 deletions.
14 changes: 8 additions & 6 deletions ammico/cropposts.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,15 @@ def crop_media_posts(

# get the reference images with regions that signify areas to crop
ref_views = []
for ref_file in ref_files:
ref_view = cv2.imread(ref_file)
for ref_file in ref_files.values():
ref_file_path = ref_file["filename"]
ref_view = cv2.imread(ref_file_path)
ref_views.append(ref_view)
# parse through the social media posts to be cropped
for crop_file in files:
view = cv2.imread(crop_file)
print("Doing file {}".format(crop_file))
for crop_file in files.values():
crop_file_path = crop_file["filename"]
view = cv2.imread(crop_file_path)
print("Doing file {}".format(crop_file_path))
crop_view = crop_posts_from_refs(
ref_views,
view,
Expand All @@ -341,7 +343,7 @@ def crop_media_posts(
)
if crop_view is not None:
# save the image to the provided folder
filename = ntpath.basename(crop_file)
filename = ntpath.basename(crop_file_path)
save_path = os.path.join(save_crop_dir, filename)
save_path = save_path.replace("\\", "/")
cv2.imwrite(save_path, crop_view)
20 changes: 6 additions & 14 deletions ammico/notebooks/cropposts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"source": [
"# load ref view for cropping the same type social media posts images.\n",
"# substitute the below paths for your samples\n",
"path_ref = \"/content/ref/ref-00.png\"\n",
"path_ref = \"../data/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",
Expand Down Expand Up @@ -133,24 +133,16 @@
"outputs": [],
"source": [
"\n",
"crop_dir = \"/content/drive/MyDrive/misinformation-data/\"\n",
"ref_dir = \"/content/ref/\"\n",
"save_crop_dir = \"/content/drive/MyDrive/misinformation-data/crop/\"\n",
"crop_dir = \"../data/\"\n",
"ref_dir = \"../data/ref/\"\n",
"save_crop_dir = \"../data/crop/\"\n",
"\n",
"files = ammico.find_files(path=crop_dir,limit=10,)\n",
"files = ammico.find_files(path=crop_dir,limit=10,recursive=False)\n",
"ref_files = ammico.find_files(path=ref_dir, limit=100)\n",
"\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\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3b3c1ad",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -169,7 +161,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
File renamed without changes
File renamed without changes
10 changes: 6 additions & 4 deletions ammico/test/test_cropposts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import cv2
import pytest
import numpy as np
import ammico.utils as utils


TEST_IMAGE_1 = "pic1.png"
TEST_IMAGE_2 = "pic2.png"
TEST_IMAGE_1 = "crop_test_files/pic1.png"
TEST_IMAGE_2 = "crop_test_ref_files/pic2.png"


@pytest.fixture
Expand Down Expand Up @@ -75,8 +76,9 @@ def test_paste_image_and_comment(open_images):


def test_crop_media_posts(get_path, tmp_path):
files = [get_path + TEST_IMAGE_1]
ref_files = [get_path + TEST_IMAGE_2]
print(get_path)
files = utils.find_files(path=get_path + "crop_test_files/")
ref_files = utils.find_files(path=get_path + "crop_test_ref_files/")
crpo.crop_media_posts(files, ref_files, tmp_path)
assert len(list(tmp_path.iterdir())) == 1
# now check that image in tmp_path is the cropped one
Expand Down
13 changes: 3 additions & 10 deletions docs/source/notebooks/DemoNotebook_ammico.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,9 @@
"\n",
"The user can set if the text should be further summarized, and analyzed for sentiment and named entity recognition, by setting the keyword `analyse_text` to `True` (the default is `False`). If set, the transformers pipeline is used for each of these tasks, with the default models as of 03/2023. Other models can be selected by setting the optional keyword `model_names` to a list of selected models, on for each task: `model_names=[\"sshleifer/distilbart-cnn-12-6\", \"distilbert-base-uncased-finetuned-sst-2-english\", \"dbmdz/bert-large-cased-finetuned-conll03-english\"]` for summary, sentiment, and ner. To be even more specific, revision numbers can also be selected by specifying the optional keyword `revision_numbers` to a list of revision numbers for each model, for example `revision_numbers=[\"a4f8f3e\", \"af0f99b\", \"f2482bf\"]`. \n",
"\n",
"Please note that for the Google Cloud Vision API (the TextDetector class) you need to set a key in order to process the images. This key is ideally set as an environment variable using for example"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"] = \"misinformation-campaign-981aa55a3b13.json\"\n"
"Please note that for the Google Cloud Vision API (the TextDetector class) you need to set a key in order to process the images. This key is ideally set as an environment variable using for example:\n",
"\n",
"`os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"] = \"misinformation-campaign-981aa55a3b13.json\"`"
]
},
{
Expand Down
17 changes: 16 additions & 1 deletion docs/source/notebooks/Example multimodal.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@
" drive.mount(\"/content/drive\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d58a23e",
"metadata": {
"nbsphinx": "hidden"
},
"outputs": [],
"source": [
"# if there is not enough space on your hard drive, clear the cache folder\n",
"# this cell will delete all your cache!\n",
"!rm -rf ~/.cache\n",
"!mkdir ~/.cache"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -531,7 +546,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down

0 comments on commit fcd6d19

Please sign in to comment.