mongodb 中上传图片的内容搜索

Content search of uploaded image in mongodb

我必须在文件中搜索内容。在数据库中上传的图像(bmp、tiff、png)或 pdf 等

我正在使用最新版本 Mongodb 来存储图像(png、bmp、jpg)或使用 GridFS 的文档。即以二进制形式存储数据。 MongoDB 使用两种方式存储文档,一种是二进制文件,另一种是 json .

所以Mongodb没有提供直接搜索图片内容的方法。 另一个是对我来说,我可以使用 OCR,但 OCR 以字符串形式提供最终结果,因此我必须将其转换为有效的 json 以存储在数据库中。如果这是我的最后选择,那么我将如何将该字符串转换为有效的 json 格式。

我正在尝试使用以下代码将文本文件存储在 mongodb 中。

// result5.txt is a text file that is result of OCR.        

string text = System.IO.File.ReadAllText("E:\result5.txt");
    
var document = BsonSerializer.Deserialize<BsonDocument>(text);

var collection = Database.GetCollection("articles");
         
collection.Insert(text);

但是我收到一个错误。

MongoCommandException: Command insert failed: Wrong type for documents[0]. Expected a object, got a string.

如何在我上传到数据库中的图片文件中进行搜索??

所以任何建议将不胜感激,请管理员不要为此关闭评论post谢谢。

此表单中存储的文本数据。

只需创建新的 class 以包含 OCR 结果:

public class OcrContainer
{
    public BsonObjectId Id { get; set; }
    public string OcrResult { get; set;}
}

然后将其存储到 mongo:

var collection = Database.GetCollection<OcrContainer >("articles");
collection.InsertOne(new OcrContainer {OcrResult = text});

之后您可以搜索您的结果:

collection.Find(x=>x.OcrResult.Contains("bla"))

但是: 你打算用它做什么?您将需要 OcrCollection 中的更多属性来将 ocr 结果与您的其他数据连接起来。