I coded FaceCropped using cv2 and it works!
I want to put img through facecropped method in ProcessedImageField model.
Is there anyway?
What I want is to automatically cut the face and put it in the database when uploading pictures into student model.
method facecropped
import numpy as np
import cv2
import os
import glob
def FaceCropped(full_path, extra='face', show=False):
face_cascade = cv2.CascadeClassifier('C:../haarcascade_frontalface_default.xml')
full_path = full_path
path,file = os.path.split(full_path)
ff = np.fromfile(full_path, np.uint8)
img = cv2.imdecode(ff, cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3,5)
for (x,y,w,h) in faces:
cropped = img[y - int(h/4):y + h + int(h/4), x - int(w/4):x + w + int(w/4)]
result, encoded_img = cv2.imencode(full_path, cropped)
if result:
with open(path + '/' + extra + file, mode='w+b') as f:
encoded_img.tofile(f)
if show:
cv2.imshow('Image view', cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
my model.py
class Student(models.Model):
picture = ProcessedImageField(
verbose_name = 'picture',
upload_to = 'students/%Y/',
processors=[ResizeToFill(300,300)],
options={'quality':80},
format='JPEG',
null=True,
blank=True,
default='students/no-img.jpg',
)
name = models.CharField(max_length=255)
my views.py
class StudentAdd(FormView):
model = Student
template_name = 'student/list_add.html'
context_object_name = 'student'
form_class = AddStudent
def post(self, request):
form = self.form_class(request.POST, request.FILES)
if form.is_valid():
student = form.save(commit=False)
student.created_by = request.user
student.save()
messages.info(request, student.name + ' addddd', extra_tags='info')
if "save_add" in self.request.POST:
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
return redirect('student_detail', pk=student.pk, school_year=student.school_year)
return FormView.post(self, request)
question from:
https://stackoverflow.com/questions/65949196/django-cut-and-put-only-the-face-in-the-picture-field-using-opencv2 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…