{"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":"import os\nimport random\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\n\nimport PIL.Image\n\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2021-09-14T02:47:07.042788Z","iopub.execute_input":"2021-09-14T02:47:07.043134Z","iopub.status.idle":"2021-09-14T02:47:07.293248Z","shell.execute_reply.started":"2021-09-14T02:47:07.043037Z","shell.execute_reply":"2021-09-14T02:47:07.292374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/wikipedia-image-caption/')","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-14T02:47:12.811116Z","iopub.execute_input":"2021-09-14T02:47:12.811387Z","iopub.status.idle":"2021-09-14T02:47:12.821559Z","shell.execute_reply.started":"2021-09-14T02:47:12.811362Z","shell.execute_reply":"2021-09-14T02:47:12.820673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#I hate the word deprecated. Nothing stays in programming languages for a decent time.","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/wikipedia-image-caption/test.tsv', sep='\\t', error_bad_lines=False)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T02:47:25.441078Z","iopub.execute_input":"2021-09-14T02:47:25.441347Z","iopub.status.idle":"2021-09-14T02:47:25.806937Z","shell.execute_reply.started":"2021-09-14T02:47:25.441320Z","shell.execute_reply":"2021-09-14T02:47:25.806152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#That snippet below with GPU takes more than 4 min. till the message: \"Your notebook tried to allocate more memory than is available. It has restarted.\"  Go to Google Cloud.","metadata":{}},{"cell_type":"code","source":"#df1 = pd.read_csv('../input/wikipedia-image-caption/train-00000-of-00005.tsv', sep='\\t', error_bad_lines=False)\n#df1.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T02:47:50.869020Z","iopub.execute_input":"2021-09-14T02:47:50.869735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2nd row, fourth column (image url)\n\ndf.iloc[1,3]","metadata":{"execution":{"iopub.status.busy":"2021-09-14T01:04:56.475831Z","iopub.execute_input":"2021-09-14T01:04:56.476133Z","iopub.status.idle":"2021-09-14T01:04:56.481741Z","shell.execute_reply.started":"2021-09-14T01:04:56.476103Z","shell.execute_reply":"2021-09-14T01:04:56.481091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndf['language'].hist(figsize = (10, 5))\nplt.title('Wikipedia Caption Matching Languages')\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2021-09-14T01:05:44.934260Z","iopub.execute_input":"2021-09-14T01:05:44.934810Z","iopub.status.idle":"2021-09-14T01:05:45.942677Z","shell.execute_reply.started":"2021-09-14T01:05:44.934762Z","shell.execute_reply":"2021-09-14T01:05:45.941723Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by  Georgii Sirotenko  https://www.kaggle.com/georgiisirotenko/pytorch-fish-outliers-handling-test-100\n    \nimport plotly.graph_objects as go    \n\nfig = go.Figure(\n    data=[ go.Bar(x=df['language'].value_counts().index, \n            y=df['language'].value_counts().values,\n            text=df['language'].value_counts().values,\n            textposition='auto',name='Count',\n           marker_color='indianred')],\n    layout_title_text=\"Language Distribution\"\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T02:31:21.158483Z","iopub.execute_input":"2021-09-14T02:31:21.158791Z","iopub.status.idle":"2021-09-14T02:31:21.307401Z","shell.execute_reply.started":"2021-09-14T02:31:21.158762Z","shell.execute_reply":"2021-09-14T02:31:21.306413Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_df = len(df)\nprint(f\"There are {len_df} images\")","metadata":{"execution":{"iopub.status.busy":"2021-09-14T00:23:10.319968Z","iopub.execute_input":"2021-09-14T00:23:10.320257Z","iopub.status.idle":"2021-09-14T00:23:10.325115Z","shell.execute_reply.started":"2021-09-14T00:23:10.320228Z","shell.execute_reply":"2021-09-14T00:23:10.324225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ERROR: Could not find a version that satisfies the requirement urllib (from versions: none)\n#ERROR: No matching distribution found for urllib\n\n#!pip install urllib","metadata":{"execution":{"iopub.status.busy":"2021-09-14T00:36:32.776488Z","iopub.execute_input":"2021-09-14T00:36:32.777313Z","iopub.status.idle":"2021-09-14T00:36:32.780706Z","shell.execute_reply.started":"2021-09-14T00:36:32.777272Z","shell.execute_reply":"2021-09-14T00:36:32.780076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import urllib","metadata":{"execution":{"iopub.status.busy":"2021-09-14T00:37:16.381961Z","iopub.execute_input":"2021-09-14T00:37:16.382250Z","iopub.status.idle":"2021-09-14T00:37:16.386791Z","shell.execute_reply.started":"2021-09-14T00:37:16.382221Z","shell.execute_reply":"2021-09-14T00:37:16.385894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import urllib.request\nrequest_url = urllib.request.urlopen('https://upload.wikimedia.org/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg')\nprint(request_url.read())","metadata":{"execution":{"iopub.status.busy":"2021-09-14T00:42:20.238449Z","iopub.execute_input":"2021-09-14T00:42:20.238699Z","iopub.status.idle":"2021-09-14T00:42:21.494793Z","shell.execute_reply.started":"2021-09-14T00:42:20.238671Z","shell.execute_reply":"2021-09-14T00:42:21.486337Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#urllib.parse\n\n\"This module helps to define functions to manipulate URLs and their components parts, to build or break them. It usually focuses on splitting a URL into small components; or joining different URL components into URL string.\"\n\nhttps://www.geeksforgeeks.org/python-urllib-module/","metadata":{}},{"cell_type":"code","source":"from urllib.parse import * \nparse_url = urlparse('https://upload.wikimedia.org/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg')\nprint(parse_url)\nprint(\"\\n\")\nunparse_url = urlunparse(parse_url)\nprint(unparse_url)","metadata":{"execution":{"iopub.status.busy":"2021-09-14T00:44:42.994536Z","iopub.execute_input":"2021-09-14T00:44:42.995195Z","iopub.status.idle":"2021-09-14T00:44:43.000878Z","shell.execute_reply.started":"2021-09-14T00:44:42.995154Z","shell.execute_reply":"2021-09-14T00:44:43.000186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#The different components of a URL are separated and joined again.","metadata":{}},{"cell_type":"code","source":"ParseResult(scheme='https', netloc='upload.wikimedia.org', path='/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg', params='', query='', fragment='')\n\n#https://www.wikimedia.org/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg","metadata":{"execution":{"iopub.status.busy":"2021-09-14T00:52:17.669175Z","iopub.execute_input":"2021-09-14T00:52:17.669487Z","iopub.status.idle":"2021-09-14T00:52:17.675575Z","shell.execute_reply.started":"2021-09-14T00:52:17.669458Z","shell.execute_reply":"2021-09-14T00:52:17.674961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pillow","metadata":{"execution":{"iopub.status.busy":"2021-09-14T01:08:44.743794Z","iopub.execute_input":"2021-09-14T01:08:44.744090Z","iopub.status.idle":"2021-09-14T01:08:53.349637Z","shell.execute_reply.started":"2021-09-14T01:08:44.744061Z","shell.execute_reply":"2021-09-14T01:08:53.348659Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://www.geeksforgeeks.org/how-to-open-an-image-from-the-url-in-pil/\n\nimport urllib.request\n\nfrom PIL import Image\n\n#urllib.request.urlretrieve(‘Image url’, “file_name”)\n\nurllib.request.urlretrieve(\n  'https://upload.wikimedia.org/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg',\n   \"Thermopylae_ancient_coastline_large.jpg\")\n  \nimg = Image.open(\"Thermopylae_ancient_coastline_large.jpg\")\nimg.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T01:13:46.769410Z","iopub.execute_input":"2021-09-14T01:13:46.769686Z","iopub.status.idle":"2021-09-14T01:13:48.514284Z","shell.execute_reply.started":"2021-09-14T01:13:46.769658Z","shell.execute_reply":"2021-09-14T01:13:48.513109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import requests\nurl = 'https://upload.wikimedia.org/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg'\nresponse = urllib.request.urlopen(url)\n\nimg = Image.open(\"Thermopylae_ancient_coastline_large.jpg\")\nimg.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T02:33:27.301137Z","iopub.execute_input":"2021-09-14T02:33:27.301544Z","iopub.status.idle":"2021-09-14T02:33:28.566822Z","shell.execute_reply.started":"2021-09-14T02:33:27.301513Z","shell.execute_reply":"2021-09-14T02:33:28.565921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://stackoverflow.com/questions/40911170/python-how-to-read-an-image-from-a-url\n\nimport urllib.request\n\nURL = 'https://upload.wikimedia.org/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg'\n\nwith urllib.request.urlopen(URL) as url:\n    with open('temp.jpg', 'wb') as f:\n        f.write(url.read())\n\nimg = PIL.Image.open('Thermopylae_ancient_coastline_large.jpg')\n\nimg.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T01:52:42.078665Z","iopub.execute_input":"2021-09-14T01:52:42.078986Z","iopub.status.idle":"2021-09-14T01:52:43.808650Z","shell.execute_reply.started":"2021-09-14T01:52:42.078949Z","shell.execute_reply":"2021-09-14T01:52:43.807797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by  https://stackoverflow.com/questions/7391945/how-do-i-read-image-data-from-a-url-in-python\n\nfrom PIL import Image\nimport urllib.request\nimport io\n\nURL = 'https://upload.wikimedia.org/wikipedia/commons/e/e1/Thermopylae_ancient_coastline_large.jpg'\n\nwith urllib.request.urlopen(URL) as url:\n    f = io.BytesIO(url.read())\n\nimg = Image.open('Thermopylae_ancient_coastline_large.jpg')\n\nimg.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-14T01:56:29.300748Z","iopub.execute_input":"2021-09-14T01:56:29.301022Z","iopub.status.idle":"2021-09-14T01:56:31.034429Z","shell.execute_reply.started":"2021-09-14T01:56:29.300988Z","shell.execute_reply":"2021-09-14T01:56:31.033329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#\"You can't display an image in a PythonAnywhere console. There is no screen for the image to be displayed on. You can probably save it and then view it by going to the saved image in the Files tab.\"\n\nhttps://www.pythonanywhere.com/forums/topic/12686/\n\n#After that answer above, more than 2 hours trying to work with urlLib, Not a single image! I gave up. Forking is Not in my plans.\n\n#Then appeared the message: \"Traffic Ahead\"  That's sound great. (sarcasm) ","metadata":{}}]}