You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Firebase Cloud Vision(ML)获取图片特定品牌信息

How to Get Brand/Product Names (Like "Apple MacBook") with Firebase Cloud Vision API

Hey there! I get it—using the default cloud image labeler gives you generic tags like "notebook" or "laptop" instead of the specific brand/product you're aiming for. Let's fix that!

The problem with your current code is that cloudImageLabeler() is built to detect general object categories, not brand logos or specific product models. To get "Apple MacBook" level specificity, you have two solid options depending on your needs:


Option 1: Use Cloud Logo Detection (Quickest for Brand Names)

Firebase Vision has a built-in Cloud Logo Detector that identifies brand logos in images. This will return names like "Apple", "Google", etc.—perfect if you just need the brand, or want to pair it with category labels to get something like "Apple Laptop".

Here's how to modify your code to use it:

func pickedImage(image: UIImage) { 
    imageView.image = image 
    imageView.contentMode = .scaleAspectFit 
    guard let image = imageView.image else { return } 

    // Replace the labeler with Cloud Logo Detector
    let logoDetector = Vision.vision().cloudLogoDetector()

    let imageMetadata = VisionImageMetadata() 
    imageMetadata.orientation = .topLeft 
    let visionImage = VisionImage(image: image) 
    visionImage.metadata = imageMetadata 

    logoDetector.process(visionImage) { logos, error in 
        guard error == nil, let logos = logos, !logos.isEmpty else { 
            let errorString = error?.localizedDescription ?? "No results returned." 
            print("Logo detection failed with error: \(errorString)") 
            return 
        } 

        var results = [String]() 
        let resultsText = logos.map { logo -> String in 
            let brandName = logo.description
            results.append(brandName)
            return "Brand: \(brandName), " + "Confidence: \(logo.confidence ?? 0), " + "EntityID: \(logo.entityID ?? "")" 
        }.joined(separator: "\n") 

        print(results.count) 
        print(resultsText) 
        // Combine brand with original category labels if needed (e.g., "Apple + Laptop")
        self.labelTxt.text = results.joined(separator: ",") 
        results.removeAll() 
    } 
}

If you want to combine brand names with the original category tags (like "Laptop"), you can run both the logo detector and image labeler in parallel and merge their results.


Option 2: Use Product Search (For Specific Product Models)

If you need exact product names like "Apple MacBook Pro 16", you'll need to use Firebase Vision Product Search. This requires setting up a product catalog in Google Cloud first (uploading reference images of the products you want to detect), then using the SDK to search for matches.

Step 1: Set Up Your Product Catalog

  1. Go to the Google Cloud Console, navigate to Vision API > Product Search.
  2. Create a product set, then add products (like "Apple MacBook Pro") with reference images (different angles, lighting conditions work best).
func pickedImage(image: UIImage) { 
    imageView.image = image 
    imageView.contentMode = .scaleAspectFit 
    guard let image = imageView.image else { return } 

    // Initialize Product Search client
    let productSearchClient = Vision.vision().productSearch()
    let imageMetadata = VisionImageMetadata() 
    imageMetadata.orientation = .topLeft 
    let visionImage = VisionImage(image: image) 
    visionImage.metadata = imageMetadata 

    // Configure search parameters (replace with your product set info)
    let options = VisionProductSearchOptions()
    options.productSetID = "YOUR_PRODUCT_SET_ID" // Replace with your ID
    options.productCategories = ["electronics"] // Match your product category

    productSearchClient.process(visionImage, options: options) { results, error in
        guard error == nil, let results = results, !results.results.isEmpty else {
            let errorString = error?.localizedDescription ?? "No product matches found."
            print("Product search failed: \(errorString)")
            return
        }

        // Get the top matching product
        let topResult = results.results.first!
        let productName = topResult.product.displayName
        let confidence = topResult.score

        print("Top match: \(productName) with confidence \(confidence)")
        self.labelTxt.text = productName
    }
}

This will return the exact product name from your catalog if the uploaded image matches a reference photo.


Key Notes

  • Cloud Logo Detection is great for quick brand identification without setup, but won't give specific models.
  • Product Search requires upfront work to build your catalog, but gives precise product names.
  • Make sure you've enabled the corresponding APIs in the Firebase/Google Cloud Console (Cloud Vision API for both, Product Search specifically for option 2).

内容的提问来源于stack exchange,提问作者iVJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:14:11