Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python在mongodb gridfs中存储cv图像_Python_Mongodb_Numpy_Gridfs - Fatal编程技术网

Python在mongodb gridfs中存储cv图像

Python在mongodb gridfs中存储cv图像,python,mongodb,numpy,gridfs,Python,Mongodb,Numpy,Gridfs,为了进行测试,我们希望将标记的图像日期存储到mongodb数据库中 在图像管道中的某一点上,我们将标记的图像作为openCV图像,表示为numpy ndarray 如何存储图像?由于图像相对较大,我们考虑使用Gridfs 到目前为止,我们的简单代码是: from pymongo import MongoClient import gridfs import cv2 # access our image collection client = MongoClient('localhost', 2

为了进行测试,我们希望将标记的图像日期存储到mongodb数据库中

在图像管道中的某一点上,我们将标记的图像作为openCV图像,表示为numpy ndarray

如何存储图像?由于图像相对较大,我们考虑使用Gridfs

到目前为止,我们的简单代码是:

from pymongo import MongoClient
import gridfs
import cv2

# access our image collection
client = MongoClient('localhost', 27017)
db = client['testDatabaseONE']
testCollection = db['myImageCollection']

fs = gridfs.GridFS(db)

# read the image and convert it to RGB
image = cv2.imread('./testImage.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# store the image
imageID = fs.put(image)

# create our image meta data
meta = {
    'imageID': imageID,
    'name': 'testImage1'
}

# insert the meta data
testCollection.insert_one(meta)
很遗憾,imageID=fs.put(image)抛出以下错误:

回溯(最近一次调用上次):文件 “/usr/local/lib/python3.6/dist packages/gridfs/grid_file.py”,第行 337,书面形式 read=data.read AttributeError:'numpy.ndarray'对象没有属性'read'

在处理上述异常期间,发生了另一个异常:

回溯(最近一次调用上次):文件 “/home/johann/pycharm项目/mongoTesting/mongoTesting.py”,第17行, 在里面 imageID=fs.put(image)文件“/usr/local/lib/python3.6/dist-packages/gridfs/init.py”,第121行, 投入 grid_file.write(数据)文件“/usr/local/lib/python3.6/dist packages/gridfs/grid_file.py”,第行 341,书面形式 raise TypeError(“只能写入字符串或类似文件的对象”)TypeError:只能写入字符串或类似文件的对象


有没有关于如何使用gridfs存储图像的提示或想法,或者有没有更好的方法?

很明显,问题与图像大小无关。有两个例外,我们需要先解决第一个

回溯(最近一次调用):文件“/usr/local/lib/python3.6/dist packages/gridfs/grid_File.py”,第337行,在write read=data.read AttributeError:'numpy.ndarray'对象没有属性“read”

请检查文件grid_file.py”,第337行。numpy.ndarray没有称为read的方法。要从该数据数组中读取,您只需对所需内容进行切片,例如:

b = np.fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23],
       [30, 31, 32, 33],
       [40, 41, 42, 43]])

>>> b[0:5, 1]  # each row in the second column of b
array([ 1, 11, 21, 31, 41])

我通过将ndarray转换为字符串解决了这个问题

将新图像及其元数据存储到数据库中:

# convert ndarray to string
imageString = image.tostring()

# store the image
imageID = fs.put(imageString, encoding='utf-8')

# create our image meta data
meta = {
    'name': 'myTestSet',
    'images': [
        {
            'imageID': imageID,
            'shape': image.shape,
            'dtype': str(image.dtype)
        }
    ]
}

# insert the meta data
testCollection.insert_one(meta)
# get the image meta data
image = testCollection.find_one({'name': 'myTestSet'})['images'][0]

# get the image from gridfs
gOut = fs.get(image['imageID'])

# convert bytes to ndarray
img = np.frombuffer(gOut.read(), dtype=np.uint8)

# reshape to match the image size
img = np.reshape(img, image['shape'])
取回图像:

# convert ndarray to string
imageString = image.tostring()

# store the image
imageID = fs.put(imageString, encoding='utf-8')

# create our image meta data
meta = {
    'name': 'myTestSet',
    'images': [
        {
            'imageID': imageID,
            'shape': image.shape,
            'dtype': str(image.dtype)
        }
    ]
}

# insert the meta data
testCollection.insert_one(meta)
# get the image meta data
image = testCollection.find_one({'name': 'myTestSet'})['images'][0]

# get the image from gridfs
gOut = fs.get(image['imageID'])

# convert bytes to ndarray
img = np.frombuffer(gOut.read(), dtype=np.uint8)

# reshape to match the image size
img = np.reshape(img, image['shape'])

嘿,你试过使用numpy.array2string吗?因为我们想保存整个图像,所以我认为切片不是一个好办法。在这种情况下,只需对整行和整列进行切片,例如,a=np.arange(15)。重塑(3,5)和b=a[:,:]。你也可以使用b=a.data,它使用指针,但处理数据的方式会有所不同。