Bạn có thể sử dụng ML Kit để nhận dạng các địa danh nổi tiếng trong hình ảnh.
Trước khi bắt đầu
- Nếu bạn chưa thêm Firebase vào ứng dụng của mình, hãy làm như vậy bằng cách làm theo các bước trong hướng dẫn bắt đầu .
- Bao gồm các thư viện ML Kit trong Podfile của bạn:
pod 'Firebase/MLVision', '6.25.0'
Sau khi bạn cài đặt hoặc cập nhật Pod của dự án, hãy nhớ mở dự án Xcode của bạn bằng cách sử dụng.xcworkspace
. - Trong ứng dụng của bạn, hãy nhập Firebase:
Nhanh
import Firebase
Mục tiêu-C
@import Firebase;
Nếu bạn chưa kích hoạt API dựa trên đám mây cho dự án của mình, hãy thực hiện ngay bây giờ:
- Mở trang API ML Kit của bảng điều khiển Firebase.
Nếu bạn chưa nâng cấp dự án của mình lên gói định giá Blaze, hãy nhấp vào Nâng cấp để thực hiện. (Bạn sẽ chỉ được nhắc nâng cấp nếu dự án của bạn không nằm trong gói Blaze.)
Chỉ các dự án cấp Blaze mới có thể sử dụng API dựa trên Đám mây.
- Nếu API dựa trên đám mây chưa được bật, hãy nhấp vào Bật API dựa trên đám mây .
Định cấu hình trình phát hiện mốc
Theo mặc định, Trình phát hiện đám mây sử dụng phiên bản ổn định của mô hình và trả về tối đa 10 kết quả. Nếu bạn muốn thay đổi một trong hai cài đặt này, hãy chỉ định chúng bằng đối tượng VisionCloudDetectorOptions
như trong ví dụ sau:
Nhanh
let options = VisionCloudDetectorOptions() options.modelType = .latest options.maxResults = 20
Mục tiêu-C
FIRVisionCloudDetectorOptions *options = [[FIRVisionCloudDetectorOptions alloc] init]; options.modelType = FIRVisionCloudModelTypeLatest; options.maxResults = 20;
Ở bước tiếp theo, chuyển đối tượng VisionCloudDetectorOptions
khi bạn tạo đối tượng Cloud detector.
Chạy trình dò mốc
Để nhận dạng các mốc trong hình ảnh, hãy chuyển hình ảnh dưới dạngUIImage
hoặc CMSampleBufferRef
tới phương thức detect(in:)
của VisionCloudLandmarkDetector
:- Lấy một phiên bản của
VisionCloudLandmarkDetector
:Nhanh
lazy var vision = Vision.vision() let cloudDetector = vision.cloudLandmarkDetector(options: options) // Or, to use the default settings: // let cloudDetector = vision.cloudLandmarkDetector()
Mục tiêu-C
FIRVision *vision = [FIRVision vision]; FIRVisionCloudLandmarkDetector *landmarkDetector = [vision cloudLandmarkDetector]; // Or, to change the default settings: // FIRVisionCloudLandmarkDetector *landmarkDetector = // [vision cloudLandmarkDetectorWithOptions:options];
Tạo đối tượng
VisionImage
bằngUIImage
hoặcCMSampleBufferRef
.Để sử dụng
UIImage
:- Nếu cần, hãy xoay hình ảnh sao cho thuộc tính
imageOrientation
của nó là.up
. - Tạo một đối tượng
VisionImage
bằng cách sử dụngUIImage
được xoay chính xác. Không chỉ định bất kỳ siêu dữ liệu xoay nào—giá trị mặc định,.topLeft
, phải được sử dụng.Nhanh
let image = VisionImage(image: uiImage)
Mục tiêu-C
FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];
Để sử dụng
CMSampleBufferRef
:Tạo một đối tượng
VisionImageMetadata
chỉ định hướng của dữ liệu hình ảnh có trong bộ đệmCMSampleBufferRef
.Để có được hướng hình ảnh:
Nhanh
func imageOrientation( deviceOrientation: UIDeviceOrientation, cameraPosition: AVCaptureDevice.Position ) -> VisionDetectorImageOrientation { switch deviceOrientation { case .portrait: return cameraPosition == .front ? .leftTop : .rightTop case .landscapeLeft: return cameraPosition == .front ? .bottomLeft : .topLeft case .portraitUpsideDown: return cameraPosition == .front ? .rightBottom : .leftBottom case .landscapeRight: return cameraPosition == .front ? .topRight : .bottomRight case .faceDown, .faceUp, .unknown: return .leftTop } }
Mục tiêu-C
- (FIRVisionDetectorImageOrientation) imageOrientationFromDeviceOrientation:(UIDeviceOrientation)deviceOrientation cameraPosition:(AVCaptureDevicePosition)cameraPosition { switch (deviceOrientation) { case UIDeviceOrientationPortrait: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationLeftTop; } else { return FIRVisionDetectorImageOrientationRightTop; } case UIDeviceOrientationLandscapeLeft: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationBottomLeft; } else { return FIRVisionDetectorImageOrientationTopLeft; } case UIDeviceOrientationPortraitUpsideDown: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationRightBottom; } else { return FIRVisionDetectorImageOrientationLeftBottom; } case UIDeviceOrientationLandscapeRight: if (cameraPosition == AVCaptureDevicePositionFront) { return FIRVisionDetectorImageOrientationTopRight; } else { return FIRVisionDetectorImageOrientationBottomRight; } default: return FIRVisionDetectorImageOrientationTopLeft; } }
Sau đó, tạo đối tượng siêu dữ liệu:
Nhanh
let cameraPosition = AVCaptureDevice.Position.back // Set to the capture device you used. let metadata = VisionImageMetadata() metadata.orientation = imageOrientation( deviceOrientation: UIDevice.current.orientation, cameraPosition: cameraPosition )
Mục tiêu-C
FIRVisionImageMetadata *metadata = [[FIRVisionImageMetadata alloc] init]; AVCaptureDevicePosition cameraPosition = AVCaptureDevicePositionBack; // Set to the capture device you used. metadata.orientation = [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation cameraPosition:cameraPosition];
- Tạo một đối tượng
VisionImage
bằng cách sử dụng đối tượngCMSampleBufferRef
và siêu dữ liệu xoay:Nhanh
let image = VisionImage(buffer: sampleBuffer) image.metadata = metadata
Mục tiêu-C
FIRVisionImage *image = [[FIRVisionImage alloc] initWithBuffer:sampleBuffer]; image.metadata = metadata;
- Nếu cần, hãy xoay hình ảnh sao cho thuộc tính
- Sau đó, chuyển hình ảnh sang phương thức
detect(in:)
:Nhanh
cloudDetector.detect(in: visionImage) { landmarks, error in guard error == nil, let landmarks = landmarks, !landmarks.isEmpty else { // ... return } // Recognized landmarks // ... }
Mục tiêu-C
[landmarkDetector detectInImage:image completion:^(NSArray<FIRVisionCloudLandmark *> *landmarks, NSError *error) { if (error != nil) { return; } else if (landmarks != nil) { // Got landmarks } }];
Nhận thông tin về các địa danh được công nhận
Nếu nhận dạng mốc thành công, một mảng đối tượngVisionCloudLandmark
sẽ được chuyển đến trình xử lý hoàn thành. Từ mỗi đối tượng, bạn có thể lấy thông tin về một mốc được nhận dạng trong ảnh.Ví dụ:
Nhanh
for landmark in landmarks { let landmarkDesc = landmark.landmark let boundingPoly = landmark.frame let entityId = landmark.entityId // A landmark can have multiple locations: for example, the location the image // was taken, and the location of the landmark depicted. for location in landmark.locations { let latitude = location.latitude let longitude = location.longitude } let confidence = landmark.confidence }
Mục tiêu-C
for (FIRVisionCloudLandmark *landmark in landmarks) { NSString *landmarkDesc = landmark.landmark; CGRect frame = landmark.frame; NSString *entityId = landmark.entityId; // A landmark can have multiple locations: for example, the location the image // was taken, and the location of the landmark depicted. for (FIRVisionLatitudeLongitude *location in landmark.locations) { double latitude = [location.latitude doubleValue]; double longitude = [location.longitude doubleValue]; } float confidence = [landmark.confidence floatValue]; }
Bước tiếp theo
- Trước khi triển khai để sản xuất một ứng dụng sử dụng API đám mây, bạn nên thực hiện một số bước bổ sung để ngăn chặn và giảm thiểu tác động của việc truy cập API trái phép .