-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_yolo_api.py
More file actions
169 lines (131 loc) · 5.31 KB
/
Copy pathtest_yolo_api.py
File metadata and controls
169 lines (131 loc) · 5.31 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
"""
YOLOv11 API Test Script - İşaret Kelime Tanıma
Webcam'den görüntü alıp API'ye gönderir, sonucu ekrana yazdırır
"""
import cv2
import requests
import base64
import time
API_URL = "http://127.0.0.1:8000"
def test_predict_sign():
"""Webcam'den frame alıp /predict_sign endpoint'ini test et"""
print("=" * 70)
print("YOLOv11 API TEST - İşaret Kelime Tanıma")
print("=" * 70)
print("20 kelime: MERHABA, EVET, HAYIR, TEŞEKKÜRLER, LÜTFEN, vb.")
print("Kamera açılıyor... Q tuşu ile çıkış yapın")
print("=" * 70)
# Kamera aç
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("❌ Kamera açılamadı!")
return
print("✅ Kamera açıldı")
print("\n🎯 İşaret yapın ve SPACE tuşuna basarak test edin")
print(" Q: Çıkış\n")
frame_count = 0
last_prediction = None
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# Ekranda göster
display_frame = frame.copy()
# Bilgi yaz
cv2.putText(display_frame, "SPACE: Test | Q: Cikis", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
if last_prediction:
# Son tahmini göster
text = f"{last_prediction['text']} ({last_prediction['conf']:.1%})"
cv2.putText(display_frame, text, (10, 70),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
cv2.imshow('YOLOv11 API Test', display_frame)
key = cv2.waitKey(1) & 0xFF
# Q ile çıkış
if key == ord('q'):
break
# SPACE ile test
if key == ord(' '):
print(f"\n🔄 Frame {frame_count} test ediliyor...")
# Frame'i encode et
_, buffer = cv2.imencode('.jpg', frame)
base64_image = base64.b64encode(buffer).decode('utf-8')
try:
# API'ye gönder
start_time = time.time()
response = requests.post(
f"{API_URL}/predict_sign",
json={"image_base64": base64_image},
timeout=5
)
elapsed = (time.time() - start_time) * 1000 # ms
if response.status_code == 200:
data = response.json()
if data['success']:
last_prediction = {
'text': data['prediction_text'],
'conf': data['confidence']
}
print(f"✅ Tespit: {data['prediction_text']}")
print(f" Güven: {data['confidence']:.1%}")
print(f" Süre: {elapsed:.0f}ms")
else:
print(f"⚠️ {data['message']}")
last_prediction = None
else:
print(f"❌ API hatası: {response.status_code}")
except Exception as e:
print(f"❌ Hata: {e}")
cap.release()
cv2.destroyAllWindows()
print("\n✅ Test tamamlandı!")
def test_health():
"""API sağlık kontrolü"""
try:
response = requests.get(f"{API_URL}/health", timeout=2)
data = response.json()
print("\n📊 API Sağlık Durumu:")
print(f" Status: {data['status']}")
print(f" Model yüklü: {data['model_loaded']}")
print(f" Device: {data['device']}")
return data['status'] == 'healthy'
except Exception as e:
print(f"❌ API'ye erişilemiyor: {e}")
return False
def test_model_info():
"""Model bilgilerini göster"""
try:
response = requests.get(f"{API_URL}/model/info", timeout=2)
data = response.json()
print("\n📦 Model Bilgileri:")
# Digits model
digits = data.get('digits_model', {})
print(f"\n 🔢 Digits Model:")
print(f" Sınıf sayısı: {digits.get('num_classes', 0)}")
print(f" Device: {digits.get('device', 'N/A')}")
# YOLO model
yolo = data.get('yolo_model', {})
print(f"\n ✋ YOLO Model:")
print(f" Yüklü: {'✅' if yolo.get('loaded') else '❌'}")
print(f" Mimari: {yolo.get('architecture', 'N/A')}")
print(f" mAP50: {yolo.get('mAP50', 0):.1%}")
print(f" Sınıf sayısı: {yolo.get('num_classes', 0)}")
if yolo.get('classes'):
print(f" Kelimeler: {', '.join(yolo['classes'][:5])}...")
return True
except Exception as e:
print(f"❌ Model bilgisi alınamadı: {e}")
return False
if __name__ == "__main__":
# 1. API sağlık kontrolü
if not test_health():
print("\n⚠️ API çalışmıyor! Önce API'yi başlatın:")
print(" python -m uvicorn signbridge.api.service:app --reload")
exit(1)
# 2. Model bilgilerini göster
test_model_info()
# 3. Webcam testi
print("\n" + "=" * 70)
input("Webcam testine başlamak için ENTER'a basın...")
test_predict_sign()