Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
khoon9 committed Dec 18, 2023
2 parents 383c197 + bd6918e commit 540cdf7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ out/
### VS Code ###
.vscode/

.DS_Store
.DS_Store

### ADC ###
secret-files
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// google cloud vision api
implementation 'com.google.cloud:spring-cloud-gcp-starter-vision:4.9.0'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.snowmanvillage.server.controller;

import com.snowmanvillage.server.service.GoogleVisionAiService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
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.multipart.MultipartFile;

import java.io.IOException;

@RequiredArgsConstructor
@RequestMapping("")
@RestController
public class PhotoAnalyzeController {

private final GoogleVisionAiService googleVisionAiService;

@PostMapping("/photo/analyze")
public ResponseEntity<Boolean> postPhotoAnalyze(@RequestParam("image") MultipartFile file) throws IOException {
return ResponseEntity.ok(googleVisionAiService.postPhotoAnalyze(file));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.snowmanvillage.server.service;

import com.google.cloud.vision.v1.*;
import com.google.protobuf.ByteString;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

@Service
public class GoogleVisionAiService {

public Boolean postPhotoAnalyze(MultipartFile file) throws IOException {
ByteString imgBytes = ByteString.readFrom(file.getInputStream());

Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();

try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
AnnotateImageResponse response = vision.batchAnnotateImages(Arrays.asList(request))
.getResponses(0);
if (response.hasError()) {
System.err.println("Error: " + response.getError().getMessage());
return false;
}

for (EntityAnnotation annotation : response.getLabelAnnotationsList()) {
if (annotation.getDescription().toLowerCase().contains("snowman") &&
annotation.getScore() >= 0.7) {
return true;
}
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
debug: false
logging:
level:
com.platform.match: debug
com.snowmanvillage.server: debug
org.springframework.web.servlet: debug
org.hibernate.type.descriptor.sql.BasicBinder: trace
spring:
Expand Down

0 comments on commit 540cdf7

Please sign in to comment.