<jupyter_start><jupyter_text>Transforming images into csv<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import glob
import csv
import cv2
# Helper functions
from ocr.helpers import implt
from ocr.datahelpers import correspondingShuffle
# Get locations of images
imglist0 = glob.glob('data/gapdet/0/*.jpg')
imglist1 = glob.glob('data/gapdet/1/*.jpg')

# Load grayscale images
img0 = np.array([np.array(cv2.imread(img, 0)) for img in imglist0])
img1 = np.array([np.array(cv2.imread(img, 0)) for img in imglist1])

# Image dimensions are 30x60 = 1800
print("Char: " + str(len(img0)))
print("Gaps: " + str(len(img1)))
print("Total: " + str(len(img1) + len(img0)))

images = np.concatenate([img0.reshape(len(img0), 1800), img1.reshape(len(img1), 1800)])
labels = np.concatenate([np.zeros(len(img0)), np.ones(len(img1))])

# Shuffle data
images, labels = correspondingShuffle([images, labels])    

# Create CSV files
with open('data/gapdet/data.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    for row in images:
        writer.writerow(row)
        
with open('data/gapdet/labels.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    writer.writerow(labels)
    
print("CSV data files saved.")
# Print two random images
for i in range(len(images)):
    if labels[i] == 0:
        implt(images[i].reshape(60,30), 'gray', 'Char')
        break;
        
for i in range(len(images)):
    if labels[i] == 1:
        implt(images[i].reshape(60,30), 'gray', 'Gap')
        break;<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1595873786407767, 'fraction_numerical': 0.029733009708737865, 'mean_word_length': 4.51505016722408, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '661252', 'license': 'MIT', 'n_tokens_mistral': 633, 'n_tokens_neox': 600, 'n_words': 137, 'path': 'jypeter/Gap-DataCSV.ipynb', 'repo_name': 'HarishankarSomasundaram/handwriting-ocr', 'size': '1648'}