<jupyter_start><jupyter_text>object-detection ref : https://towardsdatascience.com/object-detection-with-less-than-10-lines-of-code-using-python-2d28eebc5b11 color-extraction ref : https://code.likeagirl.io/finding-dominant-colour-on-an-image-b4e075f98097<jupyter_code>import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox

import numpy as np
from sklearn.cluster import KMeans
from collections import Counter
import json<jupyter_output>Using TensorFlow backend.<jupyter_text>1. Load image file<jupyter_code>file_path = '../data/pictures_tissemrz/tissemrz_024_3116Likes.png'
img = cv2.imread(file_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

print(f'>>> type of img     : {type(img)}')
print(f'>>> size of matrix : {img.shape}')
plt.imshow(img)
plt.show()<jupyter_output>>>> type of img     : <class 'numpy.ndarray'>
>>> size of matrix : (1350, 1080, 3)<jupyter_text>2. Detect the object 'person' from image<jupyter_code>bbox, label, conf = cv.detect_common_objects(img)
print('=== outputs ===')
print(f'bounding boxes    : {bbox}')  # x, y, x+width, y+height
print(f'detected objects  : {label}')
print(f'confidence levels : {conf}\n')

if label.count('person') > 1:   # Exceptional case : 2 or more persons are detected
    print('>>> Warning : More than 1 person is detected from the given picture!')
    idx = label.index('person')
    bbox = [bbox[idx]]
    label = [label[idx]]
    conf = [conf[idx]]
    
    output_image = draw_bbox(img, bbox, label, conf)
    plt.imshow(output_image)
    plt.show()
elif 'person' in label:   # Ideal case : only one main preson is detected
    idx = label.index('person')
    bbox = [bbox[idx]]
    label = [label[idx]]
    conf = [conf[idx]]
    
    output_image = draw_bbox(img, bbox, label, conf)
    plt.imshow(output_image)
    plt.show()
else:   # Exceptional case : No person is detected
    print('>>> Warning : No person is detected from the given picture!')
    output_image = draw_bbox(img, bbox, label, conf)
    plt.imshow(output_image)
    plt.show()<jupyter_output>=== outputs ===
bounding boxes    : [[230, 362, 696, 1354], [808, 592, 1076, 694], [192, 934, 430, 1220], [283, 488, 319, 534]]
detected objects  : ['person', 'car', 'handbag', 'traffic light']
confidence levels : [0.9996593594551086, 0.9793494343757629, 0.9627959132194519, 0.8630921244621277]<jupyter_text>3. Extract dominant colors of detected area (foreground)  3-1. Preprocess image<jupyter_code># crop the image
x = bbox[0][0]
y = bbox[0][1]
x_end = bbox[0][2]
y_end = bbox[0][3]

length_mod = int((y_end - y) * 0.2)
width_mod = int((x_end - x) * 0.2)

img = img[ (y+length_mod):(y_end-length_mod), (x+width_mod):(x_end-width_mod) ]

#represent as row*column,channel number (appropriate format for KMeans)
img_reshape = img.reshape((img.shape[0] * img.shape[1], 3))<jupyter_output><empty_output><jupyter_text>3-2. Extract color<jupyter_code># arrange the cluster_centers of KMeans output according to frequency of each label
def arrange_cluster_centers(clt):
    lb_counts = Counter(clt.labels_)
    
    tmp_dict = {}
    for idx, pixel in enumerate(clt.cluster_centers_):
        tmp_dict[ lb_counts[idx] ] = pixel  # { frequency of label : color of label } mapping
    
    centers_aranged_dict = dict(sorted(tmp_dict.items(), reverse=True))  # arrange the dict according to key (frequency of label)

    centers_aranged_arr = np.array(list(centers_aranged_dict.values()))  # pack colors into one array
    centers_aranged_arr = centers_aranged_arr.round().astype("uint8")  # tranform the type to integer
    centers_aranged_arr = np.expand_dims(centers_aranged_arr, 0)  # add one dimension for visualization
    return centers_aranged_arr


expected_num_col = 5 #cluster number

# Extract dominant color using KMeans
clt = KMeans(n_clusters = expected_num_col)
clt.fit(img_reshape)

# Visualization
centers_aranged_arr = arrange_cluster_centers(clt)

plt.imshow(centers_aranged_arr)
plt.show()<jupyter_output><empty_output><jupyter_text>3-3. Compare to the original image (visualization)<jupyter_code>plt.imshow(img)
plt.show()<jupyter_output><empty_output><jupyter_text>4. Save top 2 colors into json<jupyter_code>filename = file_path.split('/')[-1]

r_dict = {filename : centers_aranged_arr.tolist()[0][:2]}
r_json = json.dumps(r_dict)
r_json<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1310123683005039, 'fraction_numerical': 0.043976179569399906, 'mean_word_length': 5.056865464632455, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 46, 'https://': 2, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '83396', 'license': 'MIT', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1589, 'n_words': 365, 'path': 'color_extract/foreground_extract.ipynb', 'repo_name': 'sam351/Trip_Fashion_Recommend', 'size': '4366'}