nehapasricha94 commited on
Commit
74fb5a4
1 Parent(s): b43a517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -19
app.py CHANGED
@@ -1,25 +1,82 @@
1
- import gradio as gr
2
- from transformers import VisionEncoderDecoderModel, AutoTokenizer
3
- from PIL import Image
4
- import io
5
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # Load model and tokenizer
8
- model = VisionEncoderDecoderModel.from_pretrained("liuhaotian/LLaVA-1.5-7b")
9
- tokenizer = AutoTokenizer.from_pretrained("liuhaotian/LLaVA-1.5-7b")
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Function to analyze the image
12
- def analyze_image(image_blob):
13
- image = Image.open(io.BytesIO(image_blob))
14
- pixel_values = torch.tensor(image).unsqueeze(0) # Add batch dimension
15
- inputs = tokenizer("Analyze the emotions in this image", return_tensors="pt")
16
 
17
- # Run the model
18
- outputs = model.generate(**inputs, pixel_values=pixel_values)
19
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
20
 
21
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Set up the Gradio interface
24
- iface = gr.Interface(fn=analyze_image, inputs="file", outputs="text")
25
- iface.launch()
 
 
 
 
 
1
  import torch
2
+ import cv2
3
+ from PIL import Image
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ from transformers import pipeline
7
+ import gradio as gr
8
+ from sklearn.cluster import KMeans
9
+
10
+ # Emotion detection pipeline for text (if any text is included in assets)
11
+ emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
12
+
13
+ # Function to analyze colors in an image
14
+ def analyze_colors(image):
15
+ # Convert image to RGB
16
+ image = image.convert("RGB")
17
+ # Resize image for faster processing
18
+ image = image.resize((150, 150))
19
+ # Convert to numpy array
20
+ img_array = np.array(image)
21
+ # Reshape image to be a list of pixels
22
+ pixels = img_array.reshape((-1, 3))
23
+
24
+ # Use KMeans to find the dominant colors
25
+ kmeans = KMeans(n_clusters=5)
26
+ kmeans.fit(pixels)
27
+ dominant_colors = kmeans.cluster_centers_
28
+
29
+ # Plot the colors for visualization
30
+ plt.figure(figsize=(8, 6))
31
+ plt.imshow([dominant_colors.astype(int)])
32
+ plt.axis('off')
33
+ plt.show()
34
+
35
+ return dominant_colors
36
 
37
+ # Function to detect emotions from colors (simplified emotion-color mapping)
38
+ def color_emotion_analysis(dominant_colors):
39
+ # Map certain color ranges to emotions
40
+ emotions = []
41
+ for color in dominant_colors:
42
+ # Simple logic: darker tones could indicate sadness
43
+ if np.mean(color) < 85:
44
+ emotions.append("Sadness")
45
+ elif np.mean(color) > 170:
46
+ emotions.append("Happiness")
47
+ else:
48
+ emotions.append("Neutral")
49
+
50
+ return emotions
51
 
52
+ # Function to analyze patterns and shapes using OpenCV
53
+ def analyze_patterns(image):
54
+ # Convert to grayscale for edge detection
55
+ gray_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
56
+ edges = cv2.Canny(gray_image, 100, 200)
57
 
58
+ # Calculate the number of edges (chaos metric)
59
+ num_edges = np.sum(edges > 0)
 
60
 
61
+ if num_edges > 10000: # Arbitrary threshold for "chaos"
62
+ return "Chaotic patterns - possibly distress"
63
+ else:
64
+ return "Orderly patterns - possibly calm"
65
+
66
+ # Main function to process image and analyze emotional expression
67
+ def analyze_emotion_from_image(image):
68
+ # Analyze colors
69
+ dominant_colors = analyze_colors(image)
70
+ color_emotions = color_emotion_analysis(dominant_colors)
71
+
72
+ # Analyze patterns
73
+ pattern_analysis = analyze_patterns(image)
74
+
75
+ return f"Color-based emotions: {color_emotions}\nPattern analysis: {pattern_analysis}"
76
+
77
+ # Gradio interface to upload image files and perform analysis
78
+ iface = gr.Interface(fn=analyze_emotion_from_image, inputs="image", outputs="text")
79
 
80
+ # Launch the interface
81
+ if __name__ == "__main__":
82
+ iface.launch()