Skip to content

Commit

Permalink
feat: add pointer api
Browse files Browse the repository at this point in the history
  • Loading branch information
shinsj4653 committed Feb 17, 2024
1 parent efb9f84 commit 183e91d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/gdsc/pointer/controller/ImageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import gdsc.pointer.domain.User;
import gdsc.pointer.dto.request.image.ImageUploadDto;
import gdsc.pointer.dto.request.image.PointerDto;
import gdsc.pointer.dto.response.ResultDto;
import gdsc.pointer.dto.response.image.PointerResponseDto;
import gdsc.pointer.dto.response.image.PolyResponseDto;
import gdsc.pointer.service.ImageService;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

Expand Down Expand Up @@ -42,4 +42,10 @@ public ResponseEntity<?> boundingPoly(ImageUploadDto dto) throws Exception {
PolyResponseDto result = imageService.getImageTextBoundingPoly(dto.getImage());
return ResponseEntity.ok(ResultDto.res(HttpStatus.OK, "이미지 텍스트 좌표 값 반환", result));
}

@PostMapping("pointer")
public ResponseEntity<?> pointer(PointerDto dto) throws Exception {
PointerResponseDto result = imageService.getWordsWithPointer(dto);
return ResponseEntity.ok(ResultDto.res(HttpStatus.OK, "손가락 좌표에 해당하는 단어 반환", result));
}
}
14 changes: 14 additions & 0 deletions src/main/java/gdsc/pointer/dto/request/image/PointerAIDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gdsc.pointer.dto.request.image;

import lombok.*;
import org.springframework.web.multipart.MultipartFile;

@Getter
@Setter
@NoArgsConstructor()
@AllArgsConstructor
public class PointerAIDto {
private String imageUrl;
private int x;
private int y;
}
17 changes: 17 additions & 0 deletions src/main/java/gdsc/pointer/dto/request/image/PointerDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gdsc.pointer.dto.request.image;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.attoparser.dom.Text;
import org.springframework.web.multipart.MultipartFile;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class PointerDto {
private MultipartFile file;
private int x;
private int y;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gdsc.pointer.dto.response.image;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class PointerResponseDto {
private List<String> words;
}
43 changes: 43 additions & 0 deletions src/main/java/gdsc/pointer/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.google.cloud.storage.*;
import com.google.cloud.vision.v1.*;
import gdsc.pointer.dto.request.image.ImageUrlDto;
import gdsc.pointer.dto.request.image.PointerAIDto;
import gdsc.pointer.dto.request.image.PointerDto;
import gdsc.pointer.dto.response.image.PointerResponseDto;
import gdsc.pointer.dto.response.image.PolyResponseDto;
import io.grpc.netty.shaded.io.netty.handler.codec.http.HttpUtil;
import jakarta.transaction.Transactional;
Expand All @@ -14,7 +17,9 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gcp.vision.CloudVisionTemplate;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -114,6 +119,44 @@ public PolyResponseDto getImageTextBoundingPoly(MultipartFile file) throws IOExc
return responseDto.getBody();
}

public PointerResponseDto getWordsWithPointer(PointerDto dto) throws IOException {

// GCS 사진 업로드 후, 공개 이미지 url 반환
String image_url = uploadImage(dto.getFile());

//String image_url = dto.getImageUrl();
int x = dto.getX();
int y = dto.getY();
ResponseEntity<PointerResponseDto> response = postPointer(image_url, x, y);
return response.getBody();
}

private ResponseEntity<PointerResponseDto> postPointer(String url, int x, int y) {

URI uri = UriComponentsBuilder
.fromUriString(aiServerUrl)
.path("/image/pointer")
.encode()
.build()
.toUri();

PointerAIDto pointerAIDto = new PointerAIDto();
pointerAIDto.setImageUrl(url);
pointerAIDto.setX(x);
pointerAIDto.setY(y);


RestTemplate restTemplate = new RestTemplate();

ResponseEntity<PointerResponseDto> responseEntity = restTemplate.postForEntity(
uri, pointerAIDto, PointerResponseDto.class
);
System.out.println(responseEntity);

return responseEntity;

}

private ResponseEntity<PolyResponseDto> postImageBoundingPoly(String url) {
URI uri = UriComponentsBuilder
.fromUriString(aiServerUrl)
Expand Down

0 comments on commit 183e91d

Please sign in to comment.