Django 使用 opencv 剪切并仅将人脸放在图片字段中
Django cut and put only the face in the picture field using opencv
这是第一个问题
我有一些问题
第一的。即使我更新它也能正常工作,所以图片变得很奇怪。
第二。图像大小很奇怪,因为 'ProcessedImageField' 先工作, 'FaceCropped' 后来工作。
我们开始我的代码
class Student(models.Model):
picture = ProcessedImageField(
verbose_name = 'pictures',
upload_to = 'students/%Y/',
processors=[ResizeToFill(300,300)],
options={'quality':80},
format='JPEG',
null=True,
blank=True,
default='students/no-img.jpg',
)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
FaceCropped(self.picture.path)
def FaceCropped(full_path):
base_dir = os.path.dirname(os.path.abspath(__file__))
file_list = os.listdir('student')
for i in file_list:
if i == 'haarcascade_frontalface_default.xml':
face_cascade_path = os.path.join(base_dir, i)
face_cascade = cv2.CascadeClassifier(face_cascade_path)
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(full_path, mode='w+b') as f:
encoded_img.tofile(f)
break;
我想要的是当 'FaceCropped' 工作时,只有当我创建一个新模型时。
所以当我更新模型时它不起作用。
请帮帮我
我注意到您的 view.py.
中已经有一个 form_class class AddStudent
class StudentAdd(FormView):
model = Student
template_name = 'student/list_add.html'
context_object_name = 'student'
form_class = AddStudent
因此,您可以覆盖 form_class clean 方法并确保 FaceCropped
函数 运行 在 form.save()
.
之前
下面的答案 this question 应该可以回答您的问题
这是第一个问题
我有一些问题
第一的。即使我更新它也能正常工作,所以图片变得很奇怪。
第二。图像大小很奇怪,因为 'ProcessedImageField' 先工作, 'FaceCropped' 后来工作。
我们开始我的代码
class Student(models.Model):
picture = ProcessedImageField(
verbose_name = 'pictures',
upload_to = 'students/%Y/',
processors=[ResizeToFill(300,300)],
options={'quality':80},
format='JPEG',
null=True,
blank=True,
default='students/no-img.jpg',
)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
FaceCropped(self.picture.path)
def FaceCropped(full_path):
base_dir = os.path.dirname(os.path.abspath(__file__))
file_list = os.listdir('student')
for i in file_list:
if i == 'haarcascade_frontalface_default.xml':
face_cascade_path = os.path.join(base_dir, i)
face_cascade = cv2.CascadeClassifier(face_cascade_path)
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(full_path, mode='w+b') as f:
encoded_img.tofile(f)
break;
我想要的是当 'FaceCropped' 工作时,只有当我创建一个新模型时。
所以当我更新模型时它不起作用。
请帮帮我
我注意到您的 view.py.
中已经有一个 form_class classAddStudent
class StudentAdd(FormView):
model = Student
template_name = 'student/list_add.html'
context_object_name = 'student'
form_class = AddStudent
因此,您可以覆盖 form_class clean 方法并确保 FaceCropped
函数 运行 在 form.save()
.
之前
下面的答案 this question 应该可以回答您的问题