提交 203af447 authored 作者: Whispa's avatar Whispa

commit

上级 4a24139c
...@@ -2,68 +2,108 @@ package com.example.afrishop_v3.controllers; ...@@ -2,68 +2,108 @@ package com.example.afrishop_v3.controllers;
import com.example.afrishop_v3.base.Result; import com.example.afrishop_v3.base.Result;
import com.example.afrishop_v3.enums.ResultCodeEnum; import com.example.afrishop_v3.enums.ResultCodeEnum;
import com.google.api.client.util.IOUtils; import com.example.afrishop_v3.models.ItemLabel;
import com.example.afrishop_v3.models.Post;
import com.example.afrishop_v3.models.TbCfStationItem;
import com.example.afrishop_v3.repository.ItemLabelRepository;
import com.example.afrishop_v3.repository.TbCfStationItemRepository;
import com.google.cloud.vision.v1.*; import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.Feature.Type; import com.google.cloud.vision.v1.Feature.Type;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors; import org.springframework.data.domain.Page;
import com.google.protobuf.GeneratedMessageV3; import org.springframework.data.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import org.springframework.cloud.gcp.vision.CloudVisionTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("search/image") @RequestMapping("search/image")
public class ImageSearchController extends Controller { public class ImageSearchController extends Controller {
// Imports the Google Cloud client library // Imports the Google Cloud client library
private final TbCfStationItemRepository repository;
private final ItemLabelRepository labelRepository;
private final CloudVisionTemplate cloudVisionTemplate; public ImageSearchController(TbCfStationItemRepository repository, ItemLabelRepository labelRepository) {
private final ResourceLoader resourceLoader; this.repository = repository;
this.labelRepository = labelRepository;
}
public ImageSearchController(CloudVisionTemplate cloudVisionTemplate, ResourceLoader resourceLoader) { @GetMapping
this.cloudVisionTemplate = cloudVisionTemplate; public Result main(@RequestParam("url") String url) {
this.resourceLoader = resourceLoader;
// try { return new Result<>(getItemLabels(url));
// authExplicit();
// } catch (IOException e) {
// e.printStackTrace();
// }
} }
@GetMapping @PostMapping("/upload")
public Result main(@RequestParam("url") String url) throws Exception { public Result handleFileUpload(@RequestParam("file") MultipartFile file, @RequestParam(value = "pageNo",defaultValue = "0",required = false) Integer pageNo, @RequestParam(value = "pageSize",required = false,defaultValue = "12") Integer pageSize){
//authExplicit(); if( file != null ){
try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) { try {
List<ItemLabel> itemLabels = getItemLabels(file.getBytes());
List<String> collect = itemLabels.stream().map(ItemLabel::getDescription).collect(Collectors.toList());
String[] strings = collect.toArray(new String[]{});
Page<TbCfStationItem> itemsByImageSearch = repository.getItemsByImageSearch(strings, PageRequest.of(pageNo, pageSize));
return new Result<>(itemsByImageSearch.toList());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return new Result<>(new ArrayList<>());
}
@GetMapping("indexing")
public Result itemIndexing(@RequestParam("itemId") String itemId) {
Optional<TbCfStationItem> byId = repository.findById(itemId);
if( byId.isPresent() ){
TbCfStationItem stationItem = byId.get();
if( labelRepository.existsByItemItemId(itemId) )
return new Result<>(stationItem.getLabelList());
// The path to the image file to annotate String itemImg = stationItem.getItemImg();
String fileName = "./resources/wakeupcat.jpg"; String[] strings = itemImg != null ? itemImg.split(";") : new String[]{};
String img = strings.length > 0 ? strings[0] : itemImg;
if( img != null) {
List<ItemLabel> itemLabels = getItemLabels(img);
stationItem.setLabelList(itemLabels);
repository.save(stationItem);
return new Result<>(itemLabels);
}
}
return new Result<>(ResultCodeEnum.SERVICE_ERROR.getCode());
}
// Reads the image file into memory
Path path = Paths.get(fileName); private List<ItemLabel> getItemLabels(String url){
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
byte[] imageBytes = restTemplate.getForObject(url, byte[].class); byte[] imageBytes = restTemplate.getForObject(url, byte[].class);
return this.getItemLabels(imageBytes);
}
private List<ItemLabel> getItemLabels(byte[] imageBytes) {
//authExplicit();
try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
ByteString imgBytes = ByteString.copyFrom(imageBytes); ByteString imgBytes = ByteString.copyFrom(imageBytes);
// Builds the image annotation request // Builds the image annotation request
...@@ -77,27 +117,26 @@ public class ImageSearchController extends Controller { ...@@ -77,27 +117,26 @@ public class ImageSearchController extends Controller {
// Performs label detection on the image file // Performs label detection on the image file
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList(); List<AnnotateImageResponse> responses = response.getResponsesList();
List<Map<String,Object>> list = new ArrayList<>(); List<ItemLabel> list = new ArrayList<>();
for (AnnotateImageResponse res : responses) { for (AnnotateImageResponse res : responses) {
if (res.hasError()) { if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage()); System.out.format("Error: %s%n", res.getError().getMessage());
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode()); return new ArrayList<>();
} }
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { for (EntityAnnotation ann : res.getLabelAnnotationsList()) {
Map<String,Object> map = new LinkedHashMap<>();
annotation
.getAllFields()
.forEach((k, v) -> map.put(k.getJsonName(),v));
list.add(map);
}
return new Result<>(list); list.add(new ItemLabel(ann.getScore(), ann.getTopicality(), ann.getDescription(), ann.getMid()));
} }
return list;
} }
return new Result<>(new ArrayList<>()); } catch (IOException e) {
return new ArrayList<>();
} }
return new ArrayList<>();
}
} }
package com.example.afrishop_v3.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
@Entity
@Data
public class ItemLabel extends Model {
@JsonIgnore
@ManyToOne
private TbCfStationItem item;
private String description;
private double score;
private double topicality;
private String mid;
public ItemLabel(){
}
public ItemLabel(double score,double topicality,String description,String mid){
this.score = score;
this.topicality = topicality;
this.description = description;
this.mid = mid;
}
public String getDescription() {
return description;
}
}
...@@ -8,7 +8,9 @@ import org.hibernate.annotations.Where; ...@@ -8,7 +8,9 @@ import org.hibernate.annotations.Where;
import javax.persistence.*; import javax.persistence.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* 站点商品实体 * 站点商品实体
...@@ -130,10 +132,19 @@ public class TbCfStationItem { ...@@ -130,10 +132,19 @@ public class TbCfStationItem {
private int totalScore; private int totalScore;
@JsonIgnore
@OneToMany(mappedBy = "item")
private List<ItemLabel> labelList = new ArrayList<>();
public TbCfExpressTemplate getExpress() { public TbCfExpressTemplate getExpress() {
return express; return express;
} }
public List<ItemLabel> getLabelList() {
return labelList;
}
/** /**
* 设置:商品id * 设置:商品id
*/ */
...@@ -141,6 +152,11 @@ public class TbCfStationItem { ...@@ -141,6 +152,11 @@ public class TbCfStationItem {
this.itemId = itemId; this.itemId = itemId;
} }
public void setLabelList(List<ItemLabel> labelList) {
this.labelList = labelList;
}
/** /**
* 获取:商品id * 获取:商品id
*/ */
......
package com.example.afrishop_v3.repository;
import com.example.afrishop_v3.models.ItemLabel;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface ItemLabelRepository extends PagingAndSortingRepository<ItemLabel,String> {
boolean existsByItemItemId(String itemId);
}
...@@ -16,6 +16,8 @@ public interface TbCfStationItemRepository extends PagingAndSortingRepository<Tb ...@@ -16,6 +16,8 @@ public interface TbCfStationItemRepository extends PagingAndSortingRepository<Tb
Page<TbCfStationItem> findAllByItemLabelContaining(String label, Pageable pageable); Page<TbCfStationItem> findAllByItemLabelContaining(String label, Pageable pageable);
@Query(value = "select a from #{#entityName} a WHERE a.itemId <> :itemId and (a.itemDescritionId = :descriptionId or a.itemCategorytwo = :categoryTwo or a.itemCategory = :category )") @Query(value = "select a from #{#entityName} a WHERE a.itemId <> :itemId and (a.itemDescritionId = :descriptionId or a.itemCategorytwo = :categoryTwo or a.itemCategory = :category )")
Page<TbCfStationItem> getRecommendItems(@Param("itemId") String itemId,@Param("descriptionId") String descriptionId,@Param("categoryTwo") String categoryTwo,@Param("category") String category, Pageable pageable); Page<TbCfStationItem> getRecommendItems(@Param("itemId") String itemId,@Param("descriptionId") String descriptionId,@Param("categoryTwo") String categoryTwo,@Param("category") String category, Pageable pageable);
@Query(value = "select a from #{#entityName} a WHERE a in (SELECT l.item FROM ItemLabel l WHERE l.description IN :tags)")
Page<TbCfStationItem> getItemsByImageSearch(@Param("tags") String[] tags, Pageable pageable);
Page<TbCfStationItem> findAllByItemNameContainingOrItemTagsContaining(String itemName, String itemTags, Pageable pageable); Page<TbCfStationItem> findAllByItemNameContainingOrItemTagsContaining(String itemName, String itemTags, Pageable pageable);
Page<TbCfStationItem> findByItemCategory(String itemName, Pageable pageable); Page<TbCfStationItem> findByItemCategory(String itemName, Pageable pageable);
List<TbCfStationItem> findAllByItemDescritionId(String id); List<TbCfStationItem> findAllByItemDescritionId(String id);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论