Team Ai
Apppublic

codeShery/Colordetection

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py59 linesDownload Raw Back to root
1import streamlit as st2from PIL import Image3from collections import Counter4import pandas as pd5import numpy as np6 7def get_dominant_colors(image, num_colors=5):8    """9    Extract the most dominant colors from an image.10    11    Args:12        image: A PIL image object.13        num_colors: The number of dominant colors to detect.14    15    Returns:16        A list of RGB tuples representing the most dominant colors.17    """18    image = image.resize((150, 150))  # Resize for faster processing19    data = np.array(image)20    pixels = data.reshape(-1, 3)  # Flatten the 2D image into 1D array21    counter = Counter(map(tuple, pixels))22    dominant_colors = counter.most_common(num_colors)23    return dominant_colors24 25# Streamlit app26st.title("Image Color Detector")27st.subheader("Upload an image to detect its most dominant colors!")28 29# File uploader30uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])31 32if uploaded_file is not None:33    # Load the image34    image = Image.open(uploaded_file)35    st.image(image, caption="Uploaded Image", use_column_width=True)36 37    # Get dominant colors38    num_colors = st.slider("Select the number of colors to detect", min_value=1, max_value=10, value=5)39    dominant_colors = get_dominant_colors(image, num_colors=num_colors)40 41    # Display the dominant colors42    st.subheader("Detected Colors")43    color_data = []44    for color, count in dominant_colors:45        hex_color = "#{:02x}{:02x}{:02x}".format(color[0], color[1], color[2])46        color_data.append({"RGB": color, "Hex": hex_color, "Count": count})47        st.write(f"**Color:** {hex_color} | **RGB:** {color} | **Pixels Count:** {count}")48        st.markdown(f"<div style='width:50px; height:25px; background-color:{hex_color};'></div>", unsafe_allow_html=True)49 50    # Create a DataFrame for better visualization51    df = pd.DataFrame(color_data)52    st.write("### Detailed Color Data", df)53else:54    st.info("Please upload an image to start!")55 56# Footer57st.write("---")58st.write("Made with ❤️ using Streamlit.")59