-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
93 lines (73 loc) · 2.59 KB
/
Copy pathtest.py
File metadata and controls
93 lines (73 loc) · 2.59 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
import cv2
import base64
import requests
import json
import time
# local configuration (changeable)
API_URL = "http://127.0.0.1:8000"
USER_ID = "Sourasish fondles"
def capture_image(prompt):
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Cannot open webcam")
return None
print(f"\n--- {prompt} ---")
print("Press *space-bar* to save the photo, or 'q' to quit.")
captured_frame = None
while True:
ret, frame = cap.read()
if not ret: break
cv2.imshow(f"{prompt} - Press 's'", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord(' '):
captured_frame = frame
print("Image captured!")
break
elif key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
return captured_frame
def encode_image_to_base64(image):
_, buffer = cv2.imencode('.jpg', image)
b64_string = base64.b64encode(buffer).decode('utf-8')
return f"data:image/jpeg;base64,{b64_string}"
def test_enrollment():
img = capture_image("Take photo for ENROLLMENT")
if img is None: return
b64_img = encode_image_to_base64(img)
payload = {"user_id": USER_ID, "image_b64": b64_img}
print(f"Sending request to {API_URL}/enroll...")
try:
response = requests.post(f"{API_URL}/enroll", json=payload)
print("Response Status:", response.status_code)
print("Response Body:", response.json())
except Exception as e:
print("Error:", e)
def test_verification():
img = capture_image("STEP 2: Take photo for VERIFICATION")
if img is None: return
b64_img = encode_image_to_base64(img)
payload = {"user_id": USER_ID, "image_b64": b64_img}
print(f"Sending request to {API_URL}/verify...")
try:
response = requests.post(f"{API_URL}/verify", json=payload)
print("Response Status:", response.status_code)
result = response.json()
print("Response Body:", json.dumps(result, indent=2))
if result.get("verified"):
print("\nSUCCESS: User Verified!")
else:
print("\nFAILED: User NOT Verified (Low Similarity)")
except Exception as e:
print("Error:", e)
if __name__ == "__main__":
try:
r = requests.get(f"{API_URL}/health")
print("Server Health:", r.json())
test_enrollment()
print("\nWaiting 2 seconds...\n")
time.sleep(2)
test_verification()
except requests.exceptions.ConnectionError:
print("❌ CRITICAL ERROR: Could not connect to server.")