Raspberry Pi – Gesichtserkennung
bevor irgendetwas! Diese Woche werde ich mit einer kleinen Gruppe von Blogger und Rissen durch die VMworld in Barcelona sein, gib mir eine E-Mail, der will, gute Zeiten teilen und sehen Sie es! gut, Nougat, Ich lasse diesem Dokument werden die Schritte ein Raspberry Pi mit einer USB-Kamera zu bekommen verbunden, Gesichter erkennen!
Wir OpenCV installieren und anschließend durch ein Skript lernen, die Gesichter mit, die uns interessieren, Ich zeige dir meine und meine txabala. Wir zupfen eine andere Funktion Schrifterkennung mein Gesicht zu erkennen und ein Skript und einen anderen laufen die txabala erkennen.
Die erste ist, die Voraussetzungen für OpenCV oder ein installieren, benötigen:
sudo apt-get install build-essential cmake pkg-config python-dev libgtk2.0-dev libgtk2.0 zlib1g-dev libpng-dev libjpeg-dev libtiff-dev libjasper-dev libavcodec-dev swig unzip vim sudo apt-get install python-numpy python-opencv sudo apt-get install python-pip sudo apt-get install python-dev sudo pip install picamera sudo pip install rpio sudo apt-get install v4l2ucp v4l-utils libv4l-dev
Wir heruntergeladen OpenCV, kompilieren und installieren:
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip unzip opencv-2.4.9.zip cd opencv-2.4.9 cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_gpu=OFF -DBUILD_opencv_ocl=OFF make sudo make install
gut, für die Gesichtserkennung, wir werden eine kleine BD erste Seite herunterladen, eine bessere Genauigkeit haben und auf die wir unser Gesicht oder dass das Interesse uns Beitrag hinzufügen. Wir haben die BD Probe der Datenbank von Faces of AT off&T Laboratories Cambridge, zuvor erstellten wir den Ordner, in dem Sie alle unsere Inhalte setzen werden und dekomprimiert:
mkdir /home/pi/recoFacial cd /home/pi/recoFacial wget http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip unzip att_faces.zip
Jetzt haben wir diese XML und entpacken Sie es in / home / pi / recoFacial /:
wget http://www.bujarra.com/wp-content/uploads/2016/08/haarcascade_frontalface_alt.zip unzip haarcascade_frontalface_alt.zip
Am Ende des Dokuments, Ich lasse ein paar Dateien Python, man wird Gesichter lernen (capture.py) und die andere, sie zu erkennen (reconocimiento.py), Sie haben, um sie durchsetzbar zu machen:
chmod +x reconocimiento.py capture.py
Und eine Empfehlung, erhöhen würde die Anzahl der Bilder, dass der Fang Ihr Gesicht zu erkennen, wenn das Lernen, wobei, in capture.py, statt 20 Standardbild, Wir können zum Beispiel setzen 100.
Um zu erfahren, führen Sie ein Gesicht:
python capture.py nombrePersona
So starten Sie Gesichter erkennen und erkennen,:
python reconocimiento.py
capture.py
import cv2, sys, numpy, os size = 4 fn_haar = 'haarcascade_frontalface_alt.xml' fn_dir = 'att_faces/orl_faces' fn_name = sys.argv[1] path = os.path.join(fn_dir, fn_name) if not os.path.isdir(path): os.mkdir(path) (im_width, im_height) = (112, 92) haar_cascade = cv2.CascadeClassifier(fn_haar) webcam = cv2.VideoCapture(0) count = 0 while count > 100: (rval, im) = webcam.read() im = cv2.flip(im, 1, 0) gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size)) faces = haar_cascade.detectMultiScale(mini) faces = sorted(faces, key=lambda x: x[3]) if faces: face_i = faces[0] (x, y, w, h) = [v * size for v in face_i] face = gray[y:y + h, x:x + w] face_resize = cv2.resize(face, (im_width, im_height)) pin=sorted([int(n[:n.find('.')]) for n in os.listdir(path) if n[0]!='.' ]+[0])[-1] + 1 cv2.imwrite('%s/%s.png' % (path, pin), face_resize) cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3) cv2.putText(im, fn_name, (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN, 1,(0, 255, 0)) count += 1 cv2.imshow('OpenCV', im) key = cv2.waitKey(10) if key == 27: break
reconocimiento.py
import cv2, sys, numpy, os size = 4 fn_haar = 'haarcascade_frontalface_alt.xml' fn_dir = 'att_faces/orl_faces' # Part 1: Creando fisherRecognizer print('Formando...') # Crear una lista de imagenes y una lista de nombres correspondientes (images, lables, names, id) = ([], [], {}, 0) for (subdirs, dirs, files) in os.walk(fn_dir): for subdir in dirs: names[id] = subdir subjectpath = os.path.join(fn_dir, subdir) for filename in os.listdir(subjectpath): path = subjectpath + '/' + filename lable = id images.append(cv2.imread(path, 0)) lables.append(int(lable)) id += 1 (im_width, im_height) = (112, 92) # Crear una matriz Numpy de las dos listas anteriores (images, lables) = [numpy.array(lis) for lis in [images, lables]] # OpenCV entrena un modelo a partir de las imagenes model = cv2.createFisherFaceRecognizer() model.train(images, lables) # Part 2: Utilizar fisherRecognizer en funcionamiento la camara haar_cascade = cv2.CascadeClassifier(fn_haar) webcam = cv2.VideoCapture(0) while True: (rval, frame) = webcam.read() frame=cv2.flip(frame,1,0) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size)) faces = haar_cascade.detectMultiScale(mini) for i in range(len(faces)): face_i = faces[i] (x, y, w, h) = [v * size for v in face_i] face = gray[y:y + h, x:x + w] face_resize = cv2.resize(face, (im_width, im_height)) # Intentado reconocer la cara prediction = model.predict(face_resize) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) # Escribiendo el nombre de la cara reconocida # [1] if prediction[1]>500: cv2.putText(frame, '%s - %.0f' % (names[prediction[0]],prediction[1]), (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0)) #La variable cara tendra el nombre de la persona reconocida cara = '%s' % (names[prediction[0]]) #En caso de que la cara sea de Hector if cara == "HECTOR": os.system("/home/pi/hector.sh") #En caso de que la cara sea de Seila elif cara == "SEILA": os.system("/home/pi/seila.sh") #Si la cara es desconocida, poner desconocido else: cv2.putText(frame, 'Desconocido', (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0)) #No hay nadie s.system("/home/pi/nadie.sh") cv2.imshow('OpenCV', frame) key = cv2.waitKey(10) if key == 27: break
empfohlene Beiträge:
- Animierte LEDs auf unserem Windows-Bildschirm mit Hyperion - 28 von März von 2023
- Überwachen Sie Dell EMC Data Domain - 23 von März von 2023
- Regen messen mit Home Assistant - 21 von März von 2023