{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-14T09:36:52.023683Z","iopub.execute_input":"2021-09-14T09:36:52.023901Z","iopub.status.idle":"2021-09-14T09:36:52.056008Z","shell.execute_reply.started":"2021-09-14T09:36:52.023876Z","shell.execute_reply":"2021-09-14T09:36:52.055047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport requests\n\n# General packages\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport plotly.graph_objs as go\nimport plotly.express as px\nimport PIL.Image\nimport seaborn as sns\n\nimport urllib\nfrom IPython.display import Image, display\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:57:45.229075Z","iopub.execute_input":"2021-09-14T09:57:45.22936Z","iopub.status.idle":"2021-09-14T09:57:45.96194Z","shell.execute_reply.started":"2021-09-14T09:57:45.229334Z","shell.execute_reply":"2021-09-14T09:57:45.960899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/wikipedia-image-caption/')","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:37:12.497705Z","iopub.execute_input":"2021-09-14T09:37:12.498529Z","iopub.status.idle":"2021-09-14T09:37:12.511248Z","shell.execute_reply.started":"2021-09-14T09:37:12.498472Z","shell.execute_reply":"2021-09-14T09:37:12.510095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/wikipedia-image-caption/image_data_test/image_pixels/test_image_pixels_part-00000.csv', sep='\\t', names=['image_url', 'b64_bytes', 'metadata_url'])\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:38:24.409368Z","iopub.execute_input":"2021-09-14T09:38:24.410182Z","iopub.status.idle":"2021-09-14T09:38:27.662027Z","shell.execute_reply.started":"2021-09-14T09:38:24.410144Z","shell.execute_reply":"2021-09-14T09:38:27.661146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:38:52.195486Z","iopub.execute_input":"2021-09-14T09:38:52.195801Z","iopub.status.idle":"2021-09-14T09:38:52.455465Z","shell.execute_reply.started":"2021-09-14T09:38:52.195768Z","shell.execute_reply":"2021-09-14T09:38:52.454704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:39:37.134775Z","iopub.execute_input":"2021-09-14T09:39:37.135645Z","iopub.status.idle":"2021-09-14T09:39:37.152575Z","shell.execute_reply.started":"2021-09-14T09:39:37.135588Z","shell.execute_reply":"2021-09-14T09:39:37.15171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/wikipedia-image-caption/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:40:17.305298Z","iopub.execute_input":"2021-09-14T09:40:17.305691Z","iopub.status.idle":"2021-09-14T09:40:17.883601Z","shell.execute_reply.started":"2021-09-14T09:40:17.305653Z","shell.execute_reply":"2021-09-14T09:40:17.882735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_file = pd.read_csv('../input/wikipedia-image-caption/test.tsv', sep='\\t')\ntest_file.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:42:15.836395Z","iopub.execute_input":"2021-09-14T09:42:15.837233Z","iopub.status.idle":"2021-09-14T09:42:16.084329Z","shell.execute_reply.started":"2021-09-14T09:42:15.837181Z","shell.execute_reply":"2021-09-14T09:42:16.083415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_links(df, num):\n    return df.image_url[:num].values\n\n\ndef load_images(links):\n    images = []\n    \n    for link in links:\n        URL = link\n        try:\n\n            with urllib.request.urlopen(URL) as url:\n                with open('./temp.jpg', 'wb') as f:\n                    f.write(url.read())\n\n            img = PIL.Image.open('./temp.jpg')\n            img = np.asarray(img)\n            images.append(img)\n        except:\n            continue\n    return images\n\ndef display_images(images, title=None): \n    f, ax = plt.subplots(2,5, figsize=(18,12))\n    if title:\n        f.suptitle(title, fontsize = 30)\n\n    for i, image_id in enumerate(images):\n        ax[i//5, i%5].imshow(image_id) \n   \n        ax[i//5, i%5].axis('off')\n\n    plt.show() ","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:49:06.992962Z","iopub.execute_input":"2021-09-14T09:49:06.993647Z","iopub.status.idle":"2021-09-14T09:49:07.003619Z","shell.execute_reply.started":"2021-09-14T09:49:06.993587Z","shell.execute_reply":"2021-09-14T09:49:07.00245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"links = get_links(df, 10)\nimages = load_images(links)\ndisplay_images(images)","metadata":{"execution":{"iopub.status.busy":"2021-09-14T09:49:44.199314Z","iopub.execute_input":"2021-09-14T09:49:44.200171Z","iopub.status.idle":"2021-09-14T09:50:11.98931Z","shell.execute_reply.started":"2021-09-14T09:49:44.200131Z","shell.execute_reply":"2021-09-14T09:50:11.988373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25,7))\nax = test_file['language'].value_counts().plot(kind='bar')\nplt.xticks(rotation=90,fontsize=20)\nplt.yticks(fontsize=20)\nplt.title(\"Languages v/s Count\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T10:15:14.888384Z","iopub.execute_input":"2021-09-14T10:15:14.8887Z","iopub.status.idle":"2021-09-14T10:15:15.656895Z","shell.execute_reply.started":"2021-09-14T10:15:14.888664Z","shell.execute_reply":"2021-09-14T10:15:15.655896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enArticals = test_file[test_file['language']=='en']","metadata":{"execution":{"iopub.status.busy":"2021-09-14T10:17:22.81925Z","iopub.execute_input":"2021-09-14T10:17:22.819518Z","iopub.status.idle":"2021-09-14T10:17:22.844224Z","shell.execute_reply.started":"2021-09-14T10:17:22.819492Z","shell.execute_reply":"2021-09-14T10:17:22.843529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enArticals.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T10:17:41.377584Z","iopub.execute_input":"2021-09-14T10:17:41.378445Z","iopub.status.idle":"2021-09-14T10:17:41.391209Z","shell.execute_reply.started":"2021-09-14T10:17:41.378406Z","shell.execute_reply":"2021-09-14T10:17:41.390195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"links = get_links(enArticals, 10)\nimages = load_images(links)\ndisplay_images(images)","metadata":{"execution":{"iopub.status.busy":"2021-09-14T10:21:21.182979Z","iopub.execute_input":"2021-09-14T10:21:21.183711Z","iopub.status.idle":"2021-09-14T10:21:44.183931Z","shell.execute_reply.started":"2021-09-14T10:21:21.183665Z","shell.execute_reply":"2021-09-14T10:21:44.182885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enArticals.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-09-14T10:22:09.652605Z","iopub.execute_input":"2021-09-14T10:22:09.653622Z","iopub.status.idle":"2021-09-14T10:22:09.667144Z","shell.execute_reply.started":"2021-09-14T10:22:09.653574Z","shell.execute_reply":"2021-09-14T10:22:09.666201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This url have nothing to open that's why some pictures are empty\nenArticals.loc[23 , 'image_url']","metadata":{"execution":{"iopub.status.busy":"2021-09-14T10:23:12.176426Z","iopub.execute_input":"2021-09-14T10:23:12.176781Z","iopub.status.idle":"2021-09-14T10:23:12.182937Z","shell.execute_reply.started":"2021-09-14T10:23:12.176747Z","shell.execute_reply":"2021-09-14T10:23:12.182295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}