django - 如何在 Django 中使用 opencv?

标签 django opencv

我是 Django 的初学者,我想在使用 opencv 上传之前将我的图像转换为灰度。 这是我尝试做的,但它不起作用

`

from __future__ import unicode_literals
from django.db import models
import cv2

class Personne(models.Model):
    nom = models.CharField(max_length=50, unique=True, help_text="Le nom est unique")
    description = models.TextField(blank=True)

    def __unicode__(self):
        return self.nom[:50]

def traitement(filename):
    img = cv2.imread(filename, 0)
    cv2.imwrite(filename, img)


class Visage(models.Model):

    personne = models.ForeignKey(Personne)

    def generate_filename(self, filename):
        return "Images/%s/%s" % (self.personne.nom, filename)

    traitement
    image = models.ImageField(blank=False, upload_to=generate_filename)
    ajoute_le = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.image.path`

最佳答案

您必须重写Visage 模型的save() 方法才能在保存后对数据执行一些操作。 适本地更改your_project_name

from your_project_name.settings import MEDIA_ROOT

class Visage(models.Model):
    ... 

    def save(self):
        super(Visage, self).save()
        filename = os.path.abspath(os.path.join(MEDIA_ROOT, self.image.name)
        traitement(filename)

关于django - 如何在 Django 中使用 opencv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36020441/

相关文章:

python - urls django 中 '^$' 和 '' 之间的区别

node.js - react 代理错误 : Could not proxy request/api/from localhost:3000 to http://localhost:8000 (ECONNREFUSED)

python-3.x - 错误 : How to fix "SystemError: <built-in function imshow> returned NULL without setting an error" in Opencv python

python - 在 python 中通过 libvlc 读取 IP 摄像机流并在 opencv 中处理帧

python - django-filter:使用 ChoiceFilter 并根据请求进行选择

sql - 在 django ORM 中使用 postgresql 窗口函数的干净方法?

Django Elastic Beanstalk secret 变量

python - jupyter 上没有模块 cv

python - 如何在OpenCV中过滤关键点匹配?

c++ - OpenCV 3.0 未定义的引用错误?