Spaces:
Running
Running
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
from sklearn.cluster import KMeans
|
5 |
+
|
6 |
+
def get_dominant_color(image, k=1):
|
7 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
8 |
+
image = image.reshape((-1, 3))
|
9 |
+
|
10 |
+
kmeans = KMeans(n_clusters=k, random_state=0, n_init=10)
|
11 |
+
kmeans.fit(image)
|
12 |
+
|
13 |
+
dominant_color = kmeans.cluster_centers_[0].astype(int)
|
14 |
+
return tuple(dominant_color)
|
15 |
+
|
16 |
+
def is_warm_or_cool(color):
|
17 |
+
r, g, b = color
|
18 |
+
warm = (r > g and r > b)
|
19 |
+
return "Warm" if warm else "Cool"
|
20 |
+
|
21 |
+
def complementary_color(color):
|
22 |
+
r, g, b = color
|
23 |
+
return (255 - r, 255 - g, 255 - b)
|
24 |
+
|
25 |
+
st.title("VQA for Colors and Color Theory")
|
26 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
27 |
+
|
28 |
+
if uploaded_file is not None:
|
29 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
30 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
31 |
+
|
32 |
+
dominant = get_dominant_color(image)
|
33 |
+
comp_color = complementary_color(dominant)
|
34 |
+
|
35 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image", use_column_width=True)
|
36 |
+
st.write(f"**Dominant Color:** {dominant}")
|
37 |
+
st.write(f"**Temperature:** This is a {is_warm_or_cool(dominant)} color.")
|
38 |
+
st.write(f"**Complementary Color:** {comp_color}")
|