nehapasricha94 commited on
Commit
731983b
1 Parent(s): 1994e95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -13,21 +13,29 @@ emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-e
13
  # Function to analyze colors in an image
14
  def analyze_colors(image):
15
  try:
16
- # Convert image to RGB if not already
17
  if image.mode != "RGB":
18
  image = image.convert("RGB")
19
- # Resize the image for faster processing (small but still sufficient for color analysis)
 
20
  image = image.resize((150, 150))
 
21
  # Convert to numpy array
22
  img_array = np.array(image)
 
 
 
 
 
23
  # Reshape image to be a list of pixels
24
  pixels = img_array.reshape((-1, 3))
25
 
26
- # Check if there are enough pixels to perform KMeans
 
 
27
  if len(pixels) < 5:
28
  return "Image has too few pixels for analysis"
29
 
30
- # Use KMeans to find the dominant colors
31
  kmeans = KMeans(n_clusters=5, random_state=0)
32
  kmeans.fit(pixels)
33
  dominant_colors = kmeans.cluster_centers_
@@ -41,10 +49,11 @@ def analyze_colors(image):
41
  return dominant_colors
42
 
43
  except Exception as e:
44
- print(f"Error in analyze_colors: {e}")
45
  return None
46
 
47
 
 
48
  # Function to detect emotions from colors (simplified emotion-color mapping)
49
  def color_emotion_analysis(dominant_colors):
50
  try:
 
13
  # Function to analyze colors in an image
14
  def analyze_colors(image):
15
  try:
16
+ # Ensure the image is in RGB format
17
  if image.mode != "RGB":
18
  image = image.convert("RGB")
19
+
20
+ # Resize the image for faster processing
21
  image = image.resize((150, 150))
22
+
23
  # Convert to numpy array
24
  img_array = np.array(image)
25
+
26
+ # Check if the image has a valid shape
27
+ if img_array.ndim != 3 or img_array.shape[2] != 3:
28
+ raise ValueError("Invalid image array shape: Expected a 3D array with 3 channels.")
29
+
30
  # Reshape image to be a list of pixels
31
  pixels = img_array.reshape((-1, 3))
32
 
33
+ print(f"Image shape: {img_array.shape}") # Debugging line
34
+ print(f"Number of pixels: {len(pixels)}") # Debugging line
35
+
36
  if len(pixels) < 5:
37
  return "Image has too few pixels for analysis"
38
 
 
39
  kmeans = KMeans(n_clusters=5, random_state=0)
40
  kmeans.fit(pixels)
41
  dominant_colors = kmeans.cluster_centers_
 
49
  return dominant_colors
50
 
51
  except Exception as e:
52
+ print(f"Error in analyze_colors: {e}") # Capture the error
53
  return None
54
 
55
 
56
+
57
  # Function to detect emotions from colors (simplified emotion-color mapping)
58
  def color_emotion_analysis(dominant_colors):
59
  try: