0

I got a big list dataset[i][0] which I appended in pointCollector function.

Right now I was trying to access the dataset[i][0] list outside of the function but I am not sure if this is possible? I getting undefined error when trying to access it.

I need to access it, so that I can write those data list to excel file.

coOrd = [[1671, 2234], [1671, 2235]]


#Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Testing.xlsx')
worksheet = workbook.add_worksheet('Test1')

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': 1})


#Write data headers
row=0
col=0
image_start = 1768 # 1770-2 because k start from 2

title = ['No.', 'Hue', 'Saturation', 'Value',
     'Lightness', 'AComponent', 'BComponent',
     'Blue Channel', 'Green Channel', 'Red Channel']

for j, t in enumerate(title):
    worksheet.write(row + 1, col + j, t, bold)

for k in range(2, 101):
    worksheet.write(row + k , col , 'IMG_%d'%(k + image_start), bold)    

for z, t in enumerate(dataset[0][0]):
    worksheet.write(row + z + 2 , col + 1 , dataset[0][0])


worksheet.set_column(0, 0, 15)
worksheet.set_column('B:J', 13)
worksheet.set_column(10, 10, 15)
worksheet.write(row, col, 'Point 1: Coordinate[1671, 2234]', bold)
worksheet.write(row, col + 11, 'Point 2: Coordinate[1671, 2235]', bold)

workbook.close()

def pointCollector(coOrds):
    """Iterates through all images in the path and loop limits and co-ordinates

    Keyword arguments:
    coOrd -- List of co-ordinates in [x,y] format

    """
    dataset = [[[] for i in range(9)] for j in range(len(coOrd))]   # Declares a 9 * len(coOrd) 2D array for storage 
                                                                    # [[H,S,V,L,A,B],...,n]
    for i in range(0,len(coOrd)):   # Iterates through co-ordinates
        print("Working on: "+ str(coOrd[i]))                                                                 
        for j in range(1770, 1869): # Iterates through images
            print("Working on Images %d:"%(j))


            im_file = path + 'Cropped_Aligned_IMG_' + str(j) + '.png'
            im = cv2.imread(im_file)
            im1 = im.copy()
            im = im.astype(np.float32)                 
            im *= 1./255

            dataset[i][0].append(HSV(im, coOrd[i][0], coOrd[i][1], 0)) #H
            dataset[i][1].append(HSV(im, coOrd[i][0], coOrd[i][1], 1)) #S
            dataset[i][2].append(HSV(im, coOrd[i][0], coOrd[i][1], 2)) #V 
            dataset[i][3].append(LAB(im, coOrd[i][0], coOrd[i][1], 0)) #L
            dataset[i][4].append(LAB(im, coOrd[i][0], coOrd[i][1], 1)) #A
            dataset[i][5].append(LAB(im, coOrd[i][0], coOrd[i][1], 2)) #B
            dataset[i][6].append(BGR(im1, coOrd[i][0], coOrd[i][1], 0)) #Blue
            dataset[i][7].append(BGR(im1, coOrd[i][0], coOrd[i][1], 1)) #Green
            dataset[i][8].append(BGR(im1, coOrd[i][0], coOrd[i][1], 2)) #Red

    return dataset 

1st Attempt to access the dataset inside the function got me this error.

>>> 
========= RESTART: C:\Users\310293649\Desktop\PlotWhiteRegion_7_9.py =========
Traceback (most recent call last):
  File "C:\Users\310293649\Desktop\PlotWhiteRegion_7_9.py", line 41, in <module>
    for z, t in enumerate(dataset[0][0]):
NameError: name 'dataset' is not defined
>>> 

I found this Accessing List from outside function but I am not sure if its the same thing, but well I also dont really understand the solution proposed.

Barmar
  • 741,623
  • 53
  • 500
  • 612
SacreD
  • 363
  • 1
  • 5
  • 16
  • 1
    You're never calling the function. It returns `dataset`, so you should assign the result to a variable and use that. – Barmar Sep 08 '17 at 15:06

1 Answers1

1

You need to call the function and use its return value.

dataset = pointCollector(coOrd)
for z, t in enumerate(dataset[0][0]):
    worksheet.write(row + z + 2 , col + 1 , dataset[0][0])
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I got an error saying "dataset = pointCollector(coOrd) NameError: name 'pointCollector' is not defined" – SacreD Sep 08 '17 at 15:11
  • Functions have to be defined before they're used, so move the function definition to the top of the script. – Barmar Sep 08 '17 at 15:16