# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import os
import cv2
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from tqdm import tqdm
import pickle

save_location = './Guatemala 2021 images/sorted_cherries'
save_series_filename = 'saved_HSV_series.pkl'
fig_filename = 'HSV_distributions.png'
cherries = ['red_ripe', 'red_underripe', 'red_overripe', 'yellow_ripe', 'yellow_overripe']
foldernames = {cherries[i]: cherries[i] for i in range(len(cherries))}
colors = {cherries[0]:'red',cherries[1]:'green', cherries[2]:'brown', cherries[3]:'yellow', cherries[4]: 'orange'}

s_hue = {}
s_sat = {}
s_val = {}
for cherry in cherries:
    s_hue[cherry] = pd.Series([], dtype='float64')
    s_sat[cherry] = pd.Series([], dtype='float64')
    s_val[cherry] = pd.Series([], dtype='float64')  

s_hue

for cherry in cherries:

    # get all the files
    file_location = os.path.join(save_location, foldernames[cherry]) 
    files = [file for file in os.listdir(file_location) if file.endswith(".jpg")]
    
    print('processing imgs for cherry : {}'.format(cherry))
    # accumulate all pixels into a pandas series each for Hue, Saturation, Value
    for i, curr_file in tqdm(enumerate(files)):
        img = cv2.imread(os.path.join(file_location,curr_file))
        
        img_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        hue = pd.Series(img_HSV[:,:,0].ravel())
        sat = pd.Series(img_HSV[:,:,1].ravel())
        val = pd.Series(img_HSV[:,:,2].ravel())
        
        s_hue[cherry] = pd.concat([s_hue[cherry], hue], ignore_index=True)
        s_sat[cherry] = pd.concat([s_sat[cherry], sat], ignore_index=True)
        s_val[cherry] = pd.concat([s_val[cherry], val], ignore_index=True)

with open(os.path.join(save_location,save_series_filename), 'wb') as handle:
    pickle.dump([s_hue, s_sat, s_val], handle, protocol = pickle.HIGHEST_PROTOCOL)

# +
fig = plt.figure(figsize =(30, 12))
sns.set_style("white")
kwargs = dict(hist_kws={'alpha':.5}, kde_kws={'linewidth':2})

ax0 = fig.add_subplot(1, 3, 1)
ax0.set_title('Normalized histogram + KDF (HUE)', fontsize = 34)
for cherry in cherries:
    sns.displot(s_hue[cherry], ax=ax0, color=colors[cherry], label = cherry, **kwargs)
ax0.legend(fontsize = 24)
ax0.set_xlabel("Hue", fontsize = 24)

ax1 = fig.add_subplot(1, 3, 2)
ax1.set_title('Normalized histogram + KDF (SATURATION)', fontsize = 34)
for cherry in cherries:
    sns.displot(s_sat[cherry], ax=ax1, color=colors[cherry], label = cherry, **kwargs)
ax1.set_xlabel("Saturation", fontsize = 24)
ax1.legend(fontsize = 24)

ax2 = fig.add_subplot(1, 3, 3)
ax2.set_title('Normalized histogram + KDF (VALUE)', fontsize = 34)
for cherry in cherries:
    sns.displot(s_val[cherry], ax=ax2, color=colors[cherry], label = cherry, **kwargs)
ax2.set_xlabel("Value", fontsize = 24)
ax2.legend(fontsize = 24)

plt.savefig(os.path.join(save_location, fig_filename), dpi = 80)
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered