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

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?

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

TypeError: 'numpy.uint8' object is not iterable and put csv file didn't work

im try to put my loop numpy data Modus_citra into csv file, but i've tried using numpy and using normal write csv didn't work
import glob
import cv2
from os import listdir
from os.path import isfile, join
import os
import numpy as np
from sklearn.utils.validation import check_array
import csv
import pandas as pd
def find_mode(np_array) :
vals,counts = np.unique(np_array, return_counts=True)
index = np.argmax(counts)
return(vals[index])
folder = ("C:/Users/ROG FLOW/Desktop/Untuk SIDANG TA/Sudah Aman/testbikincsv/folderdatacitra/*.jpg")
for file in glob.glob(folder):
image = cv2.imread(file)
rows = image.shape[0]
cols = image.shape[1]
middlex = cols/2
middley = rows/2
middle = [middlex,middley]
titikawalx = middlex - 10
titikawaly = middley - 10
titikakhirx = middlex + 10
titikakhiry = middley + 10
crop = image[int(titikawaly):int(titikakhiry), int(titikawalx):int(titikakhirx)]
c = cv2.cvtColor(crop, cv2.COLOR_BGR2HSV)
H,S,V = cv2.split(c)
Modus_citra = (find_mode(H))
data = (Modus_citra)
with open("foo.csv", 'w') as file:
writer = csv.writer(file)
writer.writerows(data)
error = 'numpy.uint8' object is not iterable
foo.csv = from pictures
60
40
19
11
please can someone help me ? i Appreciate every help
According to the edit, you can try:
folder = "C:/Users/ROG FLOW/Desktop/Untuk SIDANG TA/Sudah Aman/testbikincsv/folderdatacitra/*.jpg"
all_data = []
for file in glob.glob(folder):
# ...
Modus_citra = find_mode(H)
all_data.append(Modus_citra) # <-- add the numpy.uint8 to the all_data list
# after the loop write the data to the CSV file:
with open("foo.csv", "w") as file:
writer = csv.writer(file)
for data in all_data:
writer.writerow([data])

script to autosort point vaules not working

Trying to autosort point values from greasest to least, from .txt to .csv
Im trying to sort this: "email#email.com:stuffhere | PointsTotal = 1440"
this is what I currently got;
import csv
import glob
allTxtFiles = glob.glob("txt\\*.txt")
for t in allTxtFiles:
inputFile = open(t,'r').readlines()
endlines = []
sortedLines = []
for e in inputFile:
minNum = e.split("|")
minNum[4] = minNum[4].replace("PointsTotal = ",'')
minNum[4] = minNum[4].strip()
try:
minNum[4] = int(minNum[4])
sortedLines.append(minNum)
except:
endlines.append(minNum)
sortedLines.sort(key=lambda x: int(x[4]),reverse=True)
sortedLines.extend(endlines)
with open("sorted\\"+t.replace("txt\\",'')+".csv",'a+',newline="") as outfile:
writer = csv.writer(outfile)
for s in sortedLines:
writer.writerow(s)

How can I write just one float item in csv?

In this question:
First I read the scores from the csv file and then
I saved an item in the following code in the lw list.
I want to write the lw list in a csv file.
How can I do this?
I read scores from a csv file called alaki.csv:
mandana,5,7,3,15
hamid,3,9,4,20,9,1,8,16,0,5,2,4,7,2,1
sina,19,10,19,6,8,14,3
sara,0,5,20,14
soheila,13,2,5,1,3,10,12,4,13,17,7,7
ali,1,9
sarvin,0,16,16,13,19,2,17,8
import csv
# For the average
from statistics import mean
import operator
from collections import Counter
def calculate_average_of_averages(input_file_name, output_file_name):
#output_file_name=chert.csv
with open(input_file_name) as d:
se = csv.reader(d)
l = {}
for ldf in se:
name = ldf[0]
lsd = mean([float(sd) for sd in ldf[1:]])
l[name] = lsd
with open(output_file_name,'w') as ff:
fd = csv.writer(ff)
a = list(l.values())
lw = []
m = mean(a)
lw.append(m)
calculate_average_of_averages('alaki.csv','chert.csv')
output in csv file:
8.401530612244898
please help me
How about this:
import csv
# For the average
from statistics import mean
import operator
from collections import Counter
def calculate_average_of_averages(input_file_name, output_file_name):
#output_file_name=chert.csv
with open(input_file_name) as d:
se = csv.reader(d)
l = {}
for ldf in se:
name = ldf[0]
lsd = mean([float(sd) for sd in ldf[1:]])
l[name] = lsd
m = mean(list(l.values()))
l["average_of_average"]=m
with open(output_file_name,'w') as ff:
for name,value in l.items():
ff.write("{},{}\n".format(name,value))
calculate_average_of_averages('alaki.csv','chert.csv')
output looks like:
mandana,7.5
hamid,6.066666666666666
sina,11.285714285714286
sara,9.75
soheila,7.833333333333333
ali,5.0
sarvin,11.375
average_of_average,8.401530612244898
to output just average_of_average
replace the write block:
with open(output_file_name,'w') as ff:
ff.write(l['average_of_average'])
You can use the pandas library by adding these 2 lines
import csv
import pandas as pd
# For the average
from statistics import mean
import operator
from collections import Counter
def calculate_average_of_averages(input_file_name, output_file_name):
with open(input_file_name) as d:
se = csv.reader(d)
l = {}
for ldf in se:
name = ldf[0]
lsd = mean([float(sd) for sd in ldf[1:]])
l[name] = lsd
a = list(l.values())
lw = []
m = mean(a)
lw.append(m)
pd.DataFrame(lw,columns=["yourColumn"]).to_csv(output_file_name+".csv")
calculate_average_of_averages('alaki.csv','chert.csv')
I am not sure if CSV writer is necessary to write just one line.
import csv
from statistics import mean
def calculate_mean_of_means(input_file, output_file):
with open(input_file, newline='') as csvfile:
csvreader = csv.reader(csvfile)
ls = {}
for row in csvreader:
str_to_int = [int(i) for i in row[1:]]
ls[row[0]] = str_to_int
total_means = 0
for score in ls.values():
total_means += mean(score)
mean_of_means = [total_means / len(ls)]
with open(output_file, 'w', newline='') as csvfile:
meanwriter = csv.writer(csvfile)
meanwriter.writerow(mean_of_means)
calculate_mean_of_means('alaki.csv', 'chert.csv')

How to use a variable in path?

I have the following code to read three components of two waves and extract each of them to a csv file.
I am still a beginner and so the code is very long, at least I don't want to type the wave name(EHMH011604150003.EW1 EHMH011604150003.NS1 ...) 6 times in my read part. how can I put the Variable that I defined as "name" there?
Any tips on making the code look smart is also appreciated.
Thank you
from pathlib import Path
import os
import numpy as np
import csv
#p =Path('D:/Jobs_2020/RJA/')
#p2 = p/'20160415波形'
#p3 = p2/'kik'
name = 'EHMH011604150003'
# Function 'getKiK-net'
def Convert2Acc(data):
tokens = data.split()
# Scale factor
(Scale, Factor) = tokens[tokens.index('Factor')+1].split('(gal)/')
# Strong motion
items = tokens[tokens.index('Memo.')+1:]
rdata = np.array(items, dtype=np.float64) # rdata: raw data
acc_gal = (rdata - rdata[0]) * float(Scale) / float(Factor)
return acc_gal # acc_gal: Acc. converted unit into gal
# Read data filess
rfile_EW1 = 'D:\\Jobs_2020\\RJA\\20160415波形\\kik\\EHMH011604150003.EW1'
fr_EW1 = open(rfile_EW1, 'r')
EW1_gal = fr_EW1.read()
fr_EW1.close()
rfile_NS1 = 'D:\\Jobs_2020\\RJA\\20160415波形\\kik\\EHMH011604150003.NS1'
fr_NS1 = open(rfile_NS1, 'r')
NS1_gal = fr_NS1.read()
fr_NS1.close()
rfile_UD1 = 'D:\\Jobs_2020\\RJA\\20160415波形\\kik\\EHMH011604150003.UD1'
fr_UD1 = open(rfile_UD1, 'r')
UD1_gal = fr_UD1.read()
fr_UD1.close()
rfile_EW2 = 'D:\\Jobs_2020\\RJA\\20160415波形\\kik\\EHMH011604150003.EW2'
fr_EW2 = open(rfile_EW2, 'r')
EW2_gal = fr_EW2.read()
fr_EW2.close()
rfile_NS2 = 'D:\\Jobs_2020\\RJA\\20160415波形\\kik\\EHMH011604150003.NS2'
fr_NS2 = open(rfile_NS2, 'r')
NS2_gal = fr_NS2.read()
fr_NS2.close()
rfile_UD2 = 'D:\\Jobs_2020\\RJA\\20160415波形\\kik\\EHMH011604150003.UD2'
fr_UD2 = open(rfile_UD2, 'r')
UD2_gal = fr_UD2.read()
fr_UD2.close()
# Store data in array
# _Acc: 2D Array
_Acc1 = [Convert2Acc(EW1_gal), Convert2Acc(NS1_gal), Convert2Acc(UD1_gal)]
Acc1 = np.array(_Acc1).T # Acc: Transposed 2D array to write to .csv file
_Acc2 = [Convert2Acc(EW2_gal), Convert2Acc(NS2_gal), Convert2Acc(UD2_gal)]
Acc2 = np.array(_Acc2).T # Acc: Transposed 2D array to write to .csv file
# Write to .csv file
with open(str(name)+'-1'+'.csv', 'w') as file:
writer = csv.writer(file, lineterminator='\n')
writer.writerows(Acc1)
with open(str(name)+'-2'+'.csv', 'w') as file:
writer = csv.writer(file, lineterminator='\n')
writer.writerows(Acc2)
Something like:
rfile_EW1 = 'D:\\Jobs_2020\\RJA\\20160415波形\\kik\\'+name+'.EW1'
should work.

Python dict to csv

I have written a script to find image size and aspect ratio of all images in a directory along with their corresponding filepaths, I want to print dict values to csv file with following headers width,height,aspect-ratio and filepath
import os
import json
from PIL import Image
folder_images = "/home/user/Desktop/images"
size_images = dict()
def yocd(a,b):
if(b==0):
return a
else:
return yocd(b,a%b)
for dirpath, _, filenames in os.walk(folder_images):
for path_image in filenames:
if path_image.endswith(".png") or path_image.endswith('.jpg') or path_image.endswith('.JPG') or path_image.endswith('.jpeg'):
image = os.path.abspath(os.path.join(dirpath, path_image))
""" ImageFile.LOAD_TRUNCATED_IMAGES = True """
try:
with Image.open(image) as img:
img.LOAD_TRUNCATED_IMAGES = True
img.verify()
print('Valid image')
except Exception:
print('Invalid image')
img = False
if img is not False:
width, heigth = img.size
divisor = yocd(width, heigth)
w = str(int(width / divisor))
h = str(int(heigth / divisor))
aspectratio = w+':'+h
size_images[image] = {'width': width, 'heigth': heigth,'aspect-ratio':aspectratio,'filepath': image}
for k, v in size_images.items():
print(k, '-->', v)
with open('/home/user/Documents/imagesize.txt', 'w') as file:
file.write(json.dumps(size_images))```
You can add a (properly constructed) dict directly to a pandas.DataFrame. Then, DataFrames have a .to_csv() function.
Here are the docs:
Pandas: Create a DataFrame
Pandas: Write to CSV
Without dependencies (but you may have to tweak the formatting)
csv_sep = ';' # choose here wich field separatar you want
with open('your_csv', 'w') as f:
# header
f.write("width"+csv_sep"+height"+csv_sep"+aspect-ratio"+csv_sep+"filepath\n")
# data
for img in size_images:
fields = [img['width'], img['height'], img['aspect-ratio'], img['filepath']]
f.write(csv_sep.join(fields)+'\n')

Categories