{"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 cv2\nimport os\nimport subprocess\nimport matplotlib.pyplot as plt\nimport random\nimport numpy as np \nimport pandas as pd \nimport csv\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-07T03:42:59.466591Z","iopub.execute_input":"2021-10-07T03:42:59.466974Z","iopub.status.idle":"2021-10-07T03:42:59.472478Z","shell.execute_reply.started":"2021-10-07T03:42:59.466940Z","shell.execute_reply":"2021-10-07T03:42:59.471320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LOAD MNIST DATASET","metadata":{}},{"cell_type":"markdown","source":"I will use Vietnamese for this project so feel free to use google translate :))\n\nThe code bellow is [here](https://www.kaggle.com/duansm/crnn-for-mnist/data?fbclid=IwAR1EHvegm9lcSLubJcroL5aZ_nLuDVN02ufqu3erc-pL27F_oa_bjouByo0&select=train.csv)\n\nI guess the author wanted to combine the 2 datasets to get more data\n","metadata":{}},{"cell_type":"code","source":"# 2 datasets\ntrain_data = pd.read_csv('../input/digit-recognizer/train.csv')\n(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n\n# Combine the 2 datasets together\n\ntrain_data1 = np.concatenate([x_train, x_test], axis=0)\nlabels1 = np.concatenate([y_train, y_test], axis=0)\nlabels2 = train_data.label\nlabels = np.concatenate([labels1, labels2], axis=0)\ntrain_data2 = train_data.drop(columns='label')\nimages = np.concatenate([train_data1.reshape([-1,28*28]), train_data2.values], axis=0)\nprint(images.shape)\nprint(labels.shape)\n# Now we have the amount of picture in 2 datasets (112000 pictures), but just a picture of 1 digit\n# 784 = 28x28 it is the 1-D array of 28x28 pixels picture","metadata":{"execution":{"iopub.status.busy":"2021-10-07T03:41:55.043756Z","iopub.execute_input":"2021-10-07T03:41:55.044136Z","iopub.status.idle":"2021-10-07T03:41:59.811660Z","shell.execute_reply.started":"2021-10-07T03:41:55.044106Z","shell.execute_reply":"2021-10-07T03:41:59.810638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PROCESSING THE DATASET\nThe code bellow is from [this repo](https://github.com/dredwardhyde/crnn-ctc-loss-pytorch/blob/main/mnist_sequence_recognition.py)\n\nIt's really hard to understand the original code, the author wanted to feed all of the array of picture into the model, but not me, for some reason. \n\nMay be you tooo. I guess.\n","metadata":{}},{"cell_type":"code","source":"digits_per_sequence = 7 #The number of digits in a picture\nnumber_of_sequences = 100 # the number of pictures you want to generate\ndataset_sequences = []\ndataset_labels = []\n\nfor i in range(number_of_sequences):\n    random_indices = np.random.randint(len(images), size=(digits_per_sequence,)) #Lấy random 7 cái địa chỉ ảnh\n    random_digits_images = images[random_indices] #Lấy 7 ảnh từ 7 địa chỉ đó\n    transformed_random_digits_images = []\n    # Lật hình cho dễ nhìn\n    for img in random_digits_images:\n        img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)\n        img = cv2.flip(img, 1)\n        transformed_random_digits_images.append(img)\n\n    random_digits_images = np.array(transformed_random_digits_images) #ma tran anh chuoi ki tu: ahgdasjdg\n    random_digits_labels = labels[random_indices] #[9 9 9 2 9 8 6]\n\n    random_sequence = np.hstack(random_digits_images.reshape((digits_per_sequence, 28, 28))) #ảnh chuỗi kí tự đang xét\n    random_labels = np.hstack(random_digits_labels.reshape(digits_per_sequence, 1)) #label của ảnh chuỗi kí tự đang xét\n    \n    dataset_sequences.append(random_sequence) # chuỗi các ảnh ahdjagj\n    dataset_labels.append(random_labels) # chuỗi các label của ảnh 9,1,2,7,4,6,7\n\n\nlabels = np.array(dataset_labels)\nimages = np.array(dataset_sequences).reshape([-1, 28,28*digits_per_sequence,1])\n#print('\\n labels',labels)","metadata":{"execution":{"iopub.status.busy":"2021-10-07T03:42:04.042665Z","iopub.execute_input":"2021-10-07T03:42:04.043084Z","iopub.status.idle":"2021-10-07T03:42:04.087070Z","shell.execute_reply.started":"2021-10-07T03:42:04.043053Z","shell.execute_reply":"2021-10-07T03:42:04.086009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SAVING NEW DATASET\n## .PNG file\nNow we will save the images. I wil add all the image into zip file to download instead of png file","metadata":{}},{"cell_type":"code","source":"from zipfile import ZipFile\nimport os\nfor i in range(10000):\n    label = ( \"\".join( str(e) for e in labels[i] ) ) # remove brackets [] of array\n    images[i] = 255 - images[i]\n    name  = str(label +\".png\")\n    cv2.imwrite(name,images[i])\n\nzipObj = ZipFile('multi_digit_images_10k.zip', 'w')\nfor filename in os.listdir(\"/kaggle/working\"):\n    if filename.endswith(\".png\"):\n        zipObj.write(filename)\nzipObj.close()","metadata":{"execution":{"iopub.status.busy":"2021-07-24T17:16:02.690705Z","iopub.execute_input":"2021-07-24T17:16:02.691033Z","iopub.status.idle":"2021-07-24T17:16:05.374934Z","shell.execute_reply.started":"2021-07-24T17:16:02.691003Z","shell.execute_reply":"2021-07-24T17:16:05.373991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"multi_digit_images_10k.zip\"> Download File </a>","metadata":{}},{"cell_type":"markdown","source":"## .CSV file\nHere is updated code for my project\n1. Data augmentation (add noise)\n2. Resize, transpose and normalize image\n3. Saving picture into `.csv` file","metadata":{}},{"cell_type":"code","source":"# Data augmentation\ndef augmentation(image):\n    ''' The dataset is so clean, so it's not fit with out real input images with blobs, noise, ugly characters '''\n    height, width = image.shape \n    num_blob= int(random.gauss(20, 20))\n    for i in range (num_blob):\n        x = random.randint(0,width)\n        y = int (random.gauss(0,height))\n        radius = random.randint(0,3)\n        # add noise\n        image = cv2.circle(image, (x,y), radius = radius, color = ((0, 0, 0) ), thickness = -1) \n        # erode or dilate\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-10-07T03:42:18.415044Z","iopub.execute_input":"2021-10-07T03:42:18.415559Z","iopub.status.idle":"2021-10-07T03:42:18.422263Z","shell.execute_reply.started":"2021-10-07T03:42:18.415527Z","shell.execute_reply":"2021-10-07T03:42:18.421241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Resize, transpose and normalize image\ndef preprocess(img, imgSize ):\n    ''' resize, transpose and standardization grayscale images '''\n    # create target image and copy sample image into it\n    widthTarget, heightTarget = imgSize \n    height, width = img.shape \n    factor_x = width / widthTarget\n    factor_y = height / heightTarget\n\n    factor = max(factor_x, factor_y)\n    # scale according to factor\n    newSize = (min(widthTarget, int(width / factor)), min(heightTarget, int(height / factor)))\n\n    img = cv2.resize(img, newSize)\n    target = np.ones(shape=(heightTarget, widthTarget), dtype='uint8') * 255\n    target[0:newSize[1], 0:newSize[0]] = img\n    # transpose\n    img = cv2.transpose(target)\n    # standardization\n    mean, stddev = cv2.meanStdDev(img)\n    mean = mean[0][0]\n    stddev = stddev[0][0]\n    img = img - mean\n    img = img // stddev if stddev > 0 else img\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-10-07T03:42:20.646495Z","iopub.execute_input":"2021-10-07T03:42:20.646847Z","iopub.status.idle":"2021-10-07T03:42:20.655358Z","shell.execute_reply.started":"2021-10-07T03:42:20.646816Z","shell.execute_reply":"2021-10-07T03:42:20.654636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Write image to `csv` file","metadata":{}},{"cell_type":"code","source":"header = 'label image'\nheader = header.split()\nwith open('./aug_digit_data.csv', \"w\", encoding=\"utf-8\") as f:\n    writer = csv.writer(f)\n    writer.writerow(header)\n\nfor i,image in enumerate(images):\n    label = \"'\" + ( \"\".join( str(e) for e in labels[i] ) ) # bỏ ngoặc, thêm dấu '\n    # I have to add \"'\" bcuz csv will change label 0345 into 345\n    image = 255 - image\n    image = image[:,:,0].astype('uint8')\n\n    image = augmentation(image)\n    image = preprocess(image, imgSize = (128, 32))\n\n    image = image.flatten()\n    #print ('len image',len(image))\n    image = ( \" \".join( str(e) for e in image ) )\n    value = [label, image]\n\n    with open('./aug_digit_data.csv', \"a\", encoding=\"utf-8\") as f:\n        writer = csv.writer(f)\n        writer.writerow(value)\n\nprint('done')","metadata":{"execution":{"iopub.status.busy":"2021-10-07T03:43:05.855128Z","iopub.execute_input":"2021-10-07T03:43:05.855482Z","iopub.status.idle":"2021-10-07T03:43:06.203924Z","shell.execute_reply.started":"2021-10-07T03:43:05.855452Z","shell.execute_reply":"2021-10-07T03:43:06.202798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's see how to work with csv file and what inside","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('./aug_digit_data.csv')\n\nlabels = data.iloc[: ,0].to_list()\nimages = data.iloc[: ,1].to_list()\nt = []\nfor image in images:\n    image = image.split(' ')\n    image = np.array(image, dtype = float)\n    t.append(image)\nimages = t\nt = []\nimages = np.array(images).reshape(-1, 128, 32, 1)\n\nfor label in labels:\n    label = label.split(\"'\")[1] #remove ' comma\n    t.append(label)\nlabels = t\nt = []\n\nplt.figure(num='multi digit',figsize=(9,18))\nfor i in range(6):\n\n    plt.subplot(3,3,i+1) \n    plt.title(labels[i])\n    plt.imshow(np.squeeze(images[i,:,:,]))\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-07T03:43:23.246470Z","iopub.execute_input":"2021-10-07T03:43:23.247033Z","iopub.status.idle":"2021-10-07T03:43:23.797930Z","shell.execute_reply.started":"2021-10-07T03:43:23.246986Z","shell.execute_reply":"2021-10-07T03:43:23.797141Z"},"trusted":true},"execution_count":null,"outputs":[]}]}