Krish30 commited on
Commit
2056aa0
·
verified ·
1 Parent(s): 0dd045f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -117
app.py CHANGED
@@ -1,117 +1,117 @@
1
- import streamlit as st
2
- import google.generativeai as genai
3
- import os
4
- from PIL import Image
5
- import cv2
6
- from io import BytesIO
7
- import base64
8
- from dotenv import load_dotenv
9
- import numpy as np
10
- from fer import FER
11
-
12
- load_dotenv()
13
-
14
- genai.configure(api_key=("AIzaSyDziGvuT1woHnH4_S3L_zQZV55Yj-123A8"))
15
-
16
- # gemini function for general content generation
17
- def get_gemini_response(input):
18
- try:
19
- model = genai.GenerativeModel('gemini-pro')
20
- response = model.generate_content(input)
21
- return response
22
- except Exception as e:
23
- st.error(f"Error: {e}")
24
- return None
25
-
26
- # Function to analyze image for depression and emotion detection using FER
27
- def detect_emotions(image):
28
- detector = FER(mtcnn=True)
29
- # Convert PIL Image to NumPy array
30
- image_np = np.array(image)
31
- emotions = detector.detect_emotions(image_np)
32
- if emotions:
33
- return emotions[0]['emotions']
34
- return None
35
-
36
- # Function to analyze detected emotions with LLM
37
- def analyze_emotions_with_llm(emotions):
38
- emotion_analysis = ", ".join([f"{emotion}: {score:.2f}" for emotion, score in emotions.items()])
39
-
40
- analysis_prompt = f"""
41
- ### As a mental health and emotional well-being expert, analyze the following detected emotions.
42
- ### Detected Emotions:
43
- {emotion_analysis}
44
- ### Analysis Output:
45
- 1. Identify any potential signs of depression based on the detected emotions.
46
- 2. Explain the reasoning behind your identification.
47
- 3. Provide recommendations for addressing any identified issues.
48
- """
49
- response = get_gemini_response(analysis_prompt)
50
- return response
51
-
52
- # Function to capture live video frame for analysis
53
- def capture_video_frame():
54
- video_capture = cv2.VideoCapture(0)
55
- ret, frame = video_capture.read()
56
- if ret:
57
- frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
58
- return Image.fromarray(frame_rgb)
59
- return None
60
-
61
- # Function to parse and display response content
62
- def display_response_content(response):
63
- st.subheader("Response Output")
64
- if response and response.candidates:
65
- response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
66
- sections = response_content.split('###')
67
- for section in sections:
68
- if section.strip():
69
- section_lines = section.split('\n')
70
- section_title = section_lines[0].strip()
71
- section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
72
- if section_title:
73
- st.markdown(f"**{section_title}**")
74
- if section_body:
75
- st.write(section_body)
76
- else:
77
- st.write("No response received from the model or quota exceeded.")
78
-
79
- ## Streamlit App
80
- st.title("AI-Powered Depression and Emotion Detection System")
81
- st.text("Use the AI system for detecting depression and emotions from images and live video.")
82
-
83
- # Tabs for different functionalities
84
- tab1, tab2 = st.tabs(["Image Analysis", "Live Video Analysis"])
85
-
86
- with tab1:
87
- st.header("Image Analysis")
88
- uploaded_file = st.file_uploader("Upload an image for analysis", type=["jpg", "jpeg", "png"], help="Please upload an image file.")
89
- submit_image = st.button('Analyze Image')
90
-
91
- if submit_image:
92
- if uploaded_file is not None:
93
- image = Image.open(uploaded_file)
94
- emotions = detect_emotions(image)
95
- if emotions:
96
- response = analyze_emotions_with_llm(emotions)
97
- # Parse and display response in a structured way
98
- display_response_content(response)
99
- else:
100
- st.write("No emotions detected in the image.")
101
-
102
- with tab2:
103
- st.header("Live Video Analysis")
104
- capture_frame = st.button('Capture and Analyze Frame')
105
-
106
- if capture_frame:
107
- image = capture_video_frame()
108
- if image is not None:
109
- emotions = detect_emotions(image)
110
- if emotions:
111
- response = analyze_emotions_with_llm(emotions)
112
- # Parse and display response in a structured way
113
- display_response_content(response)
114
- else:
115
- st.write("No emotions detected in the video frame.")
116
- else:
117
- st.write("Failed to capture video frame.")
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ import os
4
+ from PIL import Image
5
+ import cv2
6
+ from io import BytesIO
7
+ import base64
8
+ from dotenv import load_dotenv
9
+ import numpy as np
10
+ from fer import FER
11
+
12
+ load_dotenv()
13
+
14
+ genai.configure(api_key=("AIzaSyARLRWGr4vloCAhea-rt8dMV7gWeC1pgBE"))
15
+
16
+ # gemini function for general content generation
17
+ def get_gemini_response(input):
18
+ try:
19
+ model = genai.GenerativeModel('gemini-pro')
20
+ response = model.generate_content(input)
21
+ return response
22
+ except Exception as e:
23
+ st.error(f"Error: {e}")
24
+ return None
25
+
26
+ # Function to analyze image for depression and emotion detection using FER
27
+ def detect_emotions(image):
28
+ detector = FER(mtcnn=True)
29
+ # Convert PIL Image to NumPy array
30
+ image_np = np.array(image)
31
+ emotions = detector.detect_emotions(image_np)
32
+ if emotions:
33
+ return emotions[0]['emotions']
34
+ return None
35
+
36
+ # Function to analyze detected emotions with LLM
37
+ def analyze_emotions_with_llm(emotions):
38
+ emotion_analysis = ", ".join([f"{emotion}: {score:.2f}" for emotion, score in emotions.items()])
39
+
40
+ analysis_prompt = f"""
41
+ ### As a mental health and emotional well-being expert, analyze the following detected emotions.
42
+ ### Detected Emotions:
43
+ {emotion_analysis}
44
+ ### Analysis Output:
45
+ 1. Identify any potential signs of depression based on the detected emotions.
46
+ 2. Explain the reasoning behind your identification.
47
+ 3. Provide recommendations for addressing any identified issues.
48
+ """
49
+ response = get_gemini_response(analysis_prompt)
50
+ return response
51
+
52
+ # Function to capture live video frame for analysis
53
+ def capture_video_frame():
54
+ video_capture = cv2.VideoCapture(0)
55
+ ret, frame = video_capture.read()
56
+ if ret:
57
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
58
+ return Image.fromarray(frame_rgb)
59
+ return None
60
+
61
+ # Function to parse and display response content
62
+ def display_response_content(response):
63
+ st.subheader("Response Output")
64
+ if response and response.candidates:
65
+ response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
66
+ sections = response_content.split('###')
67
+ for section in sections:
68
+ if section.strip():
69
+ section_lines = section.split('\n')
70
+ section_title = section_lines[0].strip()
71
+ section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
72
+ if section_title:
73
+ st.markdown(f"**{section_title}**")
74
+ if section_body:
75
+ st.write(section_body)
76
+ else:
77
+ st.write("No response received from the model or quota exceeded.")
78
+
79
+ ## Streamlit App
80
+ st.title("AI-Powered Depression and Emotion Detection System")
81
+ st.text("Use the AI system for detecting depression and emotions from images and live video.")
82
+
83
+ # Tabs for different functionalities
84
+ tab1, tab2 = st.tabs(["Image Analysis", "Live Video Analysis"])
85
+
86
+ with tab1:
87
+ st.header("Image Analysis")
88
+ uploaded_file = st.file_uploader("Upload an image for analysis", type=["jpg", "jpeg", "png"], help="Please upload an image file.")
89
+ submit_image = st.button('Analyze Image')
90
+
91
+ if submit_image:
92
+ if uploaded_file is not None:
93
+ image = Image.open(uploaded_file)
94
+ emotions = detect_emotions(image)
95
+ if emotions:
96
+ response = analyze_emotions_with_llm(emotions)
97
+ # Parse and display response in a structured way
98
+ display_response_content(response)
99
+ else:
100
+ st.write("No emotions detected in the image.")
101
+
102
+ with tab2:
103
+ st.header("Live Video Analysis")
104
+ capture_frame = st.button('Capture and Analyze Frame')
105
+
106
+ if capture_frame:
107
+ image = capture_video_frame()
108
+ if image is not None:
109
+ emotions = detect_emotions(image)
110
+ if emotions:
111
+ response = analyze_emotions_with_llm(emotions)
112
+ # Parse and display response in a structured way
113
+ display_response_content(response)
114
+ else:
115
+ st.write("No emotions detected in the video frame.")
116
+ else:
117
+ st.write("Failed to capture video frame.")