Skip to content

Commit

Permalink
fixed croppost issues
Browse files Browse the repository at this point in the history
  • Loading branch information
piterand committed Nov 9, 2023
1 parent d2409aa commit 2825454
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
10 changes: 6 additions & 4 deletions ammico/cropposts.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,14 @@ 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)
ref_file_path = ref_files[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))
crop_file_path = files[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

0 comments on commit 2825454

Please sign in to comment.