import cv2
import face_recognition
import datetime
def log_alert(msg):
with open("proctoring_log.txt", "a") as f:
f.write(f"{datetime.datetime.now()} - {msg}\n")
def main():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Could not access webcam.")
return
print("[INFO] Proctoring started. Press 'q' to quit.")
while True:
ret, frame = cap.read()
if not ret:
break
# Resize frame for faster processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
# Detect all faces
face_locations = face_recognition.face_locations(rgb_small_frame)
face_count = len(face_locations)
# Draw rectangles around faces
for (top, right, bottom, left) in face_locations:
top, right, bottom, left = top*4, right*4, bottom*4, left*4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
# Check for multiple faces
if face_count > 1:
cv2.putText(frame, "ALERT: Multiple Faces Detected!", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
log_alert("Multiple faces detected!")
# Show output
cv2.imshow("Exam Proctoring Feed", frame)
# Break on 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
print("[INFO] Proctoring ended.")
if __name__ == "__main__":
main()
No comments:
Post a Comment