Spaces:
Sleeping
Sleeping
nehapasricha94
commited on
Commit
•
de777bc
1
Parent(s):
408933e
Update app.py
Browse files
app.py
CHANGED
@@ -6,13 +6,13 @@ import matplotlib.pyplot as plt
|
|
6 |
from transformers import pipeline
|
7 |
import gradio as gr
|
8 |
from sklearn.cluster import KMeans
|
|
|
9 |
|
10 |
# Emotion detection pipeline for text (if any text is included in assets)
|
11 |
emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
12 |
|
13 |
# Function to analyze colors in an image
|
14 |
def analyze_colors(image):
|
15 |
-
print(f"Image type: {type(image)}, Image mode: {image.mode}") # Debugging line
|
16 |
try:
|
17 |
# Ensure the image is in RGB format
|
18 |
if image.mode != "RGB":
|
@@ -24,19 +24,9 @@ def analyze_colors(image):
|
|
24 |
# Convert to numpy array
|
25 |
img_array = np.array(image)
|
26 |
|
27 |
-
# Check if the image has a valid shape
|
28 |
-
if img_array.ndim != 3 or img_array.shape[2] != 3:
|
29 |
-
raise ValueError("Invalid image array shape: Expected a 3D array with 3 channels.")
|
30 |
-
|
31 |
# Reshape image to be a list of pixels
|
32 |
pixels = img_array.reshape((-1, 3))
|
33 |
|
34 |
-
print(f"Image shape: {img_array.shape}") # Debugging line
|
35 |
-
print(f"Number of pixels: {len(pixels)}") # Debugging line
|
36 |
-
|
37 |
-
if len(pixels) < 5:
|
38 |
-
return "Image has too few pixels for analysis"
|
39 |
-
|
40 |
kmeans = KMeans(n_clusters=5, random_state=0)
|
41 |
kmeans.fit(pixels)
|
42 |
dominant_colors = kmeans.cluster_centers_
|
@@ -50,30 +40,47 @@ def analyze_colors(image):
|
|
50 |
return dominant_colors
|
51 |
|
52 |
except Exception as e:
|
53 |
-
print(f"Error in analyze_colors: {e}")
|
54 |
return None
|
55 |
|
56 |
|
57 |
-
# Function to
|
58 |
def color_emotion_analysis(dominant_colors):
|
59 |
try:
|
60 |
emotions = []
|
61 |
stress_levels = []
|
62 |
|
|
|
|
|
|
|
|
|
|
|
63 |
for color in dominant_colors:
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
#
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
emotions.append("Sadness")
|
69 |
-
stress_levels.append("High Stress")
|
70 |
-
elif
|
71 |
emotions.append("Neutral")
|
72 |
stress_levels.append("Moderate Stress")
|
73 |
-
elif
|
74 |
emotions.append("Okay")
|
75 |
stress_levels.append("Low Stress")
|
76 |
-
elif
|
77 |
emotions.append("Happiness")
|
78 |
stress_levels.append("Very Low Stress")
|
79 |
else:
|
@@ -86,6 +93,7 @@ def color_emotion_analysis(dominant_colors):
|
|
86 |
print(f"Error in color_emotion_analysis: {e}")
|
87 |
return ["Error analyzing emotions"], ["Error analyzing stress levels"]
|
88 |
|
|
|
89 |
# Function to analyze patterns and shapes using OpenCV
|
90 |
def analyze_patterns(image):
|
91 |
try:
|
@@ -97,12 +105,12 @@ def analyze_patterns(image):
|
|
97 |
num_edges = np.sum(edges > 0)
|
98 |
|
99 |
if num_edges > 10000: # Arbitrary threshold for "chaos"
|
100 |
-
return "Chaotic patterns - possibly distress"
|
101 |
else:
|
102 |
-
return "Orderly patterns - possibly calm"
|
103 |
except Exception as e:
|
104 |
print(f"Error in analyze_patterns: {e}")
|
105 |
-
return "Error analyzing patterns"
|
106 |
|
107 |
# Main function to process image and analyze emotional expression
|
108 |
def analyze_emotion_from_image(image):
|
@@ -116,19 +124,12 @@ def analyze_emotion_from_image(image):
|
|
116 |
if dominant_colors is None:
|
117 |
return "Error analyzing colors"
|
118 |
|
119 |
-
color_emotions,
|
120 |
|
121 |
# Analyze patterns
|
122 |
-
pattern_analysis
|
123 |
-
|
124 |
-
# Combine color and pattern stress levels
|
125 |
-
overall_stress_level = pattern_stress_level if "High Stress" in color_stress_levels else "Moderate Stress"
|
126 |
-
|
127 |
-
return (f"Color-based emotions: {color_emotions}\n"
|
128 |
-
f"Color-based stress levels: {color_stress_levels}\n"
|
129 |
-
f"Pattern analysis: {pattern_analysis}\n"
|
130 |
-
f"Overall stress level: {overall_stress_level}")
|
131 |
|
|
|
132 |
except Exception as e:
|
133 |
return f"Error processing image: {str(e)}"
|
134 |
|
|
|
6 |
from transformers import pipeline
|
7 |
import gradio as gr
|
8 |
from sklearn.cluster import KMeans
|
9 |
+
from colorsys import rgb_to_hsv
|
10 |
|
11 |
# Emotion detection pipeline for text (if any text is included in assets)
|
12 |
emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
13 |
|
14 |
# Function to analyze colors in an image
|
15 |
def analyze_colors(image):
|
|
|
16 |
try:
|
17 |
# Ensure the image is in RGB format
|
18 |
if image.mode != "RGB":
|
|
|
24 |
# Convert to numpy array
|
25 |
img_array = np.array(image)
|
26 |
|
|
|
|
|
|
|
|
|
27 |
# Reshape image to be a list of pixels
|
28 |
pixels = img_array.reshape((-1, 3))
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
kmeans = KMeans(n_clusters=5, random_state=0)
|
31 |
kmeans.fit(pixels)
|
32 |
dominant_colors = kmeans.cluster_centers_
|
|
|
40 |
return dominant_colors
|
41 |
|
42 |
except Exception as e:
|
43 |
+
print(f"Error in analyze_colors: {e}")
|
44 |
return None
|
45 |
|
46 |
|
47 |
+
# Function to analyze emotions based on color (hue, brightness, saturation) and stress with weights
|
48 |
def color_emotion_analysis(dominant_colors):
|
49 |
try:
|
50 |
emotions = []
|
51 |
stress_levels = []
|
52 |
|
53 |
+
# Weight coefficients for each factor
|
54 |
+
brightness_weight = 0.5
|
55 |
+
hue_weight = 0.3
|
56 |
+
saturation_weight = 0.2
|
57 |
+
|
58 |
for color in dominant_colors:
|
59 |
+
# Normalize RGB values to 0-1 range
|
60 |
+
r, g, b = color / 255.0
|
61 |
+
|
62 |
+
# Convert RGB to HSV
|
63 |
+
h, s, v = rgb_to_hsv(r, g, b) # Hue, Saturation, Value (brightness)
|
64 |
|
65 |
+
# Calculate weighted emotion and stress levels
|
66 |
+
weighted_brightness = v * brightness_weight
|
67 |
+
weighted_hue = h * hue_weight
|
68 |
+
weighted_saturation = s * saturation_weight
|
69 |
+
|
70 |
+
# Combine weighted factors
|
71 |
+
score = weighted_brightness + weighted_hue + weighted_saturation
|
72 |
+
|
73 |
+
# Analyze emotion and stress based on combined score
|
74 |
+
if score < 0.3: # Lower combined score, less rigid "high stress"
|
75 |
emotions.append("Sadness")
|
76 |
+
stress_levels.append("Moderate-High Stress")
|
77 |
+
elif 0.3 <= score < 0.5:
|
78 |
emotions.append("Neutral")
|
79 |
stress_levels.append("Moderate Stress")
|
80 |
+
elif 0.5 <= score < 0.7:
|
81 |
emotions.append("Okay")
|
82 |
stress_levels.append("Low Stress")
|
83 |
+
elif 0.7 <= score < 0.85:
|
84 |
emotions.append("Happiness")
|
85 |
stress_levels.append("Very Low Stress")
|
86 |
else:
|
|
|
93 |
print(f"Error in color_emotion_analysis: {e}")
|
94 |
return ["Error analyzing emotions"], ["Error analyzing stress levels"]
|
95 |
|
96 |
+
|
97 |
# Function to analyze patterns and shapes using OpenCV
|
98 |
def analyze_patterns(image):
|
99 |
try:
|
|
|
105 |
num_edges = np.sum(edges > 0)
|
106 |
|
107 |
if num_edges > 10000: # Arbitrary threshold for "chaos"
|
108 |
+
return "Chaotic patterns - possibly distress"
|
109 |
else:
|
110 |
+
return "Orderly patterns - possibly calm"
|
111 |
except Exception as e:
|
112 |
print(f"Error in analyze_patterns: {e}")
|
113 |
+
return "Error analyzing patterns"
|
114 |
|
115 |
# Main function to process image and analyze emotional expression
|
116 |
def analyze_emotion_from_image(image):
|
|
|
124 |
if dominant_colors is None:
|
125 |
return "Error analyzing colors"
|
126 |
|
127 |
+
color_emotions, stress_levels = color_emotion_analysis(dominant_colors)
|
128 |
|
129 |
# Analyze patterns
|
130 |
+
pattern_analysis = analyze_patterns(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
+
return f"Color-based emotions: {color_emotions}\nStress levels: {stress_levels}\nPattern analysis: {pattern_analysis}"
|
133 |
except Exception as e:
|
134 |
return f"Error processing image: {str(e)}"
|
135 |
|