【问题标题】:How to store byte[] image in mongodb for Doocr process如何在 mongodb 中为 Doocr 进程存储 byte[] 图像
【发布时间】:2019-11-01 11:09:50
【问题描述】:

您好如何在mongodb中存储字节图像并执行doocr进程(通过public static void main)。是否可以通过对来自 mongodb 的存储图像执行 doocr

型号:

public class Photo {    
    @Id
    private String id;      
    private byte[] image; } getter & setter

控制器

@Controller
public class PhotoController {
   @GetMapping("/photos/upload")
    public String uploadPhoto(Model model) {
        model.addAttribute("message", "hello");
        return "uploadPhoto";
    }

在存储图像之后添加了 mongodb 图像路径。 ?这样做是正确的方法

主要:

 public static void main(String[] args) 
{   
SpringApplication.run(StackoverflowApplication.class, args);            

   /// mongodb data path is it right ? for doocr
 File image = new File("mongodb://localhost:27017//test-db//user");

   // encode nd decode  sample

String encodedString =Base64.getEncoder().encodeToString(originalInput. 
  getBytes());      
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
    String decodedString = new String(decodedBytes);

    Tesseract tessInst = new Tesseract();
    tessInst.setDatapath("C:\\Users\\Administrator\\Desktop\\tessdata");
    try {
            String result= tessInst.doOCR(image);
            System.out.println(result);
    } catch (TesseractException e) {
            System.err.println(e.getMessage());
    }           }

是否可能或需要任何其他 base64。

【问题讨论】:

  • 您是否在 mongoDB 中查找过 GridF。如果您想将文件存储在 mongoDB 中,那就太棒了
  • 实际上我不希望在我的控制器中执行 doocr [图像到文本] 过程并将图像、文本存储在 mongodb 中。这种图像类型是否可行,因为 byte [] 类型只执行这些任务。

标签: mongodb rest spring-mvc ocr tesseract


【解决方案1】:

由于您使用的是弹簧。您可以使用MultipartFile 获取控制器中的文件,然后使用org.bsonBinary 将文件存储到MongoDB,如果您的图像大小 16 MB,您可以使用GridFs)。

您只需要向您的项目添加一个依赖项 - spring-data-mongoDB

让我们以一个如下所示的 User 集合为例:

@Document
public class User {
    @Id
    private String id;
    
    private String name;
    private Binary image;
    // getters and setters
}

在这里你可以看到Binary image代表你的图片文件。

现在使用MongoRepository为此用户集合创建一个存储库

public interface UserRepository extends MongoRepository<User, String>{

}

为演示目的创建一个控制器。使用@RequestParam MultipartFile file 将文件获取到您的控制器,从文件中获取字节并将其设置为用户对象user.setImage(new Binary(file.getBytes())); 完整示例如下:

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @PostMapping("/users")
    User createUser(@RequestParam String name, @RequestParam MultipartFile file) throws IOException {
        User user = new User();
        user.setName(name);
        user.setImage(new Binary(file.getBytes()));
        
        return userRepository.save(user);
    }

    @GetMapping("/users")
    String getImage(@RequestParam String id) {
        Optional<User> user = userRepository.findById(id);
        Encoder encoder = Base64.getEncoder();
        
        return encoder.encodeToString(user.get().getImage().getData());

    }
}

启动服务器并点击如下邮递员截图所示的终点

您的数据以BinData格式存储在mongoDb中,要从数据库中获取数据请参考上述代码的getImage方法。

编辑:

提问者使用tess4j 库从图像中提取文本,doOCR 是该库中的一种方法。我已按照这些步骤在我的 Spring Boot 应用程序中从图像中提取文本。

  1. tesseract-ocr 安装到您的系统中:

    sudo apt-get install tesseract-ocr

  2. https://github.com/tesseract-ocr/tessdata 下载eng.traineddata 训练数据并将其移动到项目根文件夹。

  3. 将以下依赖项添加到您的项目中:

   <dependency>
        <groupId>net.sourceforge.tess4j</groupId>
        <artifactId>tess4j</artifactId>
        <version>3.2.1</version>
   </dependency>
  1. 将以下代码添加到现有项目中:
@GetMapping("/image-text")
String getImageText(@RequestParam String id) {
    Optional<User> user = userRepository.findById(id);
    ITesseract instance = new Tesseract();
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(user.get().getImage().getData());
        BufferedImage bufferImg = ImageIO.read(bais);
        String imgText = instance.doOCR(bufferImg);
        return imgText;
    } catch (Exception e) {
        return "Error while reading image";
    }
}

【讨论】:

  • 从给定的数据一步一步地进行,但是在点击 post 方法时,它显示了一个错误 404 not found 。我不知道发生了什么。如果它基于给定的图像工作,那么我们如何执行它;解码器解码器 = Base64.getDecoder();然后 doocr() 类似的东西。 ?.也许为那个解码和 doocr 进程创建了另一个 @getmapping,因为我们只存储编码细节。
  • 404 错误是由于您点击的端点不在您的应用程序中
  • 这是我完整的 Spring Boot 代码:drive.google.com/open?id=10gDvS8dkbVGT_syyu5smhRt0vuAmgAU3
  • 哦,好的,我会检查的。二进制图像数据类型真的支持 doocr 进程吗?或者将图像类型更改为 byte[]。你有关于 doocr[image to text] 的想法并将其存储在 mongodb 或高于你为执行 doocr 提供的源代码之上。
  • 我检查getImage 方法。 user.get().getImage().getData() 这一行提供了 byte[] 中的图像内容。我也没有在 doocr 上工作过
【解决方案2】:

您应该考虑使用名为 Spring Content for Mongo 的社区项目来存储内容。 Spring Content 之于非结构化数据(文档、视频、图像),Spring Data 之于结构化数据。它提供了对存储的抽象。为您提供相同的编程模型,以便快速轻松地实现基于 REST 的内容服务。

您可以像这样将它添加到您的项目中:

pom.xml

<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>spring-content-mongo</artifactId>
    <version>0.11.0</version>    <!-- 1.0.0.M1 for Spring Boot 2.2 -->
</dependency>
<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>spring-content-rest</artifactId>
    <version>0.11.0</version>    <!-- 1.0.0.M1 for Spring Boot 2.2 -->
</dependency>

确保您的应用程序上下文中存在 GridFsTemplate bean。 Mongo 存储和 REST API 已启用。类似于以下内容:

@Configuration
@EnableMongoStores
@Import(org.springframework.content.rest.config.RestConfiguration.class) // Enable REST API
public class MongoConfig extends AbstractMongoConfiguration {

   @Bean
   public GridFsTemplate gridFsTemplate() throws Exception {
      return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
   }
   ...

要允许内容与您的照片实体相关联,请为其赋予以下属性:

Photo.java

public class Photo {    

    @Id
    private String id;      
    //private byte[] image;    replace this with -->

    @ContentId
    private String contentId;

    @ContentLength 
    private long contentLength = 0L;

    @MimeType
    private String mimeType;

添加商店界面:

PhotoStore.java

public interface PhotoStore extends ContentStore<Photo, String> {
}

这就是你所需要的。当您的应用程序启动时,Spring Content 将看到对 Mongo/REST 模块的依赖关系,它将注入 PhotoStore 的 GridFS 实现,以及支持完整 CRUD 功能并将这些操作映射到 @987654331 的控制器的实现@ 界面。 REST 端点将在/photos 提供。

curl -X PUT /photos/{photoId} -F "file=@/some/image.jpg" 将创建或更新照片

curl -X GET /photos/{photoId} 将获取照片

curl -X DELETE /photos/{photoId} 将删除照片

有一些入门指南here。他们将 Spring Content 用于文件系统,但模块是可互换的。 Mongo 参考指南是here。有一个教程视频here。还有一个示例项目here

HTH

【讨论】:

    猜你喜欢
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2012-09-14
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2019-05-06
    相关资源
    最近更新 更多