Guten Tag!Mein Name ist Andrey Sobolev und heute werden wir ein einfaches "Brötchen" für Django erstellen, das das Gesicht der Person auf dem Foto überprüft (was in einer Reihe von Situationen nützlich ist).Dazu benötigen wir OpenCV und 5 Minuten Freizeit. Gehen.Installation
Installieren Sie zunächst die erforderlichen Bibliotheken im Docker-Container:FROM python:3.8.3-buster
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y \
gcc && apt-get install -y \
libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev libjpeg-dev libfreetype6-dev python-dev libpq-dev python-dev libxml2-dev libxslt-dev postgresql-client git && \
apt-get install -y libxrender1 libxext-dev fontconfig && \
pip3 install -U pip setuptools
COPY ./requirements.txt /home/
* * *
Und fügen Sie die Abhängigkeiten zu den Anforderungen hinzu.txt:opencv-python
matplotlib
numpy
Gesichtsprüffunktion detect_face
Erstellen Sie die Datei utils.py (im Hauptordner können Sie einen beliebigen anderen Ordner haben) und fügen Sie dort die folgenden Zeilen hinzu:import cv2 as cv
import numpy as np
def detect_face(in_memory_photo):
face_cascade = cv.CascadeClassifier(cv.__path__[0] + "/data/haarcascade_frontalface_default.xml")
in_memory_photo = in_memory_photo.read()
nparr = np.fromstring(in_memory_photo, np.uint8)
img = cv.imdecode(nparr, 4)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) > 0:
return True
else:
return False
Integration in das Benutzereingabeformular
Importieren Sie unsere Funktion:from main.utils import detect_face
Und "überprüfen Sie das Gesicht", wenn Sie einen neuen Benutzer registrieren.if request.method == "POST":
form = RegistrationForm(request.POST, request.FILES)
if form.is_valid():
try:
in_memory_uploaded_file = request.FILES["photo"]
except:
in_memory_uploaded_file = None
if not in_memory_uploaded_file or not detect_face(in_memory_uploaded_file):
messages.add_message(request, messages.WARNING, _("You can only upload a face photo. Please try to uploading a different photo."))
return HttpResponseRedirect(reverse("main:profile_registration"))
Ein Beispiel ohne Rahmen
import os
import cv2 as cv
import numpy as np
def detect_face(in_memory_photo):
face_cascade = cv.CascadeClassifier(cv.__path__[0] + "/data/haarcascade_frontalface_default.xml")
in_memory_photo = in_memory_photo.read()
nparr = np.fromstring(in_memory_photo, np.uint8)
img = cv.imdecode(nparr, 4)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) > 0:
return True
else:
return False
with open(os.path.dirname(os.path.abspath(__file__)) + '/photo.jpg', 'rb') as in_memory_photo:
is_it_face = detect_face(in_memory_photo)
print(is_it_face)
Das ist alles.Wie Sie sehen können, ist alles recht einfach (wenn Sie natürlich eine einfache Überprüfung benötigen).Nützliche Links
Richtung Datascience.com/face-detection-in-2-minutes-using-opencv-python-90f89d7c0f81new-friend.org/ru/195/2800/12918/profile-registration (ein Beispiel für die Funktionsweise dieser Lösung).