在 Django 应用程序中,我将在我的 models.py 中的 image
类下建立一个图像数据库。不幸的是,这些图像中的一些可能是彼此重复的,我想编写一个应用程序(部分)允许我标记这些重复的图像。作为这样的数据库设置的新手,将其实现到我的 models.py
中的最佳方法是什么?
我的models.py
如下:
class duplicate(models.Model):
#some kind of code goes here?
#perhaps...
models.ImageField(upload_to='directory/') #not uploading a new image here- just want to link it to a database full of images somehow?
class image(models.Model):
image = models.ImageField(upload_to='directory/')
duplicate = models.ManyToManyField(duplicate, null=True) #is this the correct way to do this?
最佳答案
您可以破解 Model.save/delete 方法以将图像名称和校验和存储在数据库中,然后您可以使用一种方法来计算具有相同校验和的图像的数量。
未经测试,只是为了让您朝着正确的方向开始:
class ImageAccounting(models.Model):
fk = models.IntegerField()
model_name = models.CharField(max_length=100)
md5 = models.CharField(max_length=32)
class SomeModel(models.Model)
...
image = models.ImageField(upload_to='somewhere')
...
def image_signature(self):
md5 = hashlib.md5(self.image.file.read()).hexdump()
model_name = self.__name__
return md5, model_name
def save(self, *args, *kwargs):
super(SomeModel, this).save(*args, **kwargs)
md5, model_name = self.image_signature()
try:
i = ImageAccounting.objects.get(fk=self.pk, md5=md5, model_name=model_name)
except ImageAccounting.DoesNotExist:
i = ImageAccounting(fk=self.pk, md5=md5, model_name=model_name)
i.save()
def delete(self, *args, **kwargs):
super(SomeModel, this).delete(*args, **kwargs)
md5, model_name = self.image_signature()
ImageAccounting.objects.filter(fk=self.pk, md5=md5, model_name=model_name)\
.delete()
def copies(self):
md5, _ = self.image_signature()
return ImageAccounting.objects.filter(md5=md5)
[更新]
Not all of the images will be cropped perfectly the same, but I really like where we're going here. In my case, I have a database full of images that could be duplicates of each other (but not the same scans, so they'll checksum differently). I need a way to say, "this image looks really similar to that other one I saw a few hours ago. I want them to be linked and include a description of why." It doesn't have to be automagic, just a way for me to say "these two images that I uploaded once upon a time are related." A manytomany relationship, if you will, of multiple images (class image's). – mh00h
如果图像不是完全相同的,那么我们正在进入模糊数据库和计算机视觉领域。这些不是 CS 的较简单主题,恐怕完整的答案不适合这个空间,但它是可行的 - OpenCV有一个 Python 接口(interface),它是那种受益于 Python 支持的快速原型(prototype)制作的项目。
As a result, all I am wanting to do is to mark in my database that two images, already in the database, are duplicates of each other. A user will be manually tagging the images as duplicates of each other. I just don't know how to define the many-to-many relationship in my models. A computer will not be discovering the duplicates, a user will. – mh00h
如果有人将图像归类为重复图像,您只需创建一个对称的递归关系。创建 recursive relationship – 与自身具有多对一关系的对象 – 使用 models.ManyToManyField('self')
,不需要中间模型:
duplicates = models.ManyToManyField('self', null=True)
关于python - 将图像模型与相似图像相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17637626/