Why aren't my sets containing more than one element?- python 2.7
Question: Ok, so I wrote some code and I want to compare two sets. However, the length
will only return either 0 or 1, depending on whether I'm using two images or
the same image. This is because my sets are being formed as only 1 element
sets instead of mixing the numbers apart. For example, the sets read as [(a,
b, c)] instead of [('a', 'b', 'c')].

Here's my code

    
    
    import cv2
    import numpy as np
    import time
    N=0
    colour=[]
    colourfile=open('Green from RGB.txt', 'r')
    for line in colourfile.readlines():
        colour.append([line])
    colour_set=sorted(set(map(tuple, colour)))
    
    
    
    def OneNumber(im): #Converts the pixels rgb to a single number.
        temp_im=im.astype('int32')
        r,g,b = temp_im[:,:,0], temp_im[:,:,1], temp_im[:,:,2]
        combo=r*1000000+g*1000+b
        return combo
    
    
    
    while True:
        cam = cv2.VideoCapture(0)
        start=time.time()
        while(cam.isOpened()):                  #Opens camera
            ret, im = cam.read()                #Takes screenshot
            #im=cv2.imread('filename.type')
            im=cv2.resize(im,(325,240))         #Resize to make it faster
            im= im.reshape(1,-1,3)
            im=OneNumber(im)               #Converts the pixels rgb to a singe number
            im_list=im.tolist()                 #Makes it into a list
            im_set=set(im_list[0])              #Makes set
            ColourCount= set(colour_set) & set(colour_set) #or set(im_set) for using/ comparing camera
            print len(ColourCount)
    

Also the text file I'm opening is written as:

    
    
    126255104, 8192000, 249255254, 131078, 84181000, 213254156,
    

In a single, great big line.

So basically, how do I divide the numbers into different elements in the sets,
im_set and colour_set?

Thanks


Answer: You have a few bugs in your code. It looks like you are reading all the colors
into a single string. You need to split the string instead if you want a set
of colors:

    
    
    for line in colourfile.readlines():
        temp_line = [x.strip() for x in line.split(',')]  ## create a temporary list, splitting on commas, and removing extra whitesapce
        colour.extend(temp_line)  ## don't put brackets around `line`, you add another "layer" of lists to the list
         ## also don't `append` a list with a list, use `extend()` instead
    #colour_set=sorted(set(map(tuple, colour)))  ## I think you're trying to convert a string to a 3-tuple of rgb color values.  This is not how to do that
    

You have a serious problem with your rgb color representations: what is
`131078`? Is it (13, 10, 78), or (131, 0, 78), or (1, 31, 78)? You need to
change how those color strings are written to a file, because your format is
ambiguous. To keep it simple, why not write it to a file like this:

    
    
    13 10 78
    255 255 0
    

If you insist on encoding the rgb triples as a single string, then you _HAVE_
to zero-pad all the values:

    
    
    ## for example
    my_rgb = (13,10,78)
    my_rgb_string = "%03d%03d%03d" % (r, g, b)  ## zero-pad to exactly 3 digit width
    print(my_rgb_string)
    >> 013010078
    

Another problem: you are intersecting a set with itself, instead of
intersecting two different sets:

    
    
    ColourCount= set(colour_set) & set(colour_set) #or set(im_set) for using/ comparing camera
    

should be something like:

    
    
    ColourCount= set(colour_set) | im_set #or set(im_set) for using/ comparing camera
    

if you want to create a union of all the different colors in the image.

If you still have problems after fixing these issues, I would recommend you
post a new question with the updated code.

