<jupyter_start><jupyter_text>Pandas is used because it's provind the most effective way to read the csv filecv2 is used to argparse<jupyter_code>import cv2
import pandas as pd
import argparse<jupyter_output><empty_output><jupyter_text>Creating argument parser to take image path from command line<jupyter_code>ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True, help="Image Path")
args = vars(ap.parse_args())
img_path = args['image']<jupyter_output><empty_output><jupyter_text>imread will return an 2D array which will include RGB code for all pixel in the image<jupyter_code>img = cv2.imread(img_path)<jupyter_output><empty_output><jupyter_text>Declaring global variables (are used later on)<jupyter_code>clicked = False
r = g = b = xpos = ypos = 0<jupyter_output><empty_output><jupyter_text>Reading csv file with pandas and giving names to each column<jupyter_code>index=["color","color_name","hex","R","G","B"]
csv = pd.read_csv('colors.csv', names=index, header=None)<jupyter_output><empty_output><jupyter_text>namedWindow is a c2v meathod which open's the passed image in a new windowsetMouseCallback is also a c2v meathod which takes two parameters i.e. the image and a function to get the x and y coordinates of the mouse click<jupyter_code>cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_function)<jupyter_output><empty_output><jupyter_text>getColorName is the function to calculate minimum distance from all colors and get the most matching color<jupyter_code>def getColorName(R,G,B):
    minimum = 10000
    for i in range(len(csv)):
        d = abs(R- int(csv.loc[i,"R"])) + abs(G- int(csv.loc[i,"G"]))+ abs(B- int(csv.loc[i,"B"]))
        if(d<=minimum):
            minimum = d
            cname = csv.loc[i,"color_name"]
    return cname<jupyter_output><empty_output><jupyter_text>draw_function is the function to get x,y coordinates of mouse double clickUsually we use RGB but in openCV we use BGRFrom the x position and the y position we can find the Blue, Green and Red Color value in the img array<jupyter_code>def draw_function(event, x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        global b,g,r,xpos,ypos, clicked
        clicked = True
        xpos = x
        ypos = y
        b,g,r = img[y,x]
        b = int(b)
        g = int(g)
        r = int(r)<jupyter_output><empty_output><jupyter_text>Below while loop will run unlil escape(esc) key is not pressed.Per double left click the loop will - Take image x and y location- Create a area to show image color- Put text in the area that include the color name- Addition feature like if color is light(difficult to be seen) then text color will be black<jupyter_code>while(1):
    cv2.imshow("image",img)
    if (clicked):
        cv2.rectangle(img,(20,20), (750,60), (b,g,r), -1)
        text = getColorName(r,g,b) + ' R='+ str(r) +  ' G='+ str(g) +  ' B='+ str(b)
        cv2.putText(img, text,(50,50),2,0.8,(255,255,255),2,cv2.LINE_AA)
        if(r+g+b>=600):
            cv2.putText(img, text,(50,50),2,0.8,(0,0,0),2,cv2.LINE_AA)
        clicked=False
    if cv2.waitKey(20) & 0xFF ==27:
        break<jupyter_output><empty_output><jupyter_text>Python Opencv destroyAllWindow() function allows users to destroy all windows at any time. It doesn’t take any parameters and doesn’t return anything. It is similar to destroyWIndow(). destroyWindow() only destroys a specific window but in the case of destroyAllWindow() it destroys all windows.<jupyter_code>cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11390616167326173, 'fraction_numerical': 0.01978518937252685, 'mean_word_length': 4.708064516129032, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '520860', 'license': 'MIT', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1189, 'n_words': 352, 'path': 'color_detection.ipynb', 'repo_name': 'AnmolVerma404/Color-Detection', 'size': '3538'}