-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,7 @@ out/ | |
### VS Code ### | ||
.vscode/ | ||
|
||
.DS_Store | ||
.DS_Store | ||
|
||
### ADC ### | ||
secret-files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/snowmanvillage/server/controller/PhotoAnalyzeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/snowmanvillage/server/service/GoogleVisionAiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters