{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"427e6e08-8e8d-b592-cd3f-e9e70abe4f8b"},"source":"**Read files**"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"490a761c-9b8c-4260-21e2-245579921036"},"outputs":[],"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"938228a2-59f1-e668-9f17-203ac664c2e9"},"outputs":[],"source":"import os\nfrom glob import glob\nTRAIN_DATA = \"../input/train\"\ntype_1_files = glob(os.path.join(TRAIN_DATA, \"Type_1\", \"*.jpg\"))\ntype_1_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_1\"))+1:-4] for s in type_1_files])\ntype_2_files = glob(os.path.join(TRAIN_DATA, \"Type_2\", \"*.jpg\"))\ntype_2_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_2\"))+1:-4] for s in type_2_files])\ntype_3_files = glob(os.path.join(TRAIN_DATA, \"Type_3\", \"*.jpg\"))\ntype_3_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_3\"))+1:-4] for s in type_3_files])\n\nprint(len(type_1_files), len(type_2_files), len(type_3_files))\nprint(\"Type 1\", type_1_ids[:10])\nprint(\"Type 2\", type_2_ids[:10])\nprint(\"Type 3\", type_3_ids[:10])"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"7c384cac-5edb-1f81-c551-e12afcc461d4"},"outputs":[],"source":"def get_filename(image_id, image_type):\n    \"\"\"\n    Method to get image file path from its id and type   \n    \"\"\"\n    if image_type == \"Type_1\" or \\\n        image_type == \"Type_2\" or \\\n        image_type == \"Type_3\":\n        data_path = os.path.join(TRAIN_DATA, image_type)\n    elif image_type == \"Test\":\n        data_path = TEST_DATA\n    elif image_type == \"AType_1\" or \\\n          image_type == \"AType_2\" or \\\n          image_type == \"AType_3\":\n        data_path = os.path.join(ADDITIONAL_DATA, image_type[1:])\n    else:\n        raise Exception(\"Image type '%s' is not recognized\" % image_type)\n\n    ext = 'jpg'\n    return os.path.join(data_path, \"{}.{}\".format(image_id, ext))\n\n\ndef get_image_data(image_id, image_type):\n    \"\"\"\n    Method to get image data as np.array specifying image id and type\n    \"\"\"\n    fname = get_filename(image_id, image_type)\n    img = cv2.imread(fname)\n    assert img is not None, \"Failed to read image : %s, %s\" % (image_id, image_type)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    return img"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"59bdb1c3-6f8c-252b-0e18-9fc20fb01751"},"outputs":[],"source":"import matplotlib.pylab as plt\n\ndef plt_st(l1,l2):\n    plt.figure(figsize=(l1,l2))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"97e347f9-63da-97d6-4f9f-0b71267528d5"},"outputs":[],"source":"img_1 = get_image_data('208', 'Type_1')\nfile_1 = get_filename('208','Type_1')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"09e64841-0b00-2d46-857b-25d284752680"},"outputs":[],"source":"plt_st(20, 20)\nplt.imshow(img_1)\nplt.title(\"Training dataset of type %i\" % (1))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b85cc41f-75c2-78c1-c1a2-fde100afe122"},"outputs":[],"source":"from PIL import Image, ImageDraw\nfrom PIL import ImageFont\n\nimag = Image.open(file_1)\n#Convert the image te RGB if it is a .gif for example\nimag = imag.convert ('RGB')\n#coordinates of the pixel\nX,Y = 0,0\n#Get RGB\npixelRGB = imag.getpixel((X,Y))\nR,G,B = pixelRGB \n#Luminance photometric/digital\nY = 0.2126*R + 0.7152*G + 0.0722*B\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b3e9733b-57b3-dcfc-6e06-4a2813f12644"},"outputs":[],"source":" def luminosity(rgb, rcoeff=0.2126, gcoeff=0.7152, bcoeff=0.0722):\n    return rcoeff*rgb[0] + gcoeff*rgb[1] + bcoeff*rgb[2]\n\ndef gen_pix_factory(im):\n    num_cols, num_rows=im.size\n    r, c = 0,0\n    while r!=num_rows:\n        c = c%num_cols\n        print (c)\n        print (r)\n        yield ((c,r), im.getpixel((c,r)) \n        if c == (num_cols-1): \n               r+=1\n        c+=1\ndef rgb_to_gray_level(rgb_img, conversion=luminosity):        \n        gl_img = Image.new('L',rgb_img.size)\n        gen_pix = gen_pix_factory(gl_img)\n        lum_pix = ((gp[0],conversion(gp[1])) for gp in gen_pix)\n        for lp in lum_pix:\n            gl_img.putpixel(lp[0],int(lp[1]))\n        return gl_img\n    \ndef binarize(gl_img, thresh=70):\n    gen_pix = gen_pix_factory(gl_img)\n    for pix in gen_pix:\n        if pix[1] <= thesh:\n            gl_img.putpixel(pix[0], 0)\n        else:\n            gl_img.putpixel(pix[0],255)\n        \n        "},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"734d3ae1-afc0-b5db-158e-cf7852658789"},"outputs":[],"source":"from PIL import Image, ImageDraw\nfrom PIL import ImageFont\nim = Image.open(file_1)\nim2 = rgb_to_gray_level(im, conversion=luminosity)\nplt_st(20, 20)\nplt.imshow(im2)\nplt.title(\"Training dataset of type %i\" % (1))"}],"metadata":{"_change_revision":0,"_is_fork":false,"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.6.0"}},"nbformat":4,"nbformat_minor":0}