원래 SwiftUI 프로젝트에(UIKit도 똑같을듯) .mlmodel 파일을 추가하면 알아서 해당 모델의 클래스를 만들어주고, 해당 클래스의 인스턴스를 만들어서 사용할 수 있습니다.
그런데 WWDC 스스챌 때문에 플레이그라운드 앱으로 프로젝트를 생성하고(.swiftpm) 모델 파일을 추가해보면 모델의 클래스를 만들어주지 않습니다;;;;
때문에 패키지 매니저가 해당 모델을 찾아서 불러올 수 있도록(?) 설정을 해주어야 합니다.
큰 흐름은 다음과 같습니다.
💡 .mlmodel 파일 추가 → 모델 파일 컴파일 → 패키지 매니저에 타겟 추가 → 불러오기
1 . 먼저 프로젝트 디렉토리에 Resources 라는 이름의 디렉토리를 직접!! 만들어줍니다.

2. 만들어준 Resources 디렉토리의 안에 .mlmodel 파일을 추가해줍니다.

3. 그 다음 터미널을 켜고, 해당 프로젝트 내부의 .mlmodel 파일이 있는 경로(Resources 폴더)로 이동해준 뒤, 아래 명령어를 갈겨줍니다.
- 아래 명령어는 모델 이름의 swift 클래스 파일을 생성해줍니다.
$ xcrun coremlcompiler generate 모델.mlmodel . --language Swift

4. 생성된 모델 클래스 파일(.swift)을 Resoueces 폴더의 바깥으로 빼줍니다.

5. (여전히 터미널) 프로젝트 루트 디렉토리로 가면 Package.swift 파일이 보입니다. 이놈 Xcode에서는 못봐서 개짱납니다(바로 보는 방법 아는 분 계시면 알려주십쇼). vi편집기로 이놈을 조금 수정해줍시다.

$ vi Package.swift
- 어떤가요. 작업하기 싫게 생겼죠?

- 꿀팁) 일부러 아무거나 하나 지우고 오류를 내면 Xcode에서 Package.swif가 나타납니다.

- 오류를 클릭하면 Xcode 상에서 편집이 가능합니다. HAHA

- 참고로 Package.swift 안에는 앱의 기본 설정들이 들어 있습니다. 가로모드 지원, 앱 아이콘 설정 등을 할 수 있어 보입니다.
6. Package.swift의 아래쪽 targets: 안에 다음과 같이 우리가 만들어준 Resources 폴더에 대한 경로를 추가해줍니다.
targets: [
.executableTarget(
name: "AppModule",
path: ".",
resources: [
.process("Resources")
]
)
]

7. 이제 모델이 잘 불러와지는지 테스트를 해봅시다.

이 문제를 해결해주신 퍼피에게 감사를 드립니다… 샤라웃 투 퍼피
참고 포스팅들
https://levelup.gitconnected.com/how-to-include-a-core-ml-model-in-a-swift-package-88c44c1e1403
How to include a Core ML model in a Swift Package
Xcode also you to easily include a Core ML model into your project. But not into a Swift Package. Here is how to do it.
levelup.gitconnected.com
https://developer.apple.com/forums/thread/684283
CoreML Model in Swift Package Tests | Apple Developer Forums
No idea why this is happening, but the issue was that I had the mlmodelc file in the resources folder. Despite adding the resources in the package.swift, the Bundle.module property was not available because the generated resource_bundle_accessor.swift file
developer.apple.com
https://stackoverflow.com/questions/64379775/how-to-add-a-coreml-model-into-a-swift-package
How to add a CoreML model into a Swift package?
I'm trying to write a Swift package that uses a CoreML model. I'm not very familiar with Swift packages creation and I could not make it work. Here is what I've done based on the different posts I've
stackoverflow.com
'iOS' 카테고리의 다른 글
[ iOS ] Watch Connectivity 사용해보기 (0) | 2023.08.08 |
---|---|
[ iOS ] Core Motion 사용해보기 (0) | 2023.08.08 |
[ iOS ] Xcode 프로젝트 기본 생성 파일 설명 (14.2 버전 기준, UIKit) (0) | 2023.03.06 |
[ iOS ] Xcode로 iOS 개발 시 API KEY 숨기기 (0) | 2023.03.01 |
[ iOS ] 코코아팟 설치 후 라이브러리 추가하기 (0) | 2023.02.28 |