From 6ef55f3ab91c9b11cb973e3452b7a132716a705b Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 08:54:00 +0100 Subject: [PATCH 01/15] Update views.py --- webapp/views.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/webapp/views.py b/webapp/views.py index 3e65ee8..5afce18 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -1,9 +1,9 @@ from django.shortcuts import render, redirect from django.conf import settings from django.core.files.storage import FileSystemStorage -from .forms import UploadImageForm from .forms import ImageUploadForm from .dface import dface +from .dface import gallery def home(request): return render(request, 'home.html') @@ -11,18 +11,6 @@ def home(request): def about(request): return render(request, 'about.html') -def upload(request): - form = UploadImageForm(request.POST, request.FILES) - if form.is_valid(): - myfile = request.FILES['image'] - fs = FileSystemStorage() - filename = fs.save(myfile.name, myfile) - uploaded_file_url = fs.url(filename) - return render(request, 'upload.html', {'form': form, 'uploaded_file_url': uploaded_file_url}) - else: - form = UploadImageForm() - return render(request, 'upload.html', {'form': form}) - def detection(request): form = ImageUploadForm(request.POST, request.FILES) if request.method == 'POST': @@ -36,4 +24,9 @@ def detection(request): return render(request, 'detection.html', {'form':form, 'post':post}) else: form = ImageUploadForm() - return render(request, 'detection.html',{'form':form}) \ No newline at end of file + return render(request, 'detection.html',{'form':form}) + +def gallery(request): + imageURL = settings.MEDIA_URL + form.instance.document.name + gallery(settings.MEDIA_ROOT_URL + imageURL) + return render(request, 'gallery.html') From a8d8cb7e1940964569c0a576b185be18475047b6 Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 08:55:30 +0100 Subject: [PATCH 02/15] Update dface.py --- webapp/dface.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/webapp/dface.py b/webapp/dface.py index 7775c8a..d4c9e3d 100644 --- a/webapp/dface.py +++ b/webapp/dface.py @@ -27,4 +27,9 @@ def dface(path): else: print('someting error') - print(path) \ No newline at end of file + print(path) + +def gallery(path): + img = cv2.imread(path, 1) + cv2.imwrite(path, img) + From c412f5cc9fdea416318263eca50306a06844f377 Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 08:56:29 +0100 Subject: [PATCH 03/15] Update models.py --- webapp/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webapp/models.py b/webapp/models.py index e83cb42..331eb2c 100644 --- a/webapp/models.py +++ b/webapp/models.py @@ -1,6 +1,7 @@ from django.db import models class ImageUploadModel(models.Model): + title = models.CharField(max_length=50, blank=True) description = models.CharField(max_length=255, blank=True) document = models.ImageField(upload_to='documents/') - uploaded_at = models.DateTimeField(auto_now_add=True) \ No newline at end of file + uploaded_at = models.DateTimeField(auto_now_add=True) From e1f4663746c8915bde30d610332ccb7230b8def3 Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 08:57:00 +0100 Subject: [PATCH 04/15] Update urls.py --- webapp/urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webapp/urls.py b/webapp/urls.py index 8e19844..999ed05 100644 --- a/webapp/urls.py +++ b/webapp/urls.py @@ -7,8 +7,8 @@ urlpatterns = [ url('^$', views.home, name='home'), url('detection/', views.detection, name='detection'), - url('upload/', views.upload, name='upload'), + url('gallery/', views.gallery, name='gallery'), url('about/', views.about, name='about'), ] -urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file +urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) From f1b728bfbd54f9e817ffe55a49b79d7034643b75 Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 08:57:19 +0100 Subject: [PATCH 05/15] Update forms.py --- webapp/forms.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/webapp/forms.py b/webapp/forms.py index 2107c38..7d09bc9 100644 --- a/webapp/forms.py +++ b/webapp/forms.py @@ -1,12 +1,7 @@ from django import forms from .models import ImageUploadModel -class UploadImageForm(forms.Form): - title=forms.CharField(max_length=50) - #file = forms.FileField() - image=forms.ImageField() - class ImageUploadForm(forms.ModelForm): class Meta: model = ImageUploadModel - fields = ('description', 'document', ) + fields = ('title', 'description', 'document', ) From a9e3c7496eddb19d6b70362cdf40d3c740a6e7fd Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 09:25:35 +0100 Subject: [PATCH 06/15] Create trainmodel.py --- trainmodel.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 trainmodel.py diff --git a/trainmodel.py b/trainmodel.py new file mode 100644 index 0000000..e383868 --- /dev/null +++ b/trainmodel.py @@ -0,0 +1,26 @@ +from sklearn.preprocessing import LabelEncoder +from sklearn.svm import SVC +import argparse +import pickle + +# load the face embeddings +data = pickle.loads(open(args["embeddings"], "rb").read()) + +# encode the labels +le = LabelEncoder() +labels = le.fit_transform(data["names"]) + +# train the model used to accept the 128-d embeddings of the face and +# then produce the actual face recognition +recognizer = SVC(C=1.0, kernel="linear", probability=True) +recognizer.fit(data["embeddings"], labels) + +# write the actual face recognition model to disk +f = open(args["recognizer"], "wb") +f.write(pickle.dumps(recognizer)) +f.close() + +# write the label encoder to disk +f = open(args["le"], "wb") +f.write(pickle.dumps(le)) +f.close() From c78f4ab503d995ad364a3d91e2c469f24ef827cd Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 09:25:53 +0100 Subject: [PATCH 07/15] Create extract_embedings.py --- extract_embedings.py | 97 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 extract_embedings.py diff --git a/extract_embedings.py b/extract_embedings.py new file mode 100644 index 0000000..4d68c0c --- /dev/null +++ b/extract_embedings.py @@ -0,0 +1,97 @@ +from imutils import paths +import numpy as np +import argparse +import imutils +import pickle +import cv2 +import os + +# load our serialized face detector from disk +protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"]) +modelPath = os.path.sep.join([args["detector"], + "res10_300x300_ssd_iter_140000.caffemodel"]) +detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) + +# load our serialized face embedding model from disk +embedder = cv2.dnn.readNetFromTorch(args["embedding_model"]) + +# grab the paths to the input images in our dataset +print("[INFO] quantifying faces...") +imagePaths = list(paths.list_images(args["dataset"])) + +# initialize our lists of extracted facial embeddings and +# corresponding people names +knownEmbeddings = [] +knownNames = [] + +# initialize the total number of faces processed +total = 0 + +# loop over the image paths +for (i, imagePath) in enumerate(imagePaths): + # extract the person name from the image path + print("[INFO] processing image {}/{}".format(i + 1, + len(imagePaths))) + name = imagePath.split(os.path.sep)[-2] + + # load the image, resize it to have a width of 600 pixels (while + # maintaining the aspect ratio), and then grab the image + # dimensions + image = cv2.imread(imagePath) + image = imutils.resize(image, width=600) + (h, w) = image.shape[:2] + + # construct a blob from the image + imageBlob = cv2.dnn.blobFromImage( + cv2.resize(image, (300, 300)), 1.0, (300, 300), + (104.0, 177.0, 123.0), swapRB=False, crop=False) + + # apply OpenCV's deep learning-based face detector to localize + # faces in the input image + detector.setInput(imageBlob) + detections = detector.forward() + + # ensure at least one face was found + if len(detections) > 0: + # we're making the assumption that each image has only ONE + # face, so find the bounding box with the largest probability + i = np.argmax(detections[0, 0, :, 2]) + confidence = detections[0, 0, i, 2] + + # ensure that the detection with the largest probability also + # means our minimum probability test (thus helping filter out + # weak detections) + if confidence > args["confidence"]: + # compute the (x, y)-coordinates of the bounding box for + # the face + box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) + (startX, startY, endX, endY) = box.astype("int") + + # extract the face ROI and grab the ROI dimensions + face = image[startY:endY, startX:endX] + (fH, fW) = face.shape[:2] + + # ensure the face width and height are sufficiently large + if fW < 20 or fH < 20: + continue + + # construct a blob for the face ROI, then pass the blob + # through our face embedding model to obtain the 128-d + # quantification of the face + faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, + (96, 96), (0, 0, 0), swapRB=True, crop=False) + embedder.setInput(faceBlob) + vec = embedder.forward() + + # add the name of the person + corresponding face + # embedding to their respective lists + knownNames.append(name) + knownEmbeddings.append(vec.flatten()) + total += 1 + +# dump the facial embeddings + names to disk +print("[INFO] serializing {} encodings...".format(total)) +data = {"embeddings": knownEmbeddings, "names": knownNames} +f = open(args["embeddings"], "wb") +f.write(pickle.dumps(data)) +f.close() From 83e0b917b045d4db0376d5e3ef42c8d74e21191a Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 23:03:52 +0100 Subject: [PATCH 08/15] Delete extract_embedings.py --- extract_embedings.py | 97 -------------------------------------------- 1 file changed, 97 deletions(-) delete mode 100644 extract_embedings.py diff --git a/extract_embedings.py b/extract_embedings.py deleted file mode 100644 index 4d68c0c..0000000 --- a/extract_embedings.py +++ /dev/null @@ -1,97 +0,0 @@ -from imutils import paths -import numpy as np -import argparse -import imutils -import pickle -import cv2 -import os - -# load our serialized face detector from disk -protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"]) -modelPath = os.path.sep.join([args["detector"], - "res10_300x300_ssd_iter_140000.caffemodel"]) -detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) - -# load our serialized face embedding model from disk -embedder = cv2.dnn.readNetFromTorch(args["embedding_model"]) - -# grab the paths to the input images in our dataset -print("[INFO] quantifying faces...") -imagePaths = list(paths.list_images(args["dataset"])) - -# initialize our lists of extracted facial embeddings and -# corresponding people names -knownEmbeddings = [] -knownNames = [] - -# initialize the total number of faces processed -total = 0 - -# loop over the image paths -for (i, imagePath) in enumerate(imagePaths): - # extract the person name from the image path - print("[INFO] processing image {}/{}".format(i + 1, - len(imagePaths))) - name = imagePath.split(os.path.sep)[-2] - - # load the image, resize it to have a width of 600 pixels (while - # maintaining the aspect ratio), and then grab the image - # dimensions - image = cv2.imread(imagePath) - image = imutils.resize(image, width=600) - (h, w) = image.shape[:2] - - # construct a blob from the image - imageBlob = cv2.dnn.blobFromImage( - cv2.resize(image, (300, 300)), 1.0, (300, 300), - (104.0, 177.0, 123.0), swapRB=False, crop=False) - - # apply OpenCV's deep learning-based face detector to localize - # faces in the input image - detector.setInput(imageBlob) - detections = detector.forward() - - # ensure at least one face was found - if len(detections) > 0: - # we're making the assumption that each image has only ONE - # face, so find the bounding box with the largest probability - i = np.argmax(detections[0, 0, :, 2]) - confidence = detections[0, 0, i, 2] - - # ensure that the detection with the largest probability also - # means our minimum probability test (thus helping filter out - # weak detections) - if confidence > args["confidence"]: - # compute the (x, y)-coordinates of the bounding box for - # the face - box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) - (startX, startY, endX, endY) = box.astype("int") - - # extract the face ROI and grab the ROI dimensions - face = image[startY:endY, startX:endX] - (fH, fW) = face.shape[:2] - - # ensure the face width and height are sufficiently large - if fW < 20 or fH < 20: - continue - - # construct a blob for the face ROI, then pass the blob - # through our face embedding model to obtain the 128-d - # quantification of the face - faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, - (96, 96), (0, 0, 0), swapRB=True, crop=False) - embedder.setInput(faceBlob) - vec = embedder.forward() - - # add the name of the person + corresponding face - # embedding to their respective lists - knownNames.append(name) - knownEmbeddings.append(vec.flatten()) - total += 1 - -# dump the facial embeddings + names to disk -print("[INFO] serializing {} encodings...".format(total)) -data = {"embeddings": knownEmbeddings, "names": knownNames} -f = open(args["embeddings"], "wb") -f.write(pickle.dumps(data)) -f.close() From 86abc614e2cb118e7ed91676514e52b8ef1cc9ee Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Mon, 18 Nov 2019 23:04:15 +0100 Subject: [PATCH 09/15] Delete trainmodel.py --- trainmodel.py | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 trainmodel.py diff --git a/trainmodel.py b/trainmodel.py deleted file mode 100644 index e383868..0000000 --- a/trainmodel.py +++ /dev/null @@ -1,26 +0,0 @@ -from sklearn.preprocessing import LabelEncoder -from sklearn.svm import SVC -import argparse -import pickle - -# load the face embeddings -data = pickle.loads(open(args["embeddings"], "rb").read()) - -# encode the labels -le = LabelEncoder() -labels = le.fit_transform(data["names"]) - -# train the model used to accept the 128-d embeddings of the face and -# then produce the actual face recognition -recognizer = SVC(C=1.0, kernel="linear", probability=True) -recognizer.fit(data["embeddings"], labels) - -# write the actual face recognition model to disk -f = open(args["recognizer"], "wb") -f.write(pickle.dumps(recognizer)) -f.close() - -# write the label encoder to disk -f = open(args["le"], "wb") -f.write(pickle.dumps(le)) -f.close() From d332cbe463f7ee24717f3ba58cf36ba073a9acb8 Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Tue, 19 Nov 2019 20:26:18 +0100 Subject: [PATCH 10/15] nearly gallery --- webapp/views.py | 71 +++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/webapp/views.py b/webapp/views.py index 5afce18..cb98b93 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -1,32 +1,39 @@ -from django.shortcuts import render, redirect -from django.conf import settings -from django.core.files.storage import FileSystemStorage -from .forms import ImageUploadForm -from .dface import dface -from .dface import gallery - -def home(request): - return render(request, 'home.html') - -def about(request): - return render(request, 'about.html') - -def detection(request): - form = ImageUploadForm(request.POST, request.FILES) - if request.method == 'POST': - if form.is_valid(): - post = form.save(commit=False) - post.save() - - imageURL = settings.MEDIA_URL + form.instance.document.name - dface(settings.MEDIA_ROOT_URL + imageURL) - - return render(request, 'detection.html', {'form':form, 'post':post}) - else: - form = ImageUploadForm() - return render(request, 'detection.html',{'form':form}) - -def gallery(request): - imageURL = settings.MEDIA_URL + form.instance.document.name - gallery(settings.MEDIA_ROOT_URL + imageURL) - return render(request, 'gallery.html') +import os +from django.shortcuts import render, redirect +from django.conf import settings +from django.core.files.storage import FileSystemStorage +from .forms import ImageUploadForm +#from .forms import GalleryForm +from .dface import dface +#from .dface import gallery +from cv2 import cv2 + +def home(request): + return render(request, 'home.html') + +def about(request): + return render(request, 'about.html') + +def detection(request): + form = ImageUploadForm(request.POST, request.FILES) + if request.method == 'POST': + if form.is_valid(): + post = form.save(commit=False) + post.save() + + imageURL = settings.MEDIA_URL + form.instance.document.name + dface(settings.MEDIA_ROOT_URL + imageURL) + + return render(request, 'detection.html', {'form':form, 'post':post}) + else: + form = ImageUploadForm() + return render(request, 'detection.html',{'form':form}) + +def gallery(request): + imageURL = settings.MEDIA_ROOT_URL + settings.MEDIA_URL + 'documents/' + img_list = os.listdir(imageURL) + for file in img_list: + + img = cv2.imread(imageURL+file, 1) + # cv2.imwrite(imageURL+file, img) + return render(request, "gallery.html", {'documents': img_list}) \ No newline at end of file From 526b53ff330320fcae302726a8542ac092895211 Mon Sep 17 00:00:00 2001 From: ostrowskaaa <50330163+ostrowskaaa@users.noreply.github.com> Date: Tue, 19 Nov 2019 20:26:52 +0100 Subject: [PATCH 11/15] nearly gallery --- webapp/templates/gallery.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 webapp/templates/gallery.html diff --git a/webapp/templates/gallery.html b/webapp/templates/gallery.html new file mode 100644 index 0000000..32bd437 --- /dev/null +++ b/webapp/templates/gallery.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} +{% load webapp_tags %} + +{% block content %} + +