nehapasricha94 commited on
Commit
1994e95
1 Parent(s): 785de28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -13,18 +13,22 @@ 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 in that mode
17
  if image.mode != "RGB":
18
  image = image.convert("RGB")
19
- # Resize image for faster processing
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
  # Use KMeans to find the dominant colors
27
- kmeans = KMeans(n_clusters=5)
28
  kmeans.fit(pixels)
29
  dominant_colors = kmeans.cluster_centers_
30
 
@@ -35,10 +39,12 @@ def analyze_colors(image):
35
  plt.show()
36
 
37
  return dominant_colors
 
38
  except Exception as e:
39
  print(f"Error in analyze_colors: {e}")
40
  return None
41
 
 
42
  # Function to detect emotions from colors (simplified emotion-color mapping)
43
  def color_emotion_analysis(dominant_colors):
44
  try:
 
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_
34
 
 
39
  plt.show()
40
 
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: