-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFast_api.py
More file actions
201 lines (164 loc) · 7.3 KB
/
Fast_api.py
File metadata and controls
201 lines (164 loc) · 7.3 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
import pickle
import cv2
import mediapipe as mp
import numpy as np
import time
from flask import Flask, jsonify
from flask_cors import CORS
import threading
# โหลดโมเดล
model_dict = pickle.load(open('./model.p', 'rb'))
model = model_dict['model']
# ตั้งค่ากล้อง - ลองกล้อง 1 ก่อน ถ้าไม่ได้ใช้กล้อง 0
def initialize_camera():
cap = cv2.VideoCapture(1)
if not cap.isOpened():
print("⚠️ Camera 1 not available, switching to Camera 0")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("❌ No camera available!")
return None
print("✅ Using Camera 0")
else:
print("✅ Using Camera 1")
return cap
cap = initialize_camera()
if cap is None:
raise Exception("No camera detected!")
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
hands = mp_hands.Hands(static_image_mode=False, min_detection_confidence=0.3, max_num_hands=2)
labels_dict = {
0: 'me', 1: 'sorry', 2: 'thank', 3: 'hello', 4: 'introduce', 5: 'fine',
6: 'meet', 7: 'signname', 8: 'noproblem', 9: 'unwell', 10: 'yes', 11: 'no'
}
# ตัวแปร Global
current_class = None
class_start_time = None
last_detected_time = time.time()
detected_class = None
last_sent_time = None
last_sent_class = None
last_detected_class = None
# สร้าง Flask app
app = Flask(__name__)
CORS(app) # เปิด CORS
def process_frame(frame, detected_class, current_class, class_start_time):
global last_detected_time, last_detected_class, last_sent_time, last_sent_class
H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
all_x = []
all_y = []
data_aux = []
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style()
)
for landmark in hand_landmarks.landmark:
all_x.append(landmark.x)
all_y.append(landmark.y)
for hand_landmarks in results.multi_hand_landmarks:
for landmark in hand_landmarks.landmark:
data_aux.append(landmark.x - min(all_x))
data_aux.append(landmark.y - min(all_y))
num_hands = len(results.multi_hand_landmarks)
if num_hands == 1:
data_aux.extend([0] * 42)
if len(data_aux) == 84:
try:
prediction = model.predict([np.asarray(data_aux)])
predicted_class_id = int(prediction[0])
if predicted_class_id in labels_dict:
predicted_character = labels_dict[predicted_class_id]
if current_class == predicted_character:
if class_start_time is None:
class_start_time = time.time()
elif time.time() - class_start_time >= 2:
current_time = time.time()
if (last_sent_time is None or
current_time - last_sent_time >= 3 or
last_sent_class != predicted_character):
detected_class = predicted_character
last_detected_class = predicted_character
last_sent_time = current_time
last_sent_class = predicted_character
class_start_time = current_time
else:
current_class = predicted_character
class_start_time = time.time()
last_detected_time = time.time()
x1, y1 = int(min(all_x) * W) - 10, int(min(all_y) * H) - 10
x2, y2 = int(max(all_x) * W) + 10, int(max(all_y) * H) + 10
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, predicted_character, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
if current_class == predicted_character and class_start_time:
time_remaining = 2 - (time.time() - class_start_time)
if time_remaining > 0:
cv2.putText(frame, f"Hold: {time_remaining:.1f}s", (x1, y2 + 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2, cv2.LINE_AA)
except Exception as e:
print(f"Prediction error: {e}")
else:
last_detected_class = None
current_class = None
class_start_time = None
return frame, detected_class, current_class, class_start_time
def draw_results(frame, detected_class):
H, W, _ = frame.shape
detected_text = f"Last Sent: {detected_class if detected_class else 'None'}"
cv2.rectangle(frame, (10, H - 80), (W - 10, H - 10), (255, 255, 255), -1)
cv2.putText(frame, detected_text, (20, H - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2, cv2.LINE_AA)
return frame
def video_processing_loop():
global current_class, class_start_time, last_detected_time, detected_class
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
frame, detected_class, current_class, class_start_time = process_frame(
frame, detected_class, current_class, class_start_time)
frame = draw_results(frame, detected_class)
cv2.imshow('Hand Gesture Recognition', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# API Endpoints
@app.route('/')
def root():
return jsonify({
"message": "Hand Gesture Recognition API",
"status": "running"
})
@app.route('/gesture')
def get_gesture():
"""ดึงค่าท่าทางมือที่ตรวจจับได้ล่าสุด"""
return jsonify({
"gesture": detected_class if detected_class else None,
"timestamp": time.time(),
"last_sent_class": last_sent_class,
"last_sent_time": last_sent_time
})
@app.route('/status')
def get_status():
"""ตรวจสอบสถานะของระบบ"""
return jsonify({
"status": "active",
"current_detection": current_class,
"last_detected": last_detected_class,
"camera_active": cap.isOpened()
})
def main():
# เริ่ม thread สำหรับประมวลผลวิดีโอ
video_thread = threading.Thread(target=video_processing_loop, daemon=True)
video_thread.start()
# รัน Flask server
print("🚀 Starting Hand Gesture Recognition API on http://localhost:5050")
app.run(host='127.0.0.1', port=5050, debug=False, threaded=True)
if __name__ == "__main__":
main()