From b8399ff6176923a53e0426c89e259ba08595974d Mon Sep 17 00:00:00 2001 From: Jiyong <75185178+Ataraxia1001@users.noreply.github.com> Date: Sat, 1 Jul 2023 01:03:45 +0900 Subject: [PATCH] Add files via upload --- test_one_image_sticker_on.ipynb | 188 ++++++++++++++++++++------------ 1 file changed, 116 insertions(+), 72 deletions(-) diff --git a/test_one_image_sticker_on.ipynb b/test_one_image_sticker_on.ipynb index 724869c..372d913 100644 --- a/test_one_image_sticker_on.ipynb +++ b/test_one_image_sticker_on.ipynb @@ -110,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "d239c21d", "metadata": {}, "outputs": [ @@ -582,7 +582,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "3ab49ca3", "metadata": {}, "outputs": [ @@ -592,7 +592,7 @@ "'./Brad_Pitt.png'" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -604,17 +604,17 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "62a37a1a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'c:\\\\workspace\\\\Sticker_On_Face\\\\git_push_sticker'" + "'C:\\\\Users\\\\Admin'" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -625,7 +625,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "857bb616", "metadata": {}, "outputs": [ @@ -683,7 +683,7 @@ " [0.16470589, 0.12941177, 0.11764706]]]], dtype=float32)" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -703,59 +703,59 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "id": "bdb4ebdf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([0.295754 , 0.34953576, 0.29536876, 0.37233624, 0.29394883,\n", + "array([0.295754 , 0.34953576, 0.29536876, 0.37233624, 0.2939488 ,\n", " 0.39541063, 0.29285085, 0.41659755, 0.29559624, 0.43934116,\n", - " 0.2974852 , 0.4600561 , 0.30300716, 0.48247913, 0.30756575,\n", + " 0.2974852 , 0.4600561 , 0.3030072 , 0.48247913, 0.30756575,\n", " 0.5061577 , 0.3130786 , 0.528417 , 0.32436132, 0.54882187,\n", " 0.33650964, 0.5691425 , 0.35426518, 0.5852487 , 0.37182865,\n", - " 0.6008181 , 0.3929378 , 0.61439466, 0.4145758 , 0.6214441 ,\n", + " 0.60081804, 0.39293784, 0.61439466, 0.4145758 , 0.6214441 ,\n", " 0.44033214, 0.6248505 , 0.46825892, 0.6260994 , 0.4954552 ,\n", " 0.6228098 , 0.522391 , 0.6201984 , 0.54687893, 0.6131589 ,\n", " 0.57148564, 0.59996915, 0.59324855, 0.5884598 , 0.6102371 ,\n", " 0.5714292 , 0.6249613 , 0.5539015 , 0.63657844, 0.53203714,\n", - " 0.6465676 , 0.50773525, 0.6527968 , 0.48418987, 0.6576055 ,\n", - " 0.45755792, 0.65946704, 0.43667215, 0.6617074 , 0.4125735 ,\n", - " 0.66355073, 0.39063698, 0.6601421 , 0.3648152 , 0.6573597 ,\n", - " 0.33967906, 0.32984275, 0.3371966 , 0.35017216, 0.31827185,\n", - " 0.37584046, 0.31149724, 0.4043654 , 0.31242692, 0.43117392,\n", - " 0.32046783, 0.42971358, 0.33479303, 0.40379953, 0.32893068,\n", - " 0.37877557, 0.33061895, 0.35352984, 0.3326923 , 0.5033091 ,\n", + " 0.6465676 , 0.50773525, 0.6527968 , 0.48418984, 0.6576056 ,\n", + " 0.45755792, 0.65946704, 0.43667215, 0.6617074 , 0.41257346,\n", + " 0.66355073, 0.39063698, 0.6601421 , 0.3648152 , 0.6573598 ,\n", + " 0.33967903, 0.32984275, 0.3371966 , 0.35017216, 0.31827185,\n", + " 0.37584046, 0.31149727, 0.4043654 , 0.31242692, 0.4311739 ,\n", + " 0.32046783, 0.42971355, 0.33479303, 0.40379953, 0.32893068,\n", + " 0.37877557, 0.33061895, 0.35352984, 0.33269227, 0.5033091 ,\n", " 0.31804577, 0.52909374, 0.31070626, 0.5596832 , 0.30831742,\n", - " 0.5880651 , 0.31009004, 0.6116996 , 0.33000204, 0.5847234 ,\n", + " 0.5880651 , 0.31009 , 0.61169964, 0.33000204, 0.5847234 ,\n", " 0.32804832, 0.5571705 , 0.3269383 , 0.5310863 , 0.32762566,\n", " 0.5058189 , 0.33147117, 0.46658787, 0.34902146, 0.46681538,\n", - " 0.37534127, 0.4659111 , 0.3992975 , 0.46662572, 0.42492825,\n", + " 0.37534127, 0.46591106, 0.3992975 , 0.46662575, 0.42492825,\n", " 0.44028717, 0.36255747, 0.43133745, 0.41423956, 0.41913265,\n", " 0.43931705, 0.4323608 , 0.44838887, 0.4485782 , 0.4506466 ,\n", " 0.46672505, 0.4557325 , 0.48531458, 0.45097727, 0.5036292 ,\n", " 0.44662103, 0.51893246, 0.4354125 , 0.50611585, 0.41066128,\n", - " 0.4939933 , 0.3612475 , 0.35468203, 0.3643941 , 0.37043515,\n", - " 0.35756868, 0.38859537, 0.35573584, 0.40498653, 0.35669485,\n", - " 0.4200809 , 0.36206293, 0.40411082, 0.36700672, 0.38861006,\n", - " 0.3693364 , 0.371516 , 0.36727732, 0.38882536, 0.3617027 ,\n", + " 0.4939933 , 0.36124748, 0.35468203, 0.3643941 , 0.37043515,\n", + " 0.35756868, 0.38859537, 0.35573584, 0.4049866 , 0.3566948 ,\n", + " 0.4200809 , 0.3620629 , 0.40411082, 0.36700675, 0.38861006,\n", + " 0.3693364 , 0.371516 , 0.36727738, 0.38882536, 0.3617027 ,\n", " 0.51747555, 0.35990041, 0.5328683 , 0.35205978, 0.549799 ,\n", - " 0.350487 , 0.56708884, 0.35332385, 0.58345217, 0.360135 ,\n", + " 0.350487 , 0.5670888 , 0.35332385, 0.58345217, 0.36013496,\n", " 0.56832415, 0.3631731 , 0.5511051 , 0.36521137, 0.5333417 ,\n", " 0.36329472, 0.5496076 , 0.35821697, 0.39555016, 0.50548565,\n", " 0.4220818 , 0.4914119 , 0.45107913, 0.48515984, 0.46776295,\n", " 0.48885784, 0.48486534, 0.4861649 , 0.5145559 , 0.4906208 ,\n", - " 0.5423433 , 0.5041629 , 0.51931536, 0.5194112 , 0.49735215,\n", - " 0.5261039 , 0.46801206, 0.5297542 , 0.44101968, 0.5263839 ,\n", - " 0.4173345 , 0.51918024, 0.40802932, 0.5067924 , 0.43237385,\n", + " 0.5423433 , 0.50416285, 0.51931536, 0.5194112 , 0.49735215,\n", + " 0.52610385, 0.46801206, 0.52975416, 0.44101968, 0.5263839 ,\n", + " 0.41733453, 0.51918024, 0.40802932, 0.50679237, 0.43237385,\n", " 0.50163764, 0.46788976, 0.5015161 , 0.5048963 , 0.5005993 ,\n", - " 0.5316817 , 0.5051993 , 0.5047973 , 0.5078653 , 0.46869892,\n", + " 0.5316817 , 0.5051993 , 0.5047973 , 0.5078653 , 0.46869895,\n", " 0.5091715 , 0.43297336, 0.50680435, 0.38913074, 0.36124888,\n", - " 0.55008125, 0.3581036 ], dtype=float32)" + " 0.55008125, 0.35810357], dtype=float32)" ] }, - "execution_count": 10, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -769,7 +769,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "id": "cef663e9", "metadata": {}, "outputs": [], @@ -782,7 +782,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "ddb0c88c", "metadata": {}, "outputs": [ @@ -800,7 +800,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "id": "7fb4e33c", "metadata": {}, "outputs": [ @@ -858,7 +858,7 @@ " [42, 33, 30]]], dtype=uint8)" ] }, - "execution_count": 13, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -869,17 +869,17 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "id": "06dcc9b6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "False" ] }, - "execution_count": 14, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -892,7 +892,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "id": "08b14694", "metadata": {}, "outputs": [], @@ -904,7 +904,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "id": "c06407a0", "metadata": {}, "outputs": [], @@ -936,17 +936,17 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "id": "5f45cdd6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'c:\\\\workspace\\\\Sticker_On_Face\\\\git_push_sticker'" + "'C:\\\\Users\\\\Admin'" ] }, - "execution_count": 17, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -957,27 +957,28 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 19, "id": "c5dd3da7", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "467 378\n", - "75.41726633906364 89.13161963224411\n", - "186 151\n", - "226.41726633906364 275.1316196322441\n" + "ename": "NameError", + "evalue": "name 'desired_scale' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_26744/1559227239.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0msmaller_height\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlarger_height\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mdesired_scale\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[0msmaller_width\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlarger_width\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mdesired_scale\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mrescaled_smaller\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mresize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msmaller_image\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0msmaller_width\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msmaller_height\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mNameError\u001b[0m: name 'desired_scale' is not defined" ] } ], "source": [ "larger_image = cv2.imread('Brad_Pitt.png')\n", - "smaller_image = cv2.imread('./Stickers/bat_wing-removebg-preview.png')\n", + "smaller_image = cv2.imread('./Stickers/bat.png')\n", "larger_height, larger_width, _ = larger_image.shape\n", "\n", - "# desired_scale = 0.4 # Desired scale for the smaller image (e.g., 0.5 for half the size)\n", + "desired_scale = 0.4 # Desired scale for the smaller image (e.g., 0.5 for half the size)\n", "\n", "\n", "smaller_height = int(larger_height * desired_scale)\n", @@ -985,7 +986,7 @@ "rescaled_smaller = cv2.resize(smaller_image, (smaller_width, smaller_height))\n", "\n", "\n", - "position_x = pred[0]*255 # X-coordinate of the top-left corner of the smaller image\n", + "position_x = pred[0]*255 # X-coordinate of the top-left corner of the smaller image\n", "position_y = pred[1]*255 # Y-coordinate of the top-left corner of the smaller image\n", "smaller_height, smaller_width, _ = rescaled_smaller.shape\n", " \n", @@ -1000,10 +1001,20 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 35, "id": "d19055ee", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "467 378\n", + "186 151\n", + "75.41726633906364 89.13161963224411\n" + ] + } + ], "source": [ "# overlay = larger_image.copy()\n", "# overlay[int(position_y):int(end_y), int(position_x):int(end_x)] = rescaled_smaller\n", @@ -1011,7 +1022,51 @@ "# result = cv2.add(larger_image, overlay)\n", "\n", "#https://stackoverflow.com/questions/14063070/overlay-a-smaller-image-on-a-larger-image-python-opencv\n", - "s_img = cv2.imread(\"smaller_image.png\", -1)\n", + "\n", + "s_img = cv2.imread('./bat.png')\n", + "l_img = cv2.imread('Brad_Pitt.png')\n", + "# x_offset=y_offset=50 # x_offset, y_offset = position_x, position_y\n", + "\n", + "larger_height, larger_width, _ = l_img.shape\n", + "desired_scale = 0.4 # Desired scale for the smaller image (e.g., 0.5 for half the size)\n", + "\n", + "\n", + "\n", + "\n", + "smaller_height = int(larger_height * desired_scale)\n", + "smaller_width = int(larger_width * desired_scale)\n", + "rescaled_smaller = cv2.resize(s_img, (smaller_width, smaller_height))\n", + "\n", + "smaller_height, smaller_width, _ = rescaled_smaller.shape\n", + "\n", + "x_offset = pred[0]*255 # X-coordinate of the top-left corner of the smaller image\n", + "y_offset = pred[1]*255 \n", + "\n", + "print(larger_height, larger_width)\n", + "print(smaller_height, smaller_width)\n", + "print(x_offset, y_offset)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "5809f249", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "could not broadcast input array from shape (423,360,3) into shape (186,151,3)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_26744/3739050118.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ml_img\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my_offset\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my_offset\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0msmaller_height\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx_offset\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx_offset\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0msmaller_width\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ms_img\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0my1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0my_offset\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_offset\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0ms_img\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mx1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mx_offset\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx_offset\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0ms_img\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: could not broadcast input array from shape (423,360,3) into shape (186,151,3)" + ] + } + ], + "source": [ + "l_img[int(y_offset):int(y_offset+smaller_height), int(x_offset):int(x_offset+smaller_width)] = s_img\n", "\n", "y1, y2 = y_offset, y_offset + s_img.shape[0]\n", "x1, x2 = x_offset, x_offset + s_img.shape[1]\n", @@ -1026,21 +1081,10 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "id": "45ff933e", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name = \"output_sticker_on\"\n", "cv2.imwrite(f\"./test_result/{name}.png\", result)" @@ -1086,7 +1130,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.7" + "version": "3.9.7" } }, "nbformat": 4,