-
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 #108 from TOVBUS/KAN-324
AzureInbody Model, ViewModel 추가 및 동영상 url 추가
- Loading branch information
Showing
7 changed files
with
185 additions
and
3 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
29 changes: 29 additions & 0 deletions
29
Frontend-iOS/FebirdApp/FebirdApp/Sources/Onboarding/Models/AzureInbody.swift
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 @@ | ||
// | ||
// AzureInbody.swift | ||
// FebirdApp | ||
// | ||
// Created by 이유경 on 8/8/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct AzureInbody: Codable { | ||
let weight: String | ||
let bmr: String | ||
let bmi: String | ||
let bodyFat: String | ||
let inbodyDate: String | ||
} | ||
|
||
struct Documents: Codable { | ||
let fields: AzureInbody | ||
} | ||
|
||
struct AnalyzeResult: Codable { | ||
let documents: [Documents] | ||
} | ||
|
||
struct InbodyRoot: Codable { | ||
let status: String | ||
let analyzeResult: AnalyzeResult? | ||
} |
57 changes: 57 additions & 0 deletions
57
Frontend-iOS/FebirdApp/FebirdApp/Sources/Onboarding/ViewModels/AzureInbodyService.swift
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,57 @@ | ||
// | ||
// AzureInbodyNetService.swift | ||
// FebirdApp | ||
// | ||
// Created by 이유경 on 8/8/24. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
class AzureInbodyService: ObservableObject { | ||
private var subscriptionKey: String = "9ac5258525594353a9852e94af9f2ab6" | ||
|
||
// MARK: - 이미지 분석 | ||
func analyzeImage(imageData: Data, completion: @escaping (Result<String, Error>) -> Void) { | ||
let url = "https://doc-feo.cognitiveservices.azure.com/formrecognizer/documentModels/feo-inbody-model2:analyze?api-version=2023-07-31" | ||
let headers: HTTPHeaders = [ | ||
"Ocp-Apim-Subscription-Key": subscriptionKey, | ||
"Content-Type": "application/octet-stream" | ||
] | ||
|
||
AF.upload(imageData, to: url, headers: headers) | ||
.response { response in | ||
debugPrint(response) | ||
|
||
if let httpResponse = response.response, | ||
let operationLocation = httpResponse.headers["Operation-Location"] { | ||
completion(.success(operationLocation)) | ||
} else { | ||
print("network af.upload 메서드에서 문제발생! analyzeImage 메서드임") | ||
completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Operation-Location header not found."]))) | ||
} | ||
} | ||
} | ||
// MARK: - 이미지 분석 결과 | ||
func getAnalyzeResult(operationLocation: String, completion: @escaping (Result<InbodyRoot, Error>) -> Void) { | ||
let headers: HTTPHeaders = [ | ||
"Ocp-Apim-Subscription-Key": subscriptionKey | ||
] | ||
|
||
AF.request(operationLocation, headers: headers) | ||
.responseDecodable(of: InbodyRoot.self) { response in | ||
debugPrint(response) | ||
|
||
switch response.result { | ||
case .success(let value): | ||
completion(.success(value)) | ||
case .failure(let error): | ||
print("network af.request 메서드에서 문제발생! getAnalyzeResult 메서드임") | ||
if let data = response.data, let str = String(data: data, encoding: .utf8) { | ||
print("Response data: \(str)") | ||
} | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
Frontend-iOS/FebirdApp/FebirdApp/Sources/Onboarding/ViewModels/AzureInbodyViewModel.swift
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,62 @@ | ||
// | ||
// AzureInbodyViewModel.swift | ||
// FebirdApp | ||
// | ||
// Created by 이유경 on 8/8/24. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
class AzureInbodyViewModel: ObservableObject { | ||
@Published var result: String = "" | ||
private let service = AzureInbodyService() | ||
|
||
// MARK: - 이미지 분석 | ||
func analyzeImage(imageData: Data) { | ||
service.analyzeImage(imageData: imageData) { [weak self] result in | ||
switch result { | ||
case .success(let operationLocation): | ||
print("Operation Location: \(operationLocation)") // 디버깅 출력 추가 | ||
self?.pollAnalyzeResult(operationLocation: operationLocation) | ||
|
||
case .failure(let error): | ||
DispatchQueue.main.async { | ||
self?.result = "Error: \(error.localizedDescription)" | ||
print("Error: \(error.localizedDescription)") // 디버깅 출력 추가 | ||
} | ||
} | ||
} | ||
} | ||
// MARK: - 이미지 저장 | ||
private func pollAnalyzeResult(operationLocation: String) { | ||
service.getAnalyzeResult(operationLocation: operationLocation) { [weak self] result in | ||
switch result { | ||
case .success(let analyzeResult): | ||
if analyzeResult.status == "succeeded" { | ||
DispatchQueue.main.async { | ||
if let jsonData = try? JSONEncoder().encode(analyzeResult), | ||
let jsonString = String(data: jsonData, encoding: .utf8) { | ||
self?.result = jsonString | ||
} | ||
} | ||
} else if analyzeResult.status == "running" { | ||
// 분석이 완료되지 않은 경우 일정 시간 후 다시 요청 | ||
DispatchQueue.global().asyncAfter(deadline: .now() + 3) { | ||
self?.pollAnalyzeResult(operationLocation: operationLocation) | ||
} | ||
} else { | ||
DispatchQueue.main.async { | ||
self?.result = "Analysis status: \(analyzeResult.status)" | ||
} | ||
print("Analysis status: \(analyzeResult.status)") | ||
} | ||
case .failure(let error): | ||
DispatchQueue.main.async { | ||
self?.result = "Error: \(error.localizedDescription)" | ||
} | ||
print("Error: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} |
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