0

I am working on face recognition system for my academic project. I want to set the first time an employee was recognized as his first active time and the next time he is being recognized should be recorded as his last active time and then calculate the total active hours based on first active and last active time.

I tried with the following code but I'm getting only the current system time as the start time. can someone help me on what I am doing wrong.

Code:

data = pickle.loads(open(args["encodings"], "rb").read())

vs = VideoStream(src=0).start()

writers = None

time.sleep(2.0)

while True:

frame = vs.read()

rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

rgb = imutils.resize(frame, width=750)

r = frame.shape[1] / float(rgb.shape[1])

boxes = face_recognition.face_locations(rgb)

encodings = face_recognition.face_encodings(rgb, boxes)

names = []

face_names = []

for encoding in encodings:

    matches = face_recognition.compare_faces(data["encodings"],

      encoding)

    name = "Unknown"

if True in matches:

        matchedIdxs = [i for (i, b) in enumerate(matches) if b]

        counts = {}

        for i in matchedIdxs:

            name = data["names"][i]

            counts[name] = counts.get(name, 0) + 1

        name = max(counts, key=counts.get)

    names.append(name)

if names != []:
    for i in names:
        first_active_time = datetime.now().strftime('%H:%M')
        last_active_time = datetime.now().strftime('%H:%M')

        difference = datetime.strptime(first_active_time, '%H:%M') - datetime.strptime(last_active_time, '%H:%M')
        difference = difference.total_seconds()

        total_hours = time.strftime("%H:%M", time.gmtime(difference))
        face_names.append([i, first_active_time, last_active_time, total_hours])
asha A
  • 1
  • 2
  • Hi, where did you put your code in, because the `last_active_time` is executed immediately after `first_active_time` (both get the `datetime.now()` – Phung Duy Phong Feb 20 '20 at 09:46
  • Hi, you need to get the `first_active_time` and `last_active_time` at the step you push records in names or matches (depends on your logic, at the step of recognition), save the time of event of recognition maybe in tuple, list or dict etc... to keep the `datetime` data there, then calculate it here, because as your execute, `first_active_time = datetime.now().strftime('%H:%M')`, it get your time right now, not when the face recognised – Phung Duy Phong Feb 20 '20 at 10:19
  • Could you please help me with this Phung Duy Phong – asha A Feb 20 '20 at 10:21
  • Hi, where did you get `matches` and `names` – Phung Duy Phong Feb 20 '20 at 10:23
  • Hi Phung Duy Phong I have updated the full code. – asha A Feb 20 '20 at 10:26
  • Could you please help me with this. I am stuck with this part – asha A Feb 20 '20 at 10:29

0 Answers0