You've already forked FrameTour-BE
BCE临时使用
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package com.ycwl.basic.controller.extern;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.ycwl.basic.annotation.IgnoreToken;
|
||||
import com.ycwl.basic.image.enhancer.adapter.BceImageEnhancer;
|
||||
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
|
||||
import com.ycwl.basic.mapper.AioDeviceMapper;
|
||||
import com.ycwl.basic.mapper.MemberMapper;
|
||||
import com.ycwl.basic.model.aio.entity.AioDeviceBannerEntity;
|
||||
@@ -21,13 +24,19 @@ import com.ycwl.basic.service.aio.AioDeviceService;
|
||||
import com.ycwl.basic.service.mobile.GoodsService;
|
||||
import com.ycwl.basic.service.pc.FaceService;
|
||||
import com.ycwl.basic.service.pc.OrderService;
|
||||
import com.ycwl.basic.service.pc.ScenicService;
|
||||
import com.ycwl.basic.service.pc.SourceService;
|
||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||
import com.ycwl.basic.utils.ApiResponse;
|
||||
import com.ycwl.basic.utils.JwtTokenUtil;
|
||||
import com.ycwl.basic.utils.SnowFlakeUtil;
|
||||
import jakarta.servlet.ServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.Strings;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -37,8 +46,10 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@IgnoreToken
|
||||
@@ -57,6 +68,10 @@ public class AioDeviceController {
|
||||
private AioDeviceService aioDeviceService;
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
@Autowired
|
||||
private SourceService sourceService;
|
||||
|
||||
@GetMapping("/info")
|
||||
public ApiResponse<AioDeviceInfoResp> getDeviceInfo(HttpServletRequest request) {
|
||||
@@ -105,6 +120,47 @@ public class AioDeviceController {
|
||||
memberEntity.setNickname("用户");
|
||||
memberMapper.add(memberEntity);
|
||||
FaceRecognizeResp resp = faceService.faceUpload(file, aioDevice.getScenicId(), memberEntity.getId());
|
||||
// 尝试超分
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(2000L);
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
GoodsReqQuery query = new GoodsReqQuery();
|
||||
query.setSourceType(2);
|
||||
query.setFaceId(resp.getFaceId());
|
||||
List<GoodsDetailVO> sourcePhotoList = goodsService.sourceGoodsList(query);
|
||||
if (sourcePhotoList == null || sourcePhotoList.isEmpty()) {
|
||||
log.info("无源图片");
|
||||
redisTemplate.opsForValue().set("aio:faceId:"+resp.getFaceId().toString()+":pass", "1", 1, TimeUnit.DAYS);
|
||||
return;
|
||||
}
|
||||
log.info("超分开始!");
|
||||
sourcePhotoList.forEach(photo -> {
|
||||
if (StringUtils.contains(photo.getUrl(), "_q_")) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
File dstFile = new File(photo.getGoodsId()+".jpg");
|
||||
long fileSize = HttpUtil.downloadFile(photo.getUrl(), dstFile);
|
||||
log.info("超分开始:{}", fileSize);
|
||||
BceImageEnhancer enhancer = getEnhancer();
|
||||
MultipartFile enhancedFile = enhancer.enhance(dstFile.getName());
|
||||
log.info("超分结束:{}", photo.getUrl());
|
||||
String url = sourceService.uploadAndUpdateUrl(photo.getGoodsId(), enhancedFile);
|
||||
log.info("上传结束:->{}", url);
|
||||
} catch (Exception e) {
|
||||
log.error("超分失败:{}", photo.getGoodsId(), e);
|
||||
} finally {
|
||||
File _file = new File(photo.getGoodsId()+".jpg");
|
||||
if (_file.exists()) {
|
||||
_file.delete();
|
||||
}
|
||||
}
|
||||
});
|
||||
redisTemplate.opsForValue().set("aio:faceId:"+sourcePhotoList.getFirst().getFaceId().toString()+":pass", "1", 1, TimeUnit.DAYS);
|
||||
}).start();
|
||||
return ApiResponse.success(resp);
|
||||
}
|
||||
|
||||
@@ -113,6 +169,14 @@ public class AioDeviceController {
|
||||
public ApiResponse<FaceRespVO> faceInfo(@PathVariable Long faceId) {
|
||||
return faceService.getById(faceId);
|
||||
}
|
||||
@GetMapping("/face/{faceId}/check")
|
||||
public ApiResponse<Boolean> faceCheck(@PathVariable Long faceId) {
|
||||
if (redisTemplate.hasKey("aio:faceId:"+faceId.toString()+":pass")) {
|
||||
return ApiResponse.success(true);
|
||||
} else {
|
||||
return ApiResponse.success(false);
|
||||
}
|
||||
}
|
||||
// 照片商品列表
|
||||
@GetMapping("/{faceId}/photo")
|
||||
public ApiResponse<List<GoodsDetailVO>> sourceGoodsList(@PathVariable Long faceId) {
|
||||
@@ -144,4 +208,15 @@ public class AioDeviceController {
|
||||
}
|
||||
return ApiResponse.success(orderService.queryOrder(orderId));
|
||||
}
|
||||
|
||||
private BceImageEnhancer getEnhancer() {
|
||||
BceImageEnhancer enhancer = new BceImageEnhancer();
|
||||
BceEnhancerConfig config = new BceEnhancerConfig();
|
||||
config.setQps(1);
|
||||
config.setAppId("119554288");
|
||||
config.setApiKey("OX6QoijgKio3eVtA0PiUVf7f");
|
||||
config.setSecretKey("dYatXReVriPeiktTjUblhfubpcmYfuMk");
|
||||
enhancer.setConfig(config);
|
||||
return enhancer;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,4 @@
|
||||
package com.ycwl.basic.image.enhancer;
|
||||
|
||||
public class ImageEnhancerFactory {
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
package com.ycwl.basic.image.enhancer.adapter;
|
||||
|
||||
import com.baidu.aip.imageprocess.AipImageProcess;
|
||||
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
|
||||
import com.ycwl.basic.image.util.ImageUtil;
|
||||
import com.ycwl.basic.utils.ImageUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class BceImageEnhancer implements IEnhancer {
|
||||
private BceEnhancerConfig config;
|
||||
|
||||
public boolean setConfig(BceEnhancerConfig config) {
|
||||
this.config = config;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean loadConfig(Map<String, String> _config) {
|
||||
BceEnhancerConfig config = new BceEnhancerConfig();
|
||||
config.setAppId(_config.get("appId"));
|
||||
config.setApiKey(_config.get("apiKey"));
|
||||
config.setSecretKey(_config.get("secretKey"));
|
||||
config.setQps(Float.parseFloat(_config.get("qps")));
|
||||
this.config = config;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipartFile enhance(String url) {
|
||||
AipImageProcess client = getClient();
|
||||
HashMap<String, String> options = new HashMap<>();
|
||||
JSONObject jsonObject = client.imageQualityEnhance(url, options);
|
||||
return ImageUtils.base64ToMultipartFile(jsonObject.getString("image"));
|
||||
}
|
||||
|
||||
public AipImageProcess getClient() {
|
||||
AipImageProcess client = new AipImageProcess(config.getAppId(), config.getApiKey(), config.getSecretKey());
|
||||
return client;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.ycwl.basic.image.enhancer.adapter;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface IEnhancer {
|
||||
boolean loadConfig(Map<String, String> _config);
|
||||
|
||||
public MultipartFile enhance(String url);
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.ycwl.basic.image.enhancer.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class BceEnhancerConfig {
|
||||
private String appId;
|
||||
private String apiKey;
|
||||
private String secretKey;
|
||||
private float qps = 1.0f;
|
||||
}
|
@@ -21,5 +21,5 @@ public interface SourceService {
|
||||
ApiResponse<Integer> update(SourceEntity source);
|
||||
|
||||
ApiResponse cutVideo(Long id);
|
||||
ApiResponse<String> uploadAndUpdateUrl(Long id, org.springframework.web.multipart.MultipartFile file);
|
||||
String uploadAndUpdateUrl(Long id, org.springframework.web.multipart.MultipartFile file);
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package com.ycwl.basic.service.pc.impl;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.ycwl.basic.exception.BaseException;
|
||||
import com.ycwl.basic.mapper.SourceMapper;
|
||||
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
|
||||
import com.ycwl.basic.model.pc.source.req.SourceReqQuery;
|
||||
@@ -24,6 +25,8 @@ import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static com.ycwl.basic.constant.StorageConstant.PHOTO_PATH;
|
||||
|
||||
/**
|
||||
* @Author:longbinbin
|
||||
* @Date:2024/12/3 15:49
|
||||
@@ -145,14 +148,14 @@ public class SourceServiceImpl implements SourceService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiResponse<String> uploadAndUpdateUrl(Long id, MultipartFile file) {
|
||||
public String uploadAndUpdateUrl(Long id, MultipartFile file) {
|
||||
SourceRespVO source = sourceMapper.getById(id);
|
||||
if (source == null) {
|
||||
return ApiResponse.fail("该素材不存在");
|
||||
throw new BaseException("该素材不存在");
|
||||
}
|
||||
try {
|
||||
IStorageAdapter adapter = scenicService.getScenicStorageAdapter(source.getScenicId());
|
||||
String uploadedUrl = adapter.uploadFile(file, "source", String.valueOf(id));
|
||||
String uploadedUrl = adapter.uploadFile(file, PHOTO_PATH, id + "_q_.jpg");
|
||||
|
||||
SourceEntity sourceUpd = new SourceEntity();
|
||||
sourceUpd.setId(id);
|
||||
@@ -160,12 +163,12 @@ public class SourceServiceImpl implements SourceService {
|
||||
|
||||
int updateResult = sourceMapper.update(sourceUpd);
|
||||
if (updateResult > 0) {
|
||||
return ApiResponse.success(uploadedUrl);
|
||||
return uploadedUrl;
|
||||
} else {
|
||||
return ApiResponse.fail("更新URL失败");
|
||||
throw new BaseException("更新URL失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return ApiResponse.fail("文件上传失败: " + e.getMessage());
|
||||
throw new BaseException("文件上传失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package com.ycwl.basic.image.enhancer.adapter;
|
||||
|
||||
import com.ycwl.basic.image.enhancer.entity.BceEnhancerConfig;
|
||||
import org.junit.Test;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class BceImageEnhancerTest {
|
||||
private BceImageEnhancer getEnhancer() {
|
||||
BceImageEnhancer enhancer = new BceImageEnhancer();
|
||||
BceEnhancerConfig config = new BceEnhancerConfig();
|
||||
config.setQps(1);
|
||||
config.setAppId("119554288");
|
||||
config.setApiKey("OX6QoijgKio3eVtA0PiUVf7f");
|
||||
config.setSecretKey("dYatXReVriPeiktTjUblhfubpcmYfuMk");
|
||||
enhancer.setConfig(config);
|
||||
return enhancer;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enhance() throws IOException {
|
||||
BceImageEnhancer enhancer = getEnhancer();
|
||||
MultipartFile file = enhancer.enhance("8b84a9f5-44fc-4f7e-8550-710727c47070.jpg");
|
||||
file.transferTo(new File("out.jpg"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user