nehapasricha94 commited on
Commit
62873b7
1 Parent(s): de777bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -5
app.py CHANGED
@@ -105,12 +105,53 @@ def analyze_patterns(image):
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):
@@ -127,9 +168,12 @@ def analyze_emotion_from_image(image):
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
 
 
105
  num_edges = np.sum(edges > 0)
106
 
107
  if num_edges > 10000: # Arbitrary threshold for "chaos"
108
+ return "Chaotic patterns - possibly distress", 0.8 # Assigning 0.8 stress for chaotic patterns
109
  else:
110
+ return "Orderly patterns - possibly calm", 0.2 # Assigning 0.2 stress for calm patterns
111
  except Exception as e:
112
  print(f"Error in analyze_patterns: {e}")
113
+ return "Error analyzing patterns", 0.5 # Assigning neutral weight for errors
114
+
115
+
116
+ # Function to compute overall results by combining color and pattern analyses
117
+ def compute_overall_result(color_emotions, stress_levels, pattern_analysis, pattern_stress):
118
+ try:
119
+ # Assigning weightage to different factors
120
+ color_emotion_weight = 0.7 # 70% for color-based emotions and stress
121
+ pattern_weight = 0.3 # 30% for pattern analysis
122
+
123
+ # Determine the most common emotion from colors
124
+ dominant_emotion = max(set(color_emotions), key=color_emotions.count)
125
+
126
+ # Average stress level from color analysis (converting text stress levels to numeric)
127
+ stress_mapping = {
128
+ "No Stress": 0.1,
129
+ "Very Low Stress": 0.3,
130
+ "Low Stress": 0.5,
131
+ "Moderate Stress": 0.7,
132
+ "Moderate-High Stress": 0.9,
133
+ }
134
+ color_stress_numeric = [stress_mapping[stress] for stress in stress_levels if stress in stress_mapping]
135
+ avg_color_stress = np.mean(color_stress_numeric) if color_stress_numeric else 0.5 # Default to 0.5 if no valid data
136
+
137
+ # Compute the overall stress by combining color stress and pattern stress
138
+ overall_stress = (avg_color_stress * color_emotion_weight) + (pattern_stress * pattern_weight)
139
+
140
+ # Determine overall result based on combined factors
141
+ if overall_stress < 0.3:
142
+ overall_emotion = "Calm and Relaxed"
143
+ elif 0.3 <= overall_stress < 0.6:
144
+ overall_emotion = "Neutral Mood"
145
+ elif 0.6 <= overall_stress < 0.8:
146
+ overall_emotion = "Slightly Stressed"
147
+ else:
148
+ overall_emotion = "Highly Stressed"
149
+
150
+ return f"Overall emotion: {overall_emotion} (Dominant Color Emotion: {dominant_emotion}, Pattern Analysis: {pattern_analysis})"
151
+
152
+ except Exception as e:
153
+ return f"Error computing overall result: {str(e)}"
154
+
155
 
156
  # Main function to process image and analyze emotional expression
157
  def analyze_emotion_from_image(image):
 
168
  color_emotions, stress_levels = color_emotion_analysis(dominant_colors)
169
 
170
  # Analyze patterns
171
+ pattern_analysis, pattern_stress = analyze_patterns(image)
172
+
173
+ # Compute overall result
174
+ overall_result = compute_overall_result(color_emotions, stress_levels, pattern_analysis, pattern_stress)
175
 
176
+ return overall_result
177
  except Exception as e:
178
  return f"Error processing image: {str(e)}"
179