Code
Code
import cv2
import time
import face_recognition
app = Flask(_name_)
camera = cv2.VideoCapture(0)
#
# # For an RTSP stream
# camera = cv2.VideoCapture("rtsp://your_ip_address:port/video")
#
# # For an HTTP stream (if supported by your camera)
# camera = cv2.VideoCapture("http://your_ip_address:port/video")
known_face_encodings.append(encoding)
known_face_names.append(name)
# Dictionary to store the last entry timestamp for each recognized face
last_entry_timestamps = {}
# Define the directory where you want to save the CSV files
csv_directory = 'CSV_FILES/'
capture_image_directory = 'captured_Unknown_images/'
# ...
def capture_unknown_face(frame):
current_time = time.time()
image_filename = f"{capture_image_directory}{current_time}.jpg"
cv2.imwrite(image_filename, frame)
print(f"Image of an unknown person saved as {image_filename}")
def mark_entry(name):
current_time = time.time()
# Check if this face has an entry timestamp and if enough time has passed
if name in last_entry_timestamps and current_time - last_entry_timestamps[name]
< cooldown_period:
print(f"Face {name} was recognized too soon. Skipping entry.")
return
print(recognized_entries)
def gen_frames():
while True:
success, frame = camera.read()
if not success:
break
face_names = []
for face_encoding in face_encodings:
# Use a threshold to recognize known faces
matches = face_recognition.compare_faces(known_face_encodings,
face_encoding, tolerance=0.6)
name = "Unknown"
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
face_names.append(name)
@app.route('/add_entry', methods=['POST'])
def add_entry():
name = request.form.get('name')
image_path = request.form.get('image_path')
return redirect('/')
@app.route('/')
def index():
return render_template('index.html', recognized_entries=recognized_entries)
@app.route('/get_entries', methods=['GET'])
def get_entries():
# You may need to filter and format your entries as needed
return jsonify(recognized_entries)
@app.route('/new')
def new():
return render_template('new.html')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace;
boundary=frame')
if _name_ == '_main_':
app.run(host='0.0.0.0', debug=True, port=5000)