-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp32_main.py
More file actions
296 lines (241 loc) · 10.1 KB
/
esp32_main.py
File metadata and controls
296 lines (241 loc) · 10.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from flask import Flask, jsonify, render_template, request
import paho.mqtt.client as mqtt
import ssl
import time
import pyttsx3
import speech_recognition as sr
import threading
import re
from transformers import PreTrainedTokenizerFast, GPT2LMHeadModel
import torch
import numpy as np
import requests
app = Flask(__name__)
# ===== MQTT 설정 =====
MQTT_BROKER = "voicecardwallet.r-e.kr"
MQTT_PORT = 8883
MQTT_TOPIC = "esp32/commands"
HTTP_COMMAND_URL = "https://voicecardwallet.r-e.kr/set_command"
slot_num_to_cmd = {
1: "r",
2: "s",
3: "l"
}
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("MQTT 브로커 연결 성공")
else:
print(f"MQTT 연결 실패, 코드: {rc}")
# ===== TTS 엔진 =====
tts_engine = pyttsx3.init()
tts_lock = threading.Lock()
# ===== 슬롯 저장 =====
slots = {}
# ===== KoGPT2 모델 로딩 =====
tokenizer = PreTrainedTokenizerFast.from_pretrained("skt/kogpt2-base-v2")
model = GPT2LMHeadModel.from_pretrained("skt/kogpt2-base-v2")
model.eval()
# ===== 동의어 그룹 처리 =====
synonym_groups = [
["주민등록증", "민증", "등록증"],
["롯데카드", "롯데"],
["삼성카드", "삼성"],
]
def find_canonical_name(name):
name = name.strip()
for group in synonym_groups:
if name in group:
return group[0]
return name
def speak(text):
with tts_lock:
tts_engine.say(text)
tts_engine.runAndWait()
def listen_command():
r = sr.Recognizer()
try:
with sr.Microphone() as source:
print("🎤 음성 입력 대기 중...")
audio = r.listen(source, timeout=5, phrase_time_limit=7)
except Exception as e:
print(f"마이크 에러: {e}")
return "인식 실패"
try:
text = r.recognize_google(audio, language='ko-KR')
print(f"🎿 인식된 텍스트: {text}")
return text
except sr.UnknownValueError:
return "인식 실패"
except sr.RequestError as e:
return f"API 오류: {e}"
def get_embedding(text):
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs, output_hidden_states=True)
last_hidden = outputs.hidden_states[-1]
sentence_embedding = torch.mean(last_hidden, dim=1).squeeze().cpu().numpy()
return sentence_embedding
command_templates = {
"save": "저장 롯데카드 1 저장 삼성카드 2번 저장 민증 3",
"delete": "삭제 롯데카드 삭제 민증 삭제 삼성카드",
"move": "롯데카드 민증 삼성카드 이동"
}
command_embeddings = {k: get_embedding(v) for k, v in command_templates.items()}
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
def classify_command(text):
keywords = {"save": "저장", "delete": "삭제", "move": "이동"}
for key, kw in keywords.items():
if kw in text:
return key
emb_text = get_embedding(text)
sims = {k: cosine_similarity(emb_text, emb) for k, emb in command_embeddings.items()}
return max(sims, key=sims.get)
def parse_slots(text):
pattern_save = r"(주민등록증|민증|등록증|롯데카드|롯데|삼성카드|삼성)\s*(\d+)\s*저장"
pattern_delete = r"(주민등록증|민증|등록증|롯데카드|롯데|삼성카드|삼성)\s*삭제"
pattern_move = r"((?:주민등록증|민증|등록증|롯데카드|롯데|삼성카드|삼성)\s*\d+\s*)+이동"
slots_local = {}
if "저장" in text:
for match in re.finditer(pattern_save, text):
card, num = match.groups()
slots_local[find_canonical_name(card)] = int(num)
elif "삭제" in text:
for match in re.finditer(pattern_delete, text):
card = find_canonical_name(match.group(1))
slots_local[card] = slots.get(card, 0)
elif "이동" in text:
move_part = re.search(pattern_move, text)
if move_part:
pairs = re.findall(r"(주민등록증|민증|등록증|롯데카드|롯데|삼성카드|삼성)\s*(\d+)", move_part.group(0))
for card, num in pairs:
slots_local[find_canonical_name(card)] = int(num)
return slots_local
def process_text_command(text):
cmd_type = classify_command(text)
if cmd_type == "save":
content = text.replace("저장", "").strip()
slot_num_match = re.search(r'(\d+)', content)
if not slot_num_match:
return {"result": "fail", "message": "번호 인식 실패"}
slot_num = int(slot_num_match.group(1))
slot_name = content[:slot_num_match.start()].strip()
if not slot_name:
return {"result": "fail", "message": "이름 인식 실패"}
slot_name = find_canonical_name(slot_name)
slots[slot_name] = slot_num
command_char = slot_num_to_cmd[slot_num]
response = requests.post(HTTP_COMMAND_URL, json={"command": command_char})
print(f"🔄 요청 보냄 → {command_char}")
print(f"📬 응답 코드: {response.status_code}")
print(f"📨 응답 본문: {response.text}")
return {"result": "success", "message": f"{slot_name} → {slot_num} 저장 완료"}
elif cmd_type == "delete":
slot_name = find_canonical_name(text.replace("삭제", "").strip())
if slot_name in slots:
slot_num = slots[slot_name]
command_char = slot_num_to_cmd[slot_num]
del slots[slot_name]
response = requests.post(HTTP_COMMAND_URL, json={"command": command_char})
print(f"🔄 요청 보냄 → {command_char}")
print(f"📬 응답 코드: {response.status_code}")
print(f"📨 응답 본문: {response.text}")
return {"result": "success", "message": f"{slot_name} 삭제 완료"}
return {"result": "fail", "message": "해당 슬롯 없음"}
elif cmd_type == "move":
slot_name = find_canonical_name(text.strip())
if slot_name in slots:
slot_num = slots[slot_name]
command_char = slot_num_to_cmd[slot_num]
response = requests.post(HTTP_COMMAND_URL, json={"command": command_char})
print(f"🔄 요청 보냄 → {command_char}")
print(f"📬 응답 코드: {response.status_code}")
print(f"📨 응답 본문: {response.text}")
return {"result": "success", "message": f"{slot_name} 이동 완료"}
return {"result": "fail", "message": "해당 슬롯 없음"}
return {"result": "fail", "message": "명령 분류 실패"}
def process_voice_command():
text = listen_command()
if text == "인식 실패":
return {"result": "fail", "message": "음성 인식 실패"}
cmd_type = classify_command(text)
new_slots = parse_slots(text)
if cmd_type == "save":
for card, num in new_slots.items():
slots[card] = num
command_char = slot_num_to_cmd[num]
response = requests.post(HTTP_COMMAND_URL, json={"command": command_char})
print(f"🔄 요청 보냄 → {command_char}")
print(f"📬 응답 코드: {response.status_code}")
print(f"📨 응답 본문: {response.text}")
elif cmd_type == "delete":
for card in new_slots.keys():
if card in slots:
num = slots[card]
command_char = slot_num_to_cmd[num]
response = requests.post(HTTP_COMMAND_URL, json={"command": command_char})
print(f"🔄 요청 보냄 → {command_char}")
print(f"📬 응답 코드: {response.status_code}")
print(f"📨 응답 본문: {response.text}")
del slots[card]
elif cmd_type == "move":
for card in new_slots.keys():
if card in slots:
num = slots[card]
command_char = slot_num_to_cmd[num]
response = requests.post(HTTP_COMMAND_URL, json={"command": command_char})
print(f"🔄 요청 보냄 → {command_char}")
print(f"📬 응답 코드: {response.status_code}")
print(f"📨 응답 본문: {response.text}")
else:
return {"result": "fail", "message": "알 수 없는 명령"}
print(f"📦 현재 슬롯 상태: {slots}")
return {"result": "success", "message": f"{cmd_type} 명령 처리 완료"}
# ===== 웹 라우트 =====
@app.route("/")
def index():
return render_template("index.html")
current_command = {"command": "none"}
command_acknowledged = False
@app.route("/command", methods=["GET"])
def command():
global current_command, command_acknowledged
if not command_acknowledged and current_command["command"] != "none":
return jsonify(current_command)
else:
return jsonify({"command": "none"})
@app.route("/set_command", methods=["POST"])
def set_command():
global current_command, command_acknowledged
data = request.get_json()
if not data or "command" not in data:
return jsonify({"error": "No command provided"}), 400
current_command = {"command": data["command"]}
command_acknowledged = False
return jsonify({"status": "ok", "command": current_command})
@app.route("/ack", methods=["POST"])
def ack_command():
global current_command, command_acknowledged
data = request.get_json()
if not data or "ack" not in data or not data["ack"]:
return jsonify({"error": "No acknowledgment provided"}), 400
# ESP32가 ack 보냈으니 명령 초기화
command_acknowledged = True
current_command = {"command": "none"}
return jsonify({"status": "acknowledged"})
@app.route("/slots")
def get_slots():
# 예: {"주민등록증": 3, "롯데카드": 1, "삼성카드": 2}
return jsonify(slots)
@app.route("/listen", methods=["POST"])
def listen():
data = request.get_json()
if not data or "command" not in data:
return jsonify({"result": "fail", "message": "명령어가 없습니다."}), 400
text_command = data["command"]
print(f"클라이언트에서 받은 명령어: {text_command}")
result = process_text_command(text_command)
print(result)
return jsonify(result)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=2506, debug=True)