-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSource-Code
More file actions
118 lines (91 loc) · 3.44 KB
/
Copy pathSource-Code
File metadata and controls
118 lines (91 loc) · 3.44 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
import sys
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
sys.stderr.reconfigure(encoding='utf-8', errors='replace')
import cv2
import requests
import time
import os
os.environ['DISPLAY'] = ':0'
# -- API Settings -------------------------------------------
SERVER_URL = "https://www.circuitdigest.cloud/api/v1/face-detection/detect"
API_KEY = "YourApikey"
# -- Mode Selection -----------------------------------------
# "auto" ? captures every X seconds (VNC / SSH)
# "keyboard" ? press SPACE to capture (Direct Monitor)
MODE = "keyboard"
AUTO_INTERVAL = 5 # seconds between captures
# -- Camera Setup -------------------------------------------
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
if not cap.isOpened():
print("Camera not found! Check USB camera connection.")
sys.exit()
print("Camera initialized.")
print(f"Running in [{MODE}] mode")
if MODE == "keyboard":
print("Press SPACE to capture | Press ESC to quit")
elif MODE == "auto":
print(f"Auto capturing every {AUTO_INTERVAL} seconds | Press ESC to quit")
# -- Send Image to API --------------------------------------
def count_faces(frame):
# Warm-up frames
for _ in range(3):
cap.read()
ret, frame = cap.read()
if not ret:
print("Capture failed")
return
_, img_encoded = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
img_bytes = img_encoded.tobytes()
headers = { "X-API-Key": API_KEY }
files = { "imageFile": ("snap.jpg", img_bytes, "image/jpeg") }
try:
print("\nPhoto captured! Sending to API...")
response = requests.post(SERVER_URL, headers=headers,
files=files, timeout=15)
if response.status_code == 200:
result = response.json()
safe_response = response.text.encode('utf-8', errors='replace').decode('utf-8')
print("Response:", safe_response)
# Parse face count
face_count = result.get("face_count", 0)
if face_count == 0:
face_count = result.get("detection_count", 0)
print(f"Faces detected: {face_count}")
if face_count == 0:
print("Status: No faces detected.")
elif face_count == 1:
print("Status: 1 person detected.")
else:
print(f"Status: {face_count} persons detected.")
else:
print(f"HTTP error: {response.status_code}")
except requests.exceptions.Timeout:
print("Request timed out!")
except Exception as e:
print(f"Error: {str(e).encode('utf-8', errors='replace').decode('utf-8')}")
# -- Main Loop ----------------------------------------------
last_capture_time = 0
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
cv2.imshow("Face Detection - SPACE: capture | ESC: quit", frame)
key = cv2.waitKey(1) & 0xFF
if MODE == "keyboard":
if key == 32: # SPACE
print("\nCapturing image...")
count_faces(frame)
elif MODE == "auto":
current_time = time.time()
if current_time - last_capture_time >= AUTO_INTERVAL:
last_capture_time = current_time
print("\nAuto capturing...")
count_faces(frame)
if key == 27: # ESC
print("Quitting...")
break
cap.release()
cv2.destroyAllWindows()