Hi, I have an issue with my streamlit code when I perform facial recognition. If the person is recognized, they are asked for confirmation. If yes, then they are added to the csv file. The problem is that the confirmation form is being displayed infinitely. How do we solve it so it asks only once for confirmation?
while True:
successful_frame_read, frame = webcam.read()
if not successful_frame_read:
st.write("Failed to capture frame")
break
# Convert frame to RGB format
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Detect faces in the image
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
if len(face_locations) == 0:
# If no faces detected, check for timeout
if datetime.now() - last_known_face_time >= not_recognized_duration:
print("No recognized face detected for 30 seconds. Exiting...")
break
for face_encoding, (top, right, bottom, left) in zip(face_encodings, face_locations):
# Compare face encoding with known encodings
matches = face_recognition.compare_faces(encodeListKnown, face_encoding, tolerance=0.5)
if True in matches:
# Get the index of the matched face
match_index = matches.index(True)
student_id = studentIDs[match_index]
firstname, lastname = display_student_info(student_id)
if firstname and lastname:
if modeType == 0: # Check if mode is "Active" and confirmation is not displayed
confirmation_key = f"{firstname}_{lastname}_{datetime.now().strftime('%Y%m%d%H%M%S')}_confirmation"
#mylist = ("","No", "Yes")
# Initialize the session state
if 'selection' not in st.session_state:
st.session_state['selection'] = 0
confirmation = st.selectbox(f"Are you {firstname} {lastname}?", ("No", "Yes"), key=confirmation_key, index=st.session_state['selection'], placeholder="Sellect your option")
if confirmation is not None:
#confirmation_displayed = True # Confirmation displayed
if confirmation == "Yes":
# Draw rectangle and display student info
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, f"{lastname} {student_id}", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 1,(0, 255, 0), 2)
student_name = f"{firstname} {lastname}"
save_to_csv(student_name)
marked_time = datetime.now()
modeType = 1 # Change mode to "Marked"
mode_display.write("Mode Type: Marked")
# Wait for a second before changing to "Already Marked"
time.sleep(5)
modeType = 2
mode_display.write("Mode Type: Already Marked")
elif confirmation=="No":
mode_display.write("Mode Type: Active") # User rejected confirmation
# Update last known face time for timeout check
last_known_face_time = datetime.now()
else:
# Draw rectangle and label as "Unknown"
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(frame, "Unknown", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Update mode type and check for mode transitions
if modeType == 1 and datetime.now() - marked_time >= marked_duration:
modeType = 2 # Change mode to "Already Marked"
# Check if program should end due to "Already Marked" mode
if modeType == 2 and datetime.now() - marked_time >= already_marked_duration:
# Calculate total time and time taken to mark
total_time = datetime.now() - start_time
time_taken_to_mark = marked_time - start_time
# Display time taken to mark and total time
st.write(f"Time taken to mark: {time_taken_to_mark}")
st.write(f"Total time: {total_time}")
# End the program
break
# Display the frame with bounding boxes
temp.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB", use_column_width=True)
# Add a button to download the CSV file
csv_download_button = st.download_button(
label="Download Attendance CSV",
data=open('attendance.csv', 'rb'),
file_name='attendance.csv',
mime='text/csv'
)
if csv_download_button:
st.write("Download complete!")