在这里,我想从数据库读取图像并对我的图像应用一些操作,例如噪声消除....最后我会应用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))
错误来自cv2.imread(img)
因为imread
在图像的URI
中获取string
或unicode
参数,但您使用的是完全不同的 Django 模型类。
假设您的Photo
类模型具有一个名为image
的ImageField
字段,您可以解决更改问题
img = cv2.imread(img)
到类似的东西
img = cv2.imread(img.image.url)