Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop back search image #81

Merged
merged 4 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'com.google.cloud:google-cloud-aiplatform:0.4.0'
implementation 'com.google.code.gson:gson:2.10.1'

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.example.exitmedserver.search.dto.SearchAddFavoriteRequestDto;
import com.example.exitmedserver.search.dto.SearchAddFavoriteResponseDto;
import com.example.exitmedserver.search.dto.SearchGetImageSearchResponseDto;
import com.example.exitmedserver.search.service.SearchService;
import com.example.exitmedserver.user.dto.SearchGetFavoriteResponse;
import com.example.exitmedserver.user.dto.SearchGetSearchListResponse;
import com.example.exitmedserver.user.dto.SearchTextResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

Expand All @@ -27,6 +29,15 @@ public SearchAddFavoriteResponseDto addToFavorite(@RequestHeader("Authorization"
return searchService.addToFavorite(jwtToken, searchAddFavoriteRequestDto.getPillItemSequence());
}

@PostMapping("/auth/search/image-search")
public SearchGetImageSearchResponseDto imageEncode(@RequestHeader("Authorization") String jwtToken, @RequestParam("image") MultipartFile image) throws IOException {
try {
return searchService.predictImageClassification("690976576624", image, "5170539191523606528");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@GetMapping("/auth/search/favorite")
public SearchGetFavoriteResponse getFavorite(@RequestHeader("Authorization") String jwtToken) {
return searchService.getFavorite(jwtToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.exitmedserver.search.dto;

import lombok.Data;

@Data
public class SearchGetImageSearchResponseDto {
private Long pillItemSequence;
private String imageLink;
private String pillName;
private String shape;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.example.exitmedserver.search.service;

import com.example.exitmedserver.pill.entity.Pill;
import com.example.exitmedserver.pill.entity.PillImage;
import com.example.exitmedserver.pill.repository.PillImageRepository;
import com.example.exitmedserver.pill.repository.PillRepository;
import com.example.exitmedserver.search.dto.SearchAddFavoriteResponseDto;
import com.example.exitmedserver.search.dto.SearchGetFavoriteResponseDto;
import com.example.exitmedserver.search.dto.SearchGetSearchListResponseDto;
import com.example.exitmedserver.search.dto.SearchTextResponseDto;
import com.example.exitmedserver.search.dto.*;
import com.example.exitmedserver.search.entity.FavoriteList;
import com.example.exitmedserver.search.entity.SearchHistoryList;
import com.example.exitmedserver.search.repository.FavoriteListRepository;
Expand All @@ -16,11 +14,23 @@
import com.example.exitmedserver.user.dto.SearchTextResponse;
import com.example.exitmedserver.util.auth.JwtProvider;
import com.example.exitmedserver.util.file.FileProvider;
import com.google.cloud.aiplatform.util.ValueConverter;
import com.google.cloud.aiplatform.v1.EndpointName;
import com.google.cloud.aiplatform.v1.PredictResponse;
import com.google.cloud.aiplatform.v1.PredictionServiceClient;
import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
import com.google.cloud.aiplatform.v1.schema.predict.instance.ImageClassificationPredictionInstance;
import com.google.cloud.aiplatform.v1.schema.predict.params.ImageClassificationPredictionParams;
import com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult;
import com.google.protobuf.Value;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.sql.Timestamp;
import java.util.ArrayList;
Expand Down Expand Up @@ -165,4 +175,78 @@ public byte[] getImage(String pillItemSequence) throws IOException {
FileProvider fileProvider = new FileProvider();
return Files.readAllBytes(new File(fileProvider.getServerDisplayPath() + "/" + pillItemSequence + ".jpg").toPath());
}

public SearchGetImageSearchResponseDto predictImageClassification(String project, MultipartFile fileName, String endpointId)
throws IOException {
PredictionServiceSettings settings =
PredictionServiceSettings.newBuilder()
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
.build();

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(settings)) {
String location = "us-central1";
EndpointName endpointName = EndpointName.of(project, location, endpointId);

byte[] contents = java.util.Base64.getEncoder().encode(fileName.getBytes());

String content = new String(contents, StandardCharsets.UTF_8);

ImageClassificationPredictionInstance predictionInstance =
ImageClassificationPredictionInstance.newBuilder().setContent(content).build();

List<Value> instances = new ArrayList<>();
instances.add(ValueConverter.toValue(predictionInstance));

ImageClassificationPredictionParams predictionParams =
ImageClassificationPredictionParams.newBuilder()
.setConfidenceThreshold((float) 0.01)
.setMaxPredictions(5)
.build();

PredictResponse predictResponse =
predictionServiceClient.predict(
endpointName, instances, ValueConverter.toValue(predictionParams));
System.out.println("Predict Image Classification Response");
System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());

SearchGetImageSearchResponseDto searchGetImageSearchResponseDto = new SearchGetImageSearchResponseDto();

System.out.println(predictResponse);

System.out.println("Predictions");
for (Value prediction : predictResponse.getPredictionsList()) {

ClassificationPredictionResult.Builder resultBuilder =
ClassificationPredictionResult.newBuilder();
// Display names and confidences values correspond to
// IDs in the ID list.
ClassificationPredictionResult result =
(ClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
int counter = 0;
for (Long id : result.getIdsList()) {
Long itemSequence = Long.parseLong(result.getDisplayNames(counter));

searchGetImageSearchResponseDto.setPillItemSequence(itemSequence);
searchGetImageSearchResponseDto.setPillName(pillRepository.findPillByPillItemSequence(itemSequence).getPillName());

PillImage pillImage = pillImageRepository.findByPillItemSequence(itemSequence);

searchGetImageSearchResponseDto.setImageLink(pillImage.getImageLink());
searchGetImageSearchResponseDto.setShape(pillImage.getShape());

System.out.printf("Label ID: %d\n", id);
System.out.printf("Label: %s\n", result.getDisplayNames(counter));
System.out.println(Integer.parseInt(result.getDisplayNames(counter)));
System.out.printf("Confidence: %.4f\n", result.getConfidences(counter));
counter++;
}
}

return searchGetImageSearchResponseDto;
}
}
}