Q: Python comparing which RGB value has the most pixels in an image So I am using openCV and I am taking a list of RGB values, comparing them to a selection of pixels from an image and then returning which RGB value from the list there is the most of. I have a file called colorCalc.py and face.py. ColorCalc is just methods while face uses methods from Colorcalc.py. The main issue I have is with the function colorCheck, it seems that the first if statement does not seem to  check as it is always true, which based on the numbers on the calculation from colorCheck, it should not be. The face.py script is also included in case someone wants to replicate what I have done exactly. The below is from ColorCalc.py 
 def colorCheck(src,source):

    prevCount=0
    counter=0;
    ans=20

    for i in range(len(src)):
        counter=0
        for s in range(len(source)):

            if colorDifference(src[0][0],src[0][1],src[0][2],source[0,s,0],source[0,s,1], source[0,s,2])>2:
                counter= counter+1
                print("OKAY")
            if ans== 20:
                    prevCount=counter
                    counter=0

                    ans=src[0,3]
            elif counter>=prevCount:
                    prevCount=counter
                    counter=0
                    ans=src[0,3]

            #print(colorDifference(src[0][0],src[0][1],src[0][2],source[0,s,0],source[0,s,1],source[0,s,2]))       
    return ans

This just creates the array that I am comparing to While the next function compares RGB values
def colorArray():
colArr=[[0 for x in range(3)] for y in range(12)]
#these colors are from darkest to lightest skin tone. The number at the end is to tell which value we are considering after sorting
#black skin
colArr[0]=[117,85,61,0]
colArr[1]=[159,136,121,1]
#bronze Skin
colArr[2]=[156,115,69,2]
colArr[3]=[182,157,69,3]
#medium brown skin
colArr[4]=[196,142,88,4]
colArr[5]=[217,181,149,5]
#light brown skin
colArr[6]=[220,180,119,6]
colArr[7]=[236,195,155,7]
#cream-colored skin
colArr[8]=[230,189,149,8]
colArr[9]=[239,210,180,9]
#pale skin
colArr[10]=[231,207,183,10]
colArr[11]=[240,222,205,11]

colArr= numpy.asarray(colArr)
return colArr

def colorDifference(r,g,b,r1,g1,b1):
red= abs((r-r1)**2)
blue= abs((b-b1)**2)
green= abs((g-g1**2))
answer=math.sqrt(red+blue+green)

return answer;

The next is how I am actually using this, which is the face.py script
    import numpy as np
    import cv2 as cv
    import math
    import colorCalc as col

    #load xml files
    face_cascade = cv.CascadeClassifier('C:\Program 
    Files\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
    eye_cascade = cv.CascadeClassifier('C:\Program 
    Files\opencv\sources\data\haarcascades\haarcascade_eye.xml')

#define image and convert to grayscale
pic= cv.imread('face.jpg')

gray = cv.imread('face.jpg',0)

img= pic
color = np.array([])

#scan for faces and draw rectangle
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
x1= faces[0][0]
x2= faces[0][2]
y1= faces[0][1]
y2= faces[0][3]

This takes a slice of a numpyArray created with openCV
source=img[x1:x1+x2, y1:y1+y2, :]

while this actually calls the function
 colArr=col.colorArray()


    answer=col.colorCheck(colArr,source)

    print(answer)


A: You have a typo.  Change this line
green= abs((g-g1**2))

to
green= abs((g-g1)**2)

