ImageController.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.mokasz.image_pool.restful.controller;
  2. import com.mokasz.image_pool.algorithm.FeatureExtractor;
  3. import com.mokasz.image_pool.algorithm.OpenCvService;
  4. import com.mokasz.image_pool.restful.entity.ImageQueryResponse;
  5. import com.mokasz.image_pool.restful.entity.ImageSaveResponse;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.jdbc.core.JdbcTemplate;
  10. import org.springframework.web.bind.annotation.*;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.io.IOException;
  13. import java.sql.ResultSet;
  14. import java.util.List;
  15. @RestController
  16. @RequestMapping("/service/image")
  17. public class ImageController {
  18. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  19. private final OpenCvService service = new OpenCvService();
  20. @Autowired private JdbcTemplate jdbcTemplate;
  21. @GetMapping("/")
  22. public ImageSaveResponse index() throws IOException {
  23. return new ImageSaveResponse("ok", 0);
  24. }
  25. @PostMapping("/store2")
  26. public ImageSaveResponse store2(@RequestParam("file") MultipartFile file) throws IOException {
  27. FeatureExtractor.compute(file.getBytes());
  28. return new ImageSaveResponse("OK", 1L);
  29. }
  30. @PostMapping("/store")
  31. public ImageSaveResponse store(
  32. @RequestParam("file") MultipartFile file, @RequestParam("pool") int pool) throws IOException {
  33. logger.info("File size: {} Name: {}", file.getSize(), file.getOriginalFilename());
  34. float[] floats = new float[8];
  35. service.calculatePHash(file.getBytes(), floats);
  36. float[] binaries = new float[64];
  37. service.transformBinaryArray(floats, binaries);
  38. logger.info("{}", binaries);
  39. String hash = service.getReadableHash(floats);
  40. int updated =
  41. jdbcTemplate.update(
  42. "INSERT INTO image_pool.image_hash VALUES (?, NOW(), ?) ON CONFLICT (hash) DO NOTHING",
  43. hash,
  44. binaries);
  45. logger.info("{}", updated);
  46. jdbcTemplate.update(
  47. "INSERT INTO image_pool.image_to_pool VALUES (?, ?, NOW()) ON CONFLICT (hash, pool_id) DO NOTHING",
  48. hash,
  49. pool);
  50. return new ImageSaveResponse(hash, 1L);
  51. }
  52. @PostMapping("/match")
  53. public List<ImageQueryResponse> match(
  54. @RequestParam("file") MultipartFile file,
  55. @RequestParam("pool") int pool,
  56. @RequestParam(value = "max", defaultValue = "1") float max,
  57. @RequestParam(value = "min", defaultValue = "1") float min,
  58. @RequestParam(value = "limit", defaultValue = "10") int limit)
  59. throws IOException {
  60. float[] floats = new float[8];
  61. service.calculatePHash(file.getBytes(), floats);
  62. float[] binaries = new float[64];
  63. service.transformBinaryArray(floats, binaries);
  64. logger.info("{}", floats);
  65. List<ImageQueryResponse> list =
  66. jdbcTemplate.query(
  67. "SELECT * FROM (SELECT hash, (64 - l2_distance(?::FLOAT2[], vector)) / 64 AS similarity FROM image_pool.image_hash WHERE hash IN (SELECT hash FROM image_pool.image_to_pool WHERE pool_id = ?) ORDER BY vector <-> ?::FLOAT2[] ASC) t WHERE similarity >=? and similarity<=? ORDER BY similarity DESC LIMIT ?",
  68. (ResultSet rs, int rowNum) ->
  69. new ImageQueryResponse(rs.getFloat("similarity"), rs.getString("hash")),
  70. binaries,
  71. pool,
  72. binaries,
  73. min,
  74. max,
  75. limit);
  76. return list;
  77. }
  78. }