Java 如何对Mongo DB的存储图像进行Tesseract Ocr
我将图像作为二进制数据存储在Mongodb中,无法对其执行ocr。我以两种不同的方式接受了考验Java 如何对Mongo DB的存储图像进行Tesseract Ocr,java,spring,mongodb,ocr,tesseract,Java,Spring,Mongodb,Ocr,Tesseract,我将图像作为二进制数据存储在Mongodb中,无法对其执行ocr。我以两种不同的方式接受了考验 我被单独使用@Getmapping+@Asyn来获取基于对象id的图像 第二个被用作基于Mongodb的Uri的独立程序[public static void main] 两个案件都无法做到这一点有人知道如何以正确的方式做到这一点吗??。 public class User { @Id private String id; private String name;
public class User {
@Id
private String id;
private String name;
private Binary image;}} getters & setters , constructors
控制器[对于上传图像和检索的图像作为二进制数据非常有效]
@PostMapping("/upload")
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("/retrive")
String getImage(@RequestParam String id) {
Optional<User> user = userRepository.findById(id);
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(user.get().getImage().getData());
}
您应该考虑使用社区项目调用Mongo来存储内容。Spring内容与非结构化数据(文档、视频、图像)的关系,就像Spring数据与结构化数据的关系一样。它提供了对存储的抽象。为您提供相同的编程模型,以便快速轻松地实现基于REST的内容服务 您可以将其添加到项目中,如下所示: pom.xml 要允许内容与用户实体关联,请为其指定以下属性: User.java 添加存储界面: UserImageStore.java 这里有一些入门指南。它们在文件系统中使用Spring内容,但存储模块是可互换的。Mongo参考指南是。有一个教程视频。以及一个示例项目
HTH嗨,Paul Warren,应用程序成功启动,并在点击发送时将“”与post man form数据中的图像文件一起使用,结果显示为404 not found error。我添加了jar和依赖项,如spring content mongo、spring content rest、content store api,所有内容都正确,但邮递员不接受url。它是否被接受,而应用程序属性中没有提到任何数据库,并且我们没有使用任何@Restcontroller上载和检索图像。ocr控制器在tesseract中也显示错误,它不接受依赖项。如果不仔细看的话,很难判断。您不需要创建任何控制器,这一切都已为您完成。听起来有些bean没有注册。也不需要数据库。应该使用gridfs。tesseract事件处理程序只是一个示例。但是如果你能用一个小项目来重现这个问题,请把它推到你在github的组织,然后在github的问题中引用到spring内容;上传是多部分上传吗?网址;
vijay
真的是用户ID吗?
@Bean(name = "threadPoolTaskExecutor")
public Executor threadPoolTaskExecutor()
{ return new ThreadPoolTaskExecutor();
}
@Async("threadPoolTaskExecutor")
@GetMapping("/image")
public String asyncMethodWithConfiguredExecutor(@RequestParam String id)
{
System.out.println("Execute method" + Thread.currentThread().getId());
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";
} }
@SpringBootApplication
public class StackoverflowApplication
{
public static void main(String[] args) throws IOException
{
SpringApplication.run(StackoverflowApplication.class, args);
Mongo mongodb = new MongoClient("localhost", 27017);
DB db = mongodb.getDB("test-db");
DBCollection collection = db.getCollection("user");
File image = new File("mongodb://localhost:27017//test-db//user");
Tesseract tessInst = new Tesseract();
tessInst.setDatapath("C:\\Program Files (x86)\\Tesseract-OCR\\tessdata");
try {
String result= tessInst.doOCR(image);
System.out.println(result);
} catch (TesseractException a) {
System.err.println(a.getMessage());
} } }
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-mongo</artifactId>
<version>0.12.0</version> <!-- 1.0.0.M3 for Spring Boot 2.2 -->
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest</artifactId>
<version>0.12.0</version> <!-- 1.0.0.M3 for Spring Boot 2.2 -->
</dependency>
@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());
}
...
public class User {
@Id
private String id;
//private Binary image; replace this with -->
@ContentId
private String contentId;
@ContentLength
private long contentLength = 0L;
@MimeType
private String mimeType;
public interface UserImageStore extends ContentStore<User, String> {
}
@StoreEventHandler
public static class OcrHandler {
@Autowired
private UserImageStore images;
@HandleAfterSetContent
@Order(Ordered.LOWEST_PRECEDENCE)
public void handleAfterSetContent(User user) {
tesseract.TessBaseAPI api = new tesseract.TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init("/path/to/your/trained/data", "eng") != 0) {
...
}
byte[] bytes = new byte[0];
try {
// get the image from the user for ocr processing
bytes = IOUtils.toByteArray(images.get(user));
}
catch (IOException e) {
...
}
lept.PIX pix = pixReadMem(bytes, bytes.length);
api.SetImage(pix);
BytePointer outText = api.GetUTF8Text();
// use outText.getString() containing the ocr result
api.End();
outText.deallocate();
pixDestroy(pix);
}
}