AMMICO/notebooks/cropposts.ipynb
2023-04-24 17:03:58 +02:00

145 строки
3.6 KiB
Plaintext
Generated

{
"cells": [
{
"cell_type": "markdown",
"id": "b25986d7",
"metadata": {},
"source": [
"# Crop posts from social media posts images"
]
},
{
"cell_type": "markdown",
"id": "c8a5a491",
"metadata": {},
"source": [
"Crop posts from social media posts images, to keep import text informations from social media posts images.\n",
"We can set some manually cropped views from social media posts as reference for cropping the same type social media posts images."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ae02c45",
"metadata": {},
"outputs": [],
"source": [
"import ammico.cropposts as crpo\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from PIL import Image\n",
"import cv2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d04d0e86",
"metadata": {},
"outputs": [],
"source": [
"# load ref view for cropping the same type social media posts images.\n",
"ref_view = np.array(Image.open(\"data/ref/ref-00.png\"))\n",
"plt.imshow(ref_view)\n",
"plt.show()\n",
"\n",
"view = np.array(Image.open(\"data/napsa/100539_ben.png\"))\n",
"plt.figure(figsize=(10, 15))\n",
"plt.imshow(view)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71850d9d",
"metadata": {},
"outputs": [],
"source": [
"# crop a posts from reference view, set plt_match=True, plt_crop=True\n",
"crop_view, match_num = crpo.crop_posts_image(\n",
" ref_view, view, plt_match=True, plt_crop=True\n",
")\n",
"\n",
"# save cropped images\n",
"cv2.imwrite(\"data/100539_ben.png\", crop_view)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67fc7b82",
"metadata": {},
"outputs": [],
"source": [
"view2 = np.array(Image.open(\"data/napsa/100541_ben.png\"))\n",
"plt.figure(figsize=(10, 15))\n",
"plt.imshow(view2)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21d87359",
"metadata": {},
"outputs": [],
"source": [
"# crop a posts from the same reference view, set plt_match=True, plt_crop=True\n",
"crop_view, match_num = crpo.crop_posts_image(\n",
" ref_view, view2, plt_match=True, plt_crop=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eef89291",
"metadata": {},
"outputs": [],
"source": [
"# Batch crop images from image folders.\n",
"# The cropped images will save in save_crop_dir folders with the same file name form origin.\n",
"# We can set many types reference images in ref_dir folder, to crop posts images in different types social media, like twitter or facebook.\n",
"ref_dir = \"./data/ref\"\n",
"crop_dir = \"./data/apsa\"\n",
"save_crop_dir = \"data/crop\"\n",
"crpo.crop_posts_from_files(\n",
" ref_dir, crop_dir, save_crop_dir, plt_match=False, plt_crop=False\n",
")\n",
"\n",
"print(\"Batch cropping images well done\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c10d9f6f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}