Skip to content

Commit

Permalink
merge: 이미지 Ocr 연결 구현
Browse files Browse the repository at this point in the history
Feature/#112 이미지 ocr
  • Loading branch information
hong-sile authored Aug 30, 2024
2 parents f5c0d41 + 97be898 commit ad8fb81
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/main/java/play/pluv/config/RestClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import play.pluv.oauth.apple.AppleApiClient;
import play.pluv.oauth.google.GoogleApiClient;
import play.pluv.oauth.spotify.SpotifyApiClient;
import play.pluv.playlist.infra.OcrApiClient;

@Configuration
public class RestClientConfig {
Expand Down Expand Up @@ -42,6 +43,11 @@ public GoogleApiClient googleApiClient() {
return createHttpInterface(GoogleApiClient.class);
}

@Bean
public OcrApiClient ocrApiClient() {
return createHttpInterface(OcrApiClient.class);
}

private <T> T createHttpInterface(final Class<T> clazz) {
Builder builder = RestClient.builder();
if (logActive) {
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/play/pluv/playlist/application/PlayListService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import play.pluv.playlist.domain.MusicStreaming;
import play.pluv.playlist.domain.PlayList;
import play.pluv.playlist.domain.PlayListMusic;
import play.pluv.playlist.infra.OcrReader;

@Service
@RequiredArgsConstructor
public class PlayListService {

private final PlayListConnectorComposite playListConnectorComposite;
private final OcrReader ocrReader;

public List<PlayList> getPlayLists(final String accessToken, final MusicStreaming source) {
return playListConnectorComposite.getPlayList(source, accessToken);
Expand All @@ -24,15 +26,6 @@ public List<PlayListMusic> getPlayListMusics(
}

public List<PlayListMusic> getOcrPlayListMusics(final List<String> base64EncodedImages) {
return List.of(
new PlayListMusic(
"좋은 날", List.of("아이유"), null,
"https://i.scdn.co/image/ab67616d00001e0215cf3110f19687b1a24943d1"
),
new PlayListMusic(
"ㅈㅣㅂ", List.of("한로로"), null,
"https://i.scdn.co/image/ab67616d00001e0215cf3110f19687b1a22314"
)
);
return ocrReader.ocrImages(base64EncodedImages);
}
}
13 changes: 13 additions & 0 deletions src/main/java/play/pluv/playlist/infra/OcrApiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package play.pluv.playlist.infra;

import java.util.List;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.PostExchange;
import play.pluv.playlist.infra.dto.OcrMusicRequest;
import play.pluv.playlist.infra.dto.OcrMusicResponse;

public interface OcrApiClient {

@PostExchange("http://192.168.0.22:8000/ocr")
List<OcrMusicResponse> ocrPlayListImage(@RequestBody final OcrMusicRequest request);
}
22 changes: 22 additions & 0 deletions src/main/java/play/pluv/playlist/infra/OcrReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package play.pluv.playlist.infra;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import play.pluv.playlist.domain.PlayListMusic;
import play.pluv.playlist.infra.dto.OcrMusicRequest;
import play.pluv.playlist.infra.dto.OcrMusicResponse;

@Component
@RequiredArgsConstructor
public class OcrReader {

private final OcrApiClient ocrApiClient;

public List<PlayListMusic> ocrImages(final List<String> base64EncodedImages) {
final OcrMusicRequest request = OcrMusicRequest.from(base64EncodedImages);
return ocrApiClient.ocrPlayListImage(request).stream()
.map(OcrMusicResponse::toPlayListMusic)
.toList();
}
}
20 changes: 20 additions & 0 deletions src/main/java/play/pluv/playlist/infra/dto/OcrMusicRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package play.pluv.playlist.infra.dto;

import java.util.List;

public record OcrMusicRequest(
List<Base64EncodedImage> images
) {

public static OcrMusicRequest from(final List<String> base64EncodedImages) {
return new OcrMusicRequest(
base64EncodedImages.stream()
.map(Base64EncodedImage::new)
.toList()
);
}

private record Base64EncodedImage(String base64EncodedImage) {

}
}
13 changes: 13 additions & 0 deletions src/main/java/play/pluv/playlist/infra/dto/OcrMusicResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package play.pluv.playlist.infra.dto;

import java.util.List;
import play.pluv.playlist.domain.PlayListMusic;

public record OcrMusicResponse(
String artistNames, String songTitle
) {

public PlayListMusic toPlayListMusic() {
return new PlayListMusic(songTitle, List.of(artistNames), null, "");
}
}

0 comments on commit ad8fb81

Please sign in to comment.