There was a problem creating dataset for 'Handwrite digit recognition'

Viewed 33
import pyscreenshot as ImageGrab
import time

images_folder = "captured_images/0/"
for i in range(0, 50):
    time.sleep(5)
    im = ImageGrab.grab(bbox=(150,350,900,950)) #x1, y1, x2, y2
    print("saved......",i)
    im.save(images_folder+str(i)+'.png')
    print("claer screen now and redraw now......")

import cv2
import csv
import glob

header = ["label"]
for i in range(0,784):
    header.append("pixel"+str(i))
with open('dataset.csv', 'a') as f:
    writer = csv.writer(f)
    writer.writerow(header)

for label in range(10):
    dirList = glob.glob("captured_images/"+str(label)+"/*.png")
    
    for img_path in dirList:
        im = cv2.imread(img_path)
        im_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
        im_gray = cv2.GaussianBlur(im_gray,(15,15), 0)
        roi = cv2.resize(im_gray,(28,28), interpolation = cv2.INTER_AREA)
        
        data=[]
        data.append(label)
        rows, cols = roi.shape
        
        ## Add pixel one by one into data array
        for i in range(rows):
            for j in range(cols):
                k = roi[i,j]
                if k>100:
                    k = 1
                else:
                    k = 0
                data.append(k)
        with open('dataset.csv', 'a') as f:
            writer = csv.writer(f)
            writer.writerow(header)

import pandas as pd
from sklearn.utils import shuffle
data = pd.read_csv('dataset.csv')
data = shuffle(data)
data

I want to make a 'handwrite digit recognition', but when I checked the datasheet, the datasheet was created like image 1. I need to assign a value only to the place where pixels are, like image 2, so how should I fix the code?

enter image description here enter image description here

1 Answers

You are writing the header into the csv instead of writing the data. It should be changed to

with open('dataset.csv', 'a') as f:
    writer = csv.writer(f)
    writer.writerow(data)
Related