-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws_test_client.py
More file actions
131 lines (104 loc) · 3.7 KB
/
Copy pathws_test_client.py
File metadata and controls
131 lines (104 loc) · 3.7 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
import cv2
import asyncio
import websockets
import httpx
import json
BASE_URL = "http://127.0.0.1:8000"
WS_BASE = "ws://127.0.0.1:8000"
# بيانات الطالب للتجربة
STUDENT_ID = "STU_001"
EXAM_ID = "EXAM_001"
COURSE_ID = "COURSE_001"
def start_session() -> str:
"""
بيعمل POST /session/start ويرجع session_id
"""
url = f"{BASE_URL}/session/start"
body = {
"student_id": STUDENT_ID,
"exam_id": EXAM_ID,
"course_id": COURSE_ID,
}
response = httpx.post(url, json=body, timeout=5.0)
data = response.json()
print(f"✅ Session started: {data['session_id']}")
return data["session_id"]
def end_session(session_id: str):
"""
بيعمل POST /session/end ويطبع التقرير النهائي
"""
url = f"{BASE_URL}/session/end"
body = {"session_id": session_id}
response = httpx.post(url, json=body, timeout=5.0)
data = response.json()
print("\n📊 Final Report:")
print(f" Total Warnings : {data['total_warnings']}")
print(f" Terminated : {data['terminated']}")
print(f" Started At : {data['started_at']}")
print(f" Ended At : {data['ended_at']}")
print(f" Violations :")
for v in data["violations_history"]:
print(f" - {v['violation_type']} | {v['timestamp']} | Warning #{v['warning_number']}")
async def stream_camera(session_id: str):
"""
بيفتح الكاميرا ويبعت frames للسيرفر عبر WebSocket
"""
ws_url = f"{WS_BASE}/ws/exam/{session_id}"
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("❌ Cannot open camera")
return
print(f"📡 Connecting to WebSocket: {ws_url}")
async with websockets.connect(ws_url, max_size=10_000_000) as ws:
print("✅ WebSocket Connected - Press Q to stop\n")
while True:
ret, frame = cap.read()
if not ret:
print("❌ Failed to read frame")
break
# ضغط الصورة
ok, buffer = cv2.imencode(
".jpg", frame,
[cv2.IMWRITE_JPEG_QUALITY, 80]
)
if not ok:
continue
# بعت الـ frame
await ws.send(buffer.tobytes())
try:
# استقبال الرد
response = await asyncio.wait_for(ws.recv(), timeout=2.0)
data = json.loads(response)
# طباعة الحالة
status = f"W:{data['warnings']} | Faces:{data['face_count']} | {data['message']}"
print(f"📩 {status}")
# لو الامتحان اتوقف
if data["terminated"]:
print("🛑 Exam Terminated!")
break
# عرض على الشاشة
cv2.putText(
frame, status,
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 0, 255), 2
)
except asyncio.TimeoutError:
print("⚠️ No response from server")
cv2.imshow("NCE Test Client", frame)
if cv2.waitKey(1) & 0xFF in (ord("q"), ord("Q")):
break
cap.release()
cv2.destroyAllWindows()
print("🛑 Camera stopped")
async def main():
# 1. ابدأ الجلسة من الباك اند
session_id = start_session()
try:
# 2. ابعت frames عبر WebSocket
await stream_camera(session_id)
finally:
# 3. انهي الجلسة واطبع التقرير
end_session(session_id)
if __name__ == "__main__":
asyncio.run(main())