nehapasricha94
commited on
Commit
•
34c5a4e
1
Parent(s):
7fda483
Update app.py
Browse files
app.py
CHANGED
@@ -171,33 +171,37 @@ def analyze_emotion_from_text(text):
|
|
171 |
|
172 |
# Main function to process image and analyze emotional expression
|
173 |
def analyze_emotion_from_image(image):
|
174 |
-
print(f"Color emotions: abc")
|
175 |
try:
|
176 |
-
# Ensure the input image is a PIL image
|
177 |
print(f"Initial input type: {type(image)}")
|
178 |
-
|
179 |
-
# Check if the input is a URL
|
180 |
-
if isinstance(image, str):
|
181 |
print(f"Loading image from URL: {image}")
|
182 |
response = requests.get(image, stream=True)
|
183 |
response.raise_for_status() # Raise an error for bad responses
|
184 |
-
image = Image.open(response.
|
185 |
print("Loaded image from URL.")
|
186 |
|
187 |
-
# Check if the input is a
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
elif isinstance(image, dict) and "blob" in image:
|
189 |
blob_data = image["blob"]
|
190 |
-
image = Image.open(blob_data).convert("RGB") #
|
191 |
print("Loaded image from Blob data.")
|
192 |
|
193 |
# Check if the input is a NumPy array
|
194 |
elif isinstance(image, np.ndarray):
|
195 |
-
image = Image.fromarray(image).convert("RGB") # Convert to
|
196 |
print("Converted image from NumPy array.")
|
197 |
|
198 |
print(f"Image size: {image.size}, mode: {image.mode}")
|
199 |
|
200 |
-
# Analyze colors
|
201 |
dominant_colors = analyze_colors(image)
|
202 |
if dominant_colors is None:
|
203 |
return "Error analyzing colors"
|
@@ -205,11 +209,11 @@ def analyze_emotion_from_image(image):
|
|
205 |
color_emotions, stress_levels = color_emotion_analysis(dominant_colors)
|
206 |
print(f"Color emotions: {color_emotions}, Stress levels: {stress_levels}")
|
207 |
|
208 |
-
# Analyze patterns
|
209 |
pattern_analysis, pattern_stress = analyze_patterns(image)
|
210 |
print(f"Pattern analysis: {pattern_analysis}, Pattern stress: {pattern_stress}")
|
211 |
|
212 |
-
# Compute overall result
|
213 |
overall_result = compute_overall_result(color_emotions, stress_levels, pattern_analysis, pattern_stress)
|
214 |
|
215 |
return overall_result
|
|
|
171 |
|
172 |
# Main function to process image and analyze emotional expression
|
173 |
def analyze_emotion_from_image(image):
|
|
|
174 |
try:
|
|
|
175 |
print(f"Initial input type: {type(image)}")
|
176 |
+
|
177 |
+
# Check if the input is a URL string
|
178 |
+
if isinstance(image, str) and image.startswith('http'):
|
179 |
print(f"Loading image from URL: {image}")
|
180 |
response = requests.get(image, stream=True)
|
181 |
response.raise_for_status() # Raise an error for bad responses
|
182 |
+
image = Image.open(BytesIO(response.content)).convert("RGB") # Load from URL response content
|
183 |
print("Loaded image from URL.")
|
184 |
|
185 |
+
# Check if the input is a local file path string
|
186 |
+
elif isinstance(image, str):
|
187 |
+
print(f"Loading image from file path: {image}")
|
188 |
+
image = Image.open(image).convert("RGB") # Load from file path
|
189 |
+
print("Loaded image from local file.")
|
190 |
+
|
191 |
+
# Check if the input is Blob data (file-like object)
|
192 |
elif isinstance(image, dict) and "blob" in image:
|
193 |
blob_data = image["blob"]
|
194 |
+
image = Image.open(blob_data).convert("RGB") # Load from Blob data
|
195 |
print("Loaded image from Blob data.")
|
196 |
|
197 |
# Check if the input is a NumPy array
|
198 |
elif isinstance(image, np.ndarray):
|
199 |
+
image = Image.fromarray(image).convert("RGB") # Convert NumPy array to image
|
200 |
print("Converted image from NumPy array.")
|
201 |
|
202 |
print(f"Image size: {image.size}, mode: {image.mode}")
|
203 |
|
204 |
+
# Analyze colors (stubbed function)
|
205 |
dominant_colors = analyze_colors(image)
|
206 |
if dominant_colors is None:
|
207 |
return "Error analyzing colors"
|
|
|
209 |
color_emotions, stress_levels = color_emotion_analysis(dominant_colors)
|
210 |
print(f"Color emotions: {color_emotions}, Stress levels: {stress_levels}")
|
211 |
|
212 |
+
# Analyze patterns (stubbed function)
|
213 |
pattern_analysis, pattern_stress = analyze_patterns(image)
|
214 |
print(f"Pattern analysis: {pattern_analysis}, Pattern stress: {pattern_stress}")
|
215 |
|
216 |
+
# Compute overall result (stubbed function)
|
217 |
overall_result = compute_overall_result(color_emotions, stress_levels, pattern_analysis, pattern_stress)
|
218 |
|
219 |
return overall_result
|