{"cells":[{"metadata":{"collapsed":true,"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.image as mpimg       # reading images to numpy arrays\nimport matplotlib.pyplot as plt        # to plot any graph\nimport matplotlib.patches as mpatches  # to draw a circle at the mean contour\nfrom skimage.data import imread\nfrom sklearn.ensemble import RandomForestClassifier\nimport time\nimport cv2\nfrom scipy.stats import itemfreq\nfrom sklearn.preprocessing import normalize\n\n# To calculate a normalized histogram \n\n\nfrom skimage import measure            # to find shape contour\nimport scipy.ndimage as ndi            # to determine shape centrality\n\n\n# matplotlib setup\n%matplotlib inline\nfrom pylab import rcParams\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\nimport os\nfrom skimage.feature import local_binary_pattern\n#print(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":1,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"18faf7ba-e719-411d-a060-4242b503e11d","_uuid":"9613bfbf9e259a496f75a77e4446a511796fc61a","trusted":true},"cell_type":"code","source":"from pathlib import Path\ninput_path = Path('../input')\ntrain_path = input_path / 'train'\ntest_path = input_path / 'test'\n","execution_count":2,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"bb61a96c-eafe-4955-be74-35084b2119c1","_uuid":"4cc5c39d1b062b7cdbdc3a9a0b2f4aea5af38837","trusted":true},"cell_type":"code","source":"cameras = os.listdir(train_path)\n\ntrain_images = []\nfor camera in cameras:\n    for fname in sorted(os.listdir(train_path / camera)):\n        train_images.append((camera, fname))\n\ntrain = pd.DataFrame(train_images, columns=['camera', 'fname'])\n#print(train.shape)\n","execution_count":3,"outputs":[]},{"metadata":{"scrolled":true,"collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"test_images = []\nfor fname in sorted(os.listdir(test_path)):\n    test_images.append(fname)\n\ntest = pd.DataFrame(test_images, columns=['fname'])\n#print(test.shape)","execution_count":4,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"d6bb0b20-d09b-4246-b0f5-149d197b7a46","_uuid":"ab6e9d60a672547052708d0ae290bf2c8823403f","trusted":true},"cell_type":"code","source":"nt = 10\ntrain_images2 = train_images[:nt]\ntrain_images2 = train_images2 + train_images[1000:1000+nt] \ntrain_images = train_images2\ntest_images = test_images[:nt]","execution_count":5,"outputs":[]},{"metadata":{"scrolled":false,"collapsed":true,"_cell_guid":"3525336f-f374-4b20-8468-1f62542ebc28","_uuid":"1a4c10e1df8d131b1a6024ba0ecd60f008b164f3","trusted":true},"cell_type":"code","source":"X_train = []\ny_train = []\n\nfor i in train_images:\n    i_path = \"../input/train/\" + i[0] + '/' + i[1];\n    img_aux = cv2.imread(i_path)\n    im = img_aux\n    train_image = i_path\n    # Convert to grayscale as LBP works on grayscale image\n    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)\n    radius = 3\n    # Number of points to be considered as neighbourers \n    no_points = 8 * radius\n    # Uniform LBP is used\n    lbp = local_binary_pattern(im_gray, no_points, radius, method='uniform')\n    # Calculate the histogram\n    x = itemfreq(lbp.ravel())\n    # Normalize the histogram\n    hist = x[:, 1]/sum(x[:, 1])\n    # Append image path in X_name\n    # Append histogram to X_name\n    X_train.append(hist)\n    # Append class label in y_test\n    y_train.append(i[0])\n    ","execution_count":6,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"f0f3d916-dd25-4470-b2ff-b02d8cba4f51","_uuid":"f2272bab4d1efe995612fe3170fd6bb65bb2cd4e","trusted":true},"cell_type":"code","source":"#Para submissao\nX_test = []\nX_test_name = []\n\nfor i in test_images:\n    i_path = \"../input/test/\" + '/' + i;\n    X_test_name.append(i)\n    img_aux = cv2.imread(i_path)\n    im = img_aux\n    train_image = i_path\n    # Convert to grayscale as LBP works on grayscale image\n    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)\n    radius = 3\n    # Number of points to be considered as neighbourers \n    no_points = 8 * radius\n    # Uniform LBP is used\n    lbp = local_binary_pattern(im_gray, no_points, radius, method='uniform')\n    # Calculate the histogram\n    x = itemfreq(lbp.ravel())\n    # Normalize the histogram\n    hist = x[:, 1]/sum(x[:, 1])\n    # Append image path in X_name\n    # Append histogram to X_name\n    X_test.append(hist)\n\n    ","execution_count":7,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"ef8ad23e-9238-471f-9408-c9ffc6850cff","_uuid":"e8b7a2fa2f37b2f87c31caf5f7d7eac1cff3ac1d","trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc_X = StandardScaler()\nX_train = sc_X.fit_transform(X_train)\nX_test = sc_X.transform(X_test)","execution_count":8,"outputs":[]},{"metadata":{"scrolled":true,"collapsed":true,"_cell_guid":"adb02e1d-0513-4bbf-a43c-29a37d18c5ab","_uuid":"8d338cbcdfb960761b9b472e2d22ede8fab77ef6","trusted":true},"cell_type":"code","source":"# Fitting Logistic Regression to the Training set\nfrom sklearn.linear_model import LogisticRegression\nclassifier = LogisticRegression(random_state = 0)\nclassifier.fit(X_train, y_train)\ny_pred = classifier.predict(X_test)\n","execution_count":9,"outputs":[]},{"metadata":{"_cell_guid":"9f648846-ba69-40a6-858b-51835c550654","_uuid":"8d12336c864970cc62a1c1123ca1b5bc1fd019e5","trusted":true},"cell_type":"code","source":"output = pd.DataFrame(y_pred)\noutput.to_csv(\"submission.csv\")","execution_count":14,"outputs":[]},{"metadata":{"_cell_guid":"9d04beab-3302-487b-a95f-aa33377abe05","_uuid":"e4a6c2866855364b09484b3b542e2c87aae7cc0c","trusted":true},"cell_type":"code","source":"","execution_count":15,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}