【问题标题】:expected string or Unicode object, Photo found in django预期的字符串或 Unicode 对象,在 django 中找到的照片
【发布时间】:2018-01-01 14:14:59
【问题描述】:

在这里,我想从 db 中读取图像并对我的图像应用一些操作,例如去噪 .... 最后我将应用 pytesseract 来获取文本

def GetData(request):

    img = Photo.objects.get(id=1)
    #wrapper = FileWrapper(open(img.file))


    # Read image with opencv
    img = cv2.imread(img)
    # Apply dilation and erosion to remove some noise

    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)

    b,g,r = cv2.split(img)
    # get b,g,r
    rgb_img = cv2.merge([r,g,b])
    # switch it to rgb
    # Denoising
    dst = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)
    img = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
    # Apply threshold to get image with only black and white
    img = cv2.adaptiveThreshold(img, 127, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11,2)
    new_image = cv2.blur(img, (1, 1))

【问题讨论】:

  • 你的问题是什么?
  • 请考虑编辑您的问题,添加诸如您如何定义Photo 类模型、您的堆栈跟踪到底是什么等信息
  • from django.db import models class Photo(models.Model): file = models.ImageField() description = models.CharField(max_length=255, blank=True) upload_at = models.DateTimeField(auto_now_add =True) class Meta: verbose_name = 'photo' verbose_name_plural = 'photos' 我想从照片中获取文本

标签: python django unicode


【解决方案1】:

错误来自cv2.imread(img),因为imread 采用stringunicode 参数与图像的URI,但您使用的是完全不同的Django 模型类。

假设您的 Photo 类模型有一个名为 imageImageField 字段,您可以通过更改来解决您的问题

img = cv2.imread(img)

类似

img = cv2.imread(img.image.url)

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2020-09-09
    • 2015-04-05
    • 2020-07-08
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多