Spaces:
Sleeping
Sleeping
nehapasricha94
commited on
Commit
•
408933e
1
Parent(s):
ca568da
Update app.py
Browse files
app.py
CHANGED
@@ -54,23 +54,37 @@ def analyze_colors(image):
|
|
54 |
return None
|
55 |
|
56 |
|
57 |
-
|
58 |
# Function to detect emotions from colors (simplified emotion-color mapping)
|
59 |
def color_emotion_analysis(dominant_colors):
|
60 |
try:
|
61 |
emotions = []
|
|
|
|
|
62 |
for color in dominant_colors:
|
63 |
-
|
64 |
-
|
|
|
|
|
65 |
emotions.append("Sadness")
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
emotions.append("Happiness")
|
|
|
68 |
else:
|
69 |
-
emotions.append("
|
70 |
-
|
|
|
|
|
|
|
71 |
except Exception as e:
|
72 |
print(f"Error in color_emotion_analysis: {e}")
|
73 |
-
return ["Error analyzing emotions"]
|
74 |
|
75 |
# Function to analyze patterns and shapes using OpenCV
|
76 |
def analyze_patterns(image):
|
@@ -83,12 +97,12 @@ def analyze_patterns(image):
|
|
83 |
num_edges = np.sum(edges > 0)
|
84 |
|
85 |
if num_edges > 10000: # Arbitrary threshold for "chaos"
|
86 |
-
return "Chaotic patterns - possibly distress"
|
87 |
else:
|
88 |
-
return "Orderly patterns - possibly calm"
|
89 |
except Exception as e:
|
90 |
print(f"Error in analyze_patterns: {e}")
|
91 |
-
return "Error analyzing patterns"
|
92 |
|
93 |
# Main function to process image and analyze emotional expression
|
94 |
def analyze_emotion_from_image(image):
|
@@ -102,12 +116,19 @@ def analyze_emotion_from_image(image):
|
|
102 |
if dominant_colors is None:
|
103 |
return "Error analyzing colors"
|
104 |
|
105 |
-
color_emotions = color_emotion_analysis(dominant_colors)
|
106 |
|
107 |
# Analyze patterns
|
108 |
-
pattern_analysis = analyze_patterns(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
return f"Color-based emotions: {color_emotions}\nPattern analysis: {pattern_analysis}"
|
111 |
except Exception as e:
|
112 |
return f"Error processing image: {str(e)}"
|
113 |
|
|
|
54 |
return None
|
55 |
|
56 |
|
|
|
57 |
# Function to detect emotions from colors (simplified emotion-color mapping)
|
58 |
def color_emotion_analysis(dominant_colors):
|
59 |
try:
|
60 |
emotions = []
|
61 |
+
stress_levels = []
|
62 |
+
|
63 |
for color in dominant_colors:
|
64 |
+
brightness = np.mean(color)
|
65 |
+
|
66 |
+
# Simple logic for emotion and stress based on brightness
|
67 |
+
if brightness < 85:
|
68 |
emotions.append("Sadness")
|
69 |
+
stress_levels.append("High Stress")
|
70 |
+
elif 85 <= brightness < 120:
|
71 |
+
emotions.append("Neutral")
|
72 |
+
stress_levels.append("Moderate Stress")
|
73 |
+
elif 120 <= brightness < 170:
|
74 |
+
emotions.append("Okay")
|
75 |
+
stress_levels.append("Low Stress")
|
76 |
+
elif 170 <= brightness < 200:
|
77 |
emotions.append("Happiness")
|
78 |
+
stress_levels.append("Very Low Stress")
|
79 |
else:
|
80 |
+
emotions.append("Very Happy")
|
81 |
+
stress_levels.append("No Stress")
|
82 |
+
|
83 |
+
return emotions, stress_levels
|
84 |
+
|
85 |
except Exception as e:
|
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):
|
|
|
97 |
num_edges = np.sum(edges > 0)
|
98 |
|
99 |
if num_edges > 10000: # Arbitrary threshold for "chaos"
|
100 |
+
return "Chaotic patterns - possibly distress", "High Stress"
|
101 |
else:
|
102 |
+
return "Orderly patterns - possibly calm", "Low Stress"
|
103 |
except Exception as e:
|
104 |
print(f"Error in analyze_patterns: {e}")
|
105 |
+
return "Error analyzing patterns", "Error analyzing stress levels"
|
106 |
|
107 |
# Main function to process image and analyze emotional expression
|
108 |
def analyze_emotion_from_image(image):
|
|
|
116 |
if dominant_colors is None:
|
117 |
return "Error analyzing colors"
|
118 |
|
119 |
+
color_emotions, color_stress_levels = color_emotion_analysis(dominant_colors)
|
120 |
|
121 |
# Analyze patterns
|
122 |
+
pattern_analysis, pattern_stress_level = analyze_patterns(image)
|
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 |
|