-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from BbeumbungE/dev
Deploy: v2 배포
- Loading branch information
Showing
72 changed files
with
952 additions
and
195 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
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
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
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
72 changes: 72 additions & 0 deletions
72
src/main/java/com/siliconvalley/domain/google/service/GoogleVisionApiService.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,72 @@ | ||
package com.siliconvalley.domain.google.service; | ||
|
||
import com.google.api.gax.core.FixedCredentialsProvider; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.vision.v1.*; | ||
import com.google.cloud.vision.v1.ImageAnnotatorClient; | ||
import com.google.cloud.vision.v1.ImageAnnotatorSettings; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
@Slf4j | ||
public class GoogleVisionApiService { | ||
|
||
@Value("${google.service.account.key}") | ||
private String serviceAccountKey; | ||
|
||
public Map<String, Float> detectObjects(String filePath) { | ||
List<AnnotateImageRequest> requests = new ArrayList<>(); | ||
Map<String, Float> results = new HashMap<>(); | ||
|
||
try { | ||
ImageSource imageSource = ImageSource.newBuilder().setImageUri(filePath).build(); | ||
Image img = Image.newBuilder().setSource(imageSource).build(); | ||
Feature feat = Feature.newBuilder().setType(Feature.Type.OBJECT_LOCALIZATION).build(); | ||
AnnotateImageRequest request = AnnotateImageRequest.newBuilder() | ||
.addFeatures(feat) | ||
.setImage(img) | ||
.build(); | ||
requests.add(request); | ||
|
||
GoogleCredentials credentials = GoogleCredentials.fromStream( | ||
new ByteArrayInputStream(serviceAccountKey.getBytes(StandardCharsets.UTF_8)) | ||
); | ||
ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder() | ||
.setCredentialsProvider(FixedCredentialsProvider.create(credentials)) | ||
.build(); | ||
|
||
try (ImageAnnotatorClient client = ImageAnnotatorClient.create(settings)) { | ||
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); | ||
List<AnnotateImageResponse> responses = response.getResponsesList(); | ||
|
||
for (AnnotateImageResponse res : responses) { | ||
if (res.hasError()) { | ||
results.put("Error", -1f); | ||
log.info("Error occurred: " + res.getError().getMessage()); | ||
continue; | ||
} | ||
|
||
for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) { | ||
log.info("Detected object: " + entity.getName() + " with confidence: " + entity.getScore()); | ||
results.put(entity.getName().toLowerCase(), entity.getScore()); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
results.put("Error", -1f); | ||
log.error("An IOException occurred: ", e); | ||
} | ||
|
||
return results; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/siliconvalley/domain/google/service/VisionDetectingService.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,29 @@ | ||
package com.siliconvalley.domain.google.service; | ||
|
||
import com.siliconvalley.domain.canvas.dao.CanvasFindDao; | ||
import com.siliconvalley.domain.canvas.domain.Canvas; | ||
import com.siliconvalley.domain.stage.domain.Score; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Map; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
@Slf4j | ||
public class VisionDetectingService { | ||
|
||
private final GoogleVisionApiService visionService; | ||
private final CanvasFindDao canvasFindDao; | ||
|
||
public Score calculateCanvasScore(Long canvasId) { | ||
Canvas canvas = canvasFindDao.findById(canvasId); | ||
Map<String, Float> detectResult = visionService.detectObjects(canvas.getCanvas()); | ||
Float detectionScore = detectResult.get(canvas.getSubject().getPix2Pix().getVisionName()); | ||
|
||
return Score.determineScore(detectionScore); | ||
} | ||
} |
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
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
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
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
22 changes: 14 additions & 8 deletions
22
src/main/java/com/siliconvalley/domain/item/subject/api/SubjectApi.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 |
---|---|---|
@@ -1,25 +1,31 @@ | ||
package com.siliconvalley.domain.item.subject.api; | ||
|
||
import com.siliconvalley.domain.stage.application.StageUpdateService; | ||
import com.siliconvalley.domain.image.service.S3ImageUploadService; | ||
import com.siliconvalley.domain.image.service.S3PathBuildService; | ||
import com.siliconvalley.domain.item.subject.application.SketchCreateService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.validation.Valid; | ||
import java.io.IOException; | ||
|
||
@RestController | ||
@RequestMapping("/api/subjects") | ||
@RequiredArgsConstructor | ||
public class SubjectApi { | ||
|
||
private final StageUpdateService stageUpdateService; | ||
private final S3ImageUploadService s3ImageUploadService; | ||
private final S3PathBuildService s3PathBuildService; | ||
private final SketchCreateService sketchCreateService; | ||
|
||
@PatchMapping("/{subjectId}/stages/{stageId}") | ||
public ResponseEntity setSubjectToStage( | ||
@PostMapping("/{subjectId}/sketches") | ||
public ResponseEntity addSketchToSubject( | ||
@PathVariable(name = "subjectId") Long subjectId, | ||
@PathVariable(name = "stageId") Long stageId | ||
) { | ||
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(stageUpdateService.updateStage(subjectId, stageId)); | ||
@RequestParam(name = "sketchImage") MultipartFile sketchImage | ||
) throws IOException { | ||
String sketchImageUrl = s3ImageUploadService.uploadFile(sketchImage, s3PathBuildService.buildPathForItem("sketch")); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(sketchCreateService.createSketch(subjectId, sketchImageUrl)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/siliconvalley/domain/item/subject/application/SketchCreateService.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,28 @@ | ||
package com.siliconvalley.domain.item.subject.application; | ||
|
||
import com.siliconvalley.domain.item.subject.code.SketchCode; | ||
import com.siliconvalley.domain.item.subject.dao.SketchRepository; | ||
import com.siliconvalley.domain.item.subject.dao.SubjectFindDao; | ||
import com.siliconvalley.domain.item.subject.domain.Sketch; | ||
import com.siliconvalley.domain.item.subject.domain.Subject; | ||
import com.siliconvalley.domain.item.subject.dto.SketchPostSuccessResponse; | ||
import com.siliconvalley.global.common.dto.Response; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class SketchCreateService { | ||
|
||
private final SubjectFindDao subjectFindDao; | ||
private final SketchRepository sketchRepository; | ||
|
||
public Response createSketch(Long subjectId, String sketchImageUrl) { | ||
Subject subject = subjectFindDao.findById(subjectId); | ||
Sketch sketch = Sketch.toEntity(sketchImageUrl, subject); | ||
sketchRepository.save(sketch); | ||
return Response.of(SketchCode.CREATE_SUCCESS, new SketchPostSuccessResponse(sketch)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/siliconvalley/domain/item/subject/code/SketchCode.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,35 @@ | ||
package com.siliconvalley.domain.item.subject.code; | ||
|
||
import com.siliconvalley.global.common.code.ResponseCode; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public enum SketchCode implements ResponseCode { | ||
|
||
CREATE_SUCCESS(201, "스케치 생성에 성공했습니다.", HttpStatus.CREATED); | ||
|
||
private int code; | ||
private String message; | ||
|
||
private HttpStatus httpStatus; | ||
|
||
SketchCode (int code, String message, HttpStatus httpStatus) { | ||
this.code = code; | ||
this.message = message; | ||
this.httpStatus = httpStatus; | ||
} | ||
|
||
@Override | ||
public int getCode() { | ||
return this.code; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return this.message; | ||
} | ||
|
||
@Override | ||
public HttpStatus getHttpStatus() { | ||
return this.httpStatus; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/siliconvalley/domain/item/subject/dao/SketchRepository.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,7 @@ | ||
package com.siliconvalley.domain.item.subject.dao; | ||
|
||
import com.siliconvalley.domain.item.subject.domain.Sketch; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface SketchRepository extends JpaRepository<Sketch, Long> { | ||
} |
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
Oops, something went wrong.