-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
196 lines (157 loc) · 7.96 KB
/
gui.py
File metadata and controls
196 lines (157 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import tkinter as tk
from tkinter import filedialog
import subprocess
import pygame
import pydub
import numpy as np
import sleep_analysis_module
from sklearn.preprocessing import MinMaxScaler
from sleep_analysis_module import read_user_data, feature_importance_model, extract_features, score_model, \
predict_new_score_with_model
class SleepQualityApp:
def __init__(self, root):
self.root = root
self.root.title("VitalFriend _VF (건강을 돕는 친구)")
self.root.geometry("360x360")
# 첫 번째 화면에 나오는 변수 저장
self.sleep_latency = 0
self.sleep_duration = 0
self.condition = 0
self.stress_level = 0
self.sleep_efficiency = 0
self.music_file = None # 음악 파일의 경로 초기화
self.mixer_initialized = False # pygame.mixer 초기화 여부
self.playing = False # 음악이 재생 여부
self.create_first_screen()
def initialize_mixer(self):
if not self.mixer_initialized:
pygame.mixer.init()
self.mixer_initialized = True
def create_first_screen(self):
self.label1 = tk.Label(self.root, text="수면 잠복기", font=("Arial", 12))
self.label1.pack()
self.sleep_latency_slider = tk.Scale(self.root, from_=0, to=60, orient="horizontal", length=200)
self.sleep_latency_slider.set(30)
self.sleep_latency_slider.pack()
self.label2 = tk.Label(self.root, text="수면 시간 (시간)", font=("Arial", 12))
self.label2.pack()
self.sleep_duration_slider = tk.Scale(self.root, from_=0, to=24, orient="horizontal", resolution=0.5,
length=200)
self.sleep_duration_slider.pack()
self.label3 = tk.Label(self.root, text="컨디션 (0-5)", font=("Arial", 12))
self.label3.pack()
self.condition_slider = tk.Scale(self.root, from_=0, to=5, orient="horizontal", length=200)
self.condition_slider.pack()
self.label4 = tk.Label(self.root, text="스트레스 (0-5)", font=("Arial", 12))
self.label4.pack()
self.stress_level_slider = tk.Scale(self.root, from_=0, to=5, orient="horizontal", length=200)
self.stress_level_slider.pack()
self.label5 = tk.Label(self.root, text="수면의 효율 (0-100%)", font=("Arial", 12))
self.label5.pack()
self.sleep_efficiency_slider = tk.Scale(self.root, from_=0, to=100, orient="horizontal", resolution=1,
length=200)
self.sleep_efficiency_slider.set(90)
self.sleep_efficiency_slider.pack()
self.next_button = tk.Button(self.root, text="다음", command=self.calculate_sleep_quality)
self.next_button.pack(side="right", anchor="se")
def calculate_sleep_quality(self):
self.sleep_latency = self.sleep_latency_slider.get()
self.sleep_duration = self.sleep_duration_slider.get() * 60
self.condition = self.condition_slider.get()
self.stress_level = self.stress_level_slider.get()
self.sleep_efficiency = self.sleep_efficiency_slider.get()
sleep_quality_score = self.calculate_sleep_quality_score()
self.label1.pack_forget()
self.label2.pack_forget()
self.label3.pack_forget()
self.label4.pack_forget()
self.label5.pack_forget()
self.sleep_latency_slider.pack_forget()
self.sleep_duration_slider.pack_forget()
self.condition_slider.pack_forget()
self.stress_level_slider.pack_forget()
self.sleep_efficiency_slider.pack_forget()
self.next_button.pack_forget()
self.create_second_screen(sleep_quality_score)
def create_second_screen(self, score):
self.text1 = tk.Label(self.root, text="수면의 질 점수", font=("Arial", 20, "bold"))
self.text1.pack()
self.score_label = tk.Label(self.root, text=str(score), font=("Arial", 36, "bold"))
self.score_label.pack()
self.next_button = tk.Button(self.root, text="다음", command=self.create_third_screen)
self.next_button.pack(side="right", anchor="se")
def create_third_screen(self):
self.text1.pack_forget()
self.score_label.pack_forget()
self.next_button.pack_forget()
# 세 번째 화면에 입력된 내용 표시
self.text2 = tk.Label(self.root, text="GPT에 입력될 내용", font=("Arial", 20))
self.text2.pack()
sleep_latency_text = f"수면 잠복기: {self.sleep_latency}"
sleep_duration_text = f"수면 시간: {self.sleep_duration} 분"
condition_text = f"컨디션: {self.condition}"
stress_level_text = f"스트레스 정도: {self.stress_level}"
sleep_efficiency_text = f"수면의 효율: {self.sleep_efficiency}%"
self.info_label = tk.Label(self.root,
text=f"{sleep_latency_text}\n{sleep_duration_text}\n{condition_text}\n{stress_level_text}\n{sleep_efficiency_text}",
font=("Arial", 14))
self.info_label.pack()
self.next_button = tk.Button(self.root, text="다음", command=self.create_fourth_screen)
self.next_button.pack(side="right", anchor="se")
def create_fourth_screen(self):
self.text2.pack_forget()
self.info_label.pack_forget()
self.next_button.pack_forget()
self.generate_music_button = tk.Button(self.root, text="음악 생성", command=self.generate_music, font=("Arial", 20),
height=2, width=15)
self.generate_music_button.grid(row=0, column=0, padx=55, pady=120)
def generate_music(self):
self.generate_music_button.grid_forget()
self.text2.pack_forget()
self.info_label.pack_forget()
self.next_button.pack_forget()
# 여기서 임시.py를 호출하여 음악 파일 생성
subprocess.call(["python", "C:/sleep_commu_gpt/gpt_commu.py"])
self.create_fifth_screen()
# 이미 초기화되지 않았다면 초기화
self.initialize_mixer()
# 음악 파일을 미리 설정
self.music_file = "ComMU_code/result/midi_files/combined.mid" # 음악 파일의 경로
if self.music_file:
pygame.mixer.init()
pygame.mixer.music.load(self.music_file)
pygame.mixer.music.play()
def create_fifth_screen(self):
# 재생 및 정지 버튼
self.play_button = tk.Button(self.root, text="재생", command=self.toggle_play_stop, font=("Arial", 20), height=2,
width=15)
self.play_button.grid(row=0, column=0, padx=55, pady=120)
self.next_button = tk.Button(self.root, text="종료", command=self.root.quit, font=("Arial", 10))
self.next_button.grid(row=1, column=0, columnspan=2)
def toggle_play_stop(self):
if not self.playing:
self.initialize_mixer()
pygame.mixer.music.load(self.music_file)
pygame.mixer.music.play()
self.playing = True
self.play_button.config(text="정지")
else:
pygame.mixer.music.stop()
self.playing = False
self.play_button.config(text="재생")
def play_music(self):
if self.music_file:
if not pygame.mixer.get_init():
pygame.mixer.init()
pygame.mixer.music.load(self.music_file)
pygame.mixer.music.play()
def calculate_sleep_quality_score(self):
user1_data = sleep_analysis_module.user1_data_all
new_input1 = np.array([[self.sleep_latency, self.sleep_duration, self.condition, self.stress_level, self.sleep_efficiency]])
lstm_model, scaler = sleep_analysis_module.score_model(user1_data) # 모델 및 스케일러 생성
prediction = predict_new_score_with_model(new_input1, lstm_model, scaler)
return prediction
if __name__ == "__main__":
root = tk.Tk()
app = SleepQualityApp(root)
root.mainloop()