File size: 1,276 Bytes
caefe2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cv2
import numpy as np
import streamlit as st
from sklearn.cluster import KMeans

def get_dominant_color(image, k=1):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = image.reshape((-1, 3))
    
    kmeans = KMeans(n_clusters=k, random_state=0, n_init=10)
    kmeans.fit(image)
    
    dominant_color = kmeans.cluster_centers_[0].astype(int)
    return tuple(dominant_color)

def is_warm_or_cool(color):
    r, g, b = color
    warm = (r > g and r > b)
    return "Warm" if warm else "Cool"

def complementary_color(color):
    r, g, b = color
    return (255 - r, 255 - g, 255 - b)

st.title("VQA for Colors and Color Theory")
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
    image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
    
    dominant = get_dominant_color(image)
    comp_color = complementary_color(dominant)
    
    st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image", use_column_width=True)
    st.write(f"**Dominant Color:** {dominant}")
    st.write(f"**Temperature:** This is a {is_warm_or_cool(dominant)} color.")
    st.write(f"**Complementary Color:** {comp_color}")