Use ESP32 camera stream for face recognition

Hi, can someone help me please in blow code. I want to use ESP32 camera to be my input stream instead of my webcam.

import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime

path = 'Attendance'
images = []
classnamess = []
mylist = os.listdir(path)
print(mylist)

for cls in mylist:
    curImg = cv2.imread(f'{path}/{cls}')
    images.append(curImg)
    classnamess.append(os.path.splitext(cls)[0])
print(classnamess)

def findEncodings(images):
    encodelist= []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encodelist.append(encode)
    return encodelist

def markAttendance(name):
    with open('attandancetable.csv','r+') as f:
        myDatalist = f.readlines()
        nameList =[]
        for line in myDatalist:
            entry = line.split(',')
            nameList.append(entry[1])
        if name not in nameList:
            now = datetime.now()
            dtstring = now.strftime('%D:%H:%M:%S')
            f.writelines(f'\n{name},{dtstring}')

encodeListKnown = findEncodings(images)
print('Encoding Complete')

cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
    facesCurFrame = face_recognition.face_locations(imgS)
    encodesCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)

    for encodeFace,faceLoc in zip(encodesCurFrame,facesCurFrame):
        matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
        faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
        print (faceDis)
        matchIndex = np.argmin(faceDis)

        if matches[matchIndex]:
            name = classnamess[matchIndex].upper()
            print (name)
            y1,x2,y2,x1 = faceLoc
            y1, x2, y2, x1 = y1*4,x2*4,y2*4,x1*4
            cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
            cv2.rectangle(img,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
            cv2.putText(img,name,(x1+6,y2-6),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
            markAttendance(name)

    cv2.imshow('webcam',img)
    cv2.waitKey(1)

Hi Badar,

…and what is the belonging question / problem?

Cheers, Dominik

This code uses openCV library for face recognition. By default it use laptop webcam and I want to use ESP32-CAM camera to be my input instead of laptop webcam.
ESP32-CAM opens through an IP link in the browser, so how I can use this link in python?

I see, might this:

be the answer for you?

Cheers, Dominik

1 Like

It was very helpful, thank you.
But still I can not connect ESP32-CAM by this way, it looks like this kind of cameras not easy to connect with python.

Thank you very much.

What happens when you try? Have you access to the camera with other tools? Is the firewall configured properly?..

Cheers, Dominik

This camera has one way to access only through this link http://192.168.101.26 and there is no password or admin panel. I try to put this link put in not works. Only one person can access to camera at the time.
rtsp://192.168.101.26 I try link this also not works.

What happens if you try cv2.VideoCapture(…) on said strings? Error message?

This is the error message:
File “C:/Users/Badar Al-s/pythonProject/Attandanceproject.py”, line 42
cap = cv2.VideoCapture(rtsp://192.168.101.26)
^
SyntaxError: invalid syntax

That is easy: you forgot the quotation marks, try:

cap = cv2.VideoCapture(“rtsp://192.168.101.26”)

instead…

Cheers, Dominik

Traceback (most recent call last):
File “C:/Users/Badar Al-s/pythonProject/Attandanceproject.py”, line 46, in
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-vi271kac\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’

It shows this error now

Hi Badar,

please try to read and understand the error messages on your own too:

gives you the hint, that a ‘size’ you have given to cv::resize is empty.

says, that said error occured in line 46 of your Attandanceproject.py file, which seems to be:

imgS = cv2.resize(img, (0,0), None, 0.25,0.25)

where you can find ‘(0,0)’ as the new image size - which obviously is not a valid (as it is ‘empty’) size. The ‘None’ in the same line might as well be a or even the problem.

Cheers, Dominik

1 Like

Thank you for your support, I will use my webcam for now snice is time to submit this program now and I will try to fix this issue in same time.