Google Cloud Vision Detect Texts返回NormalizedVertex为nil的问题求助
问题解决:PNG图片OCR归一化顶点返回nil
- 改用文档级OCR接口:
DetectTexts主要用于检测单行/短文本块,部分场景下不会返回归一化顶点数据。推荐使用DetectDocumentText接口,它针对文档类OCR优化,会返回完整的边界框顶点信息:
// 替换原DetectTexts调用为DetectDocumentText annotation, err := client.DetectDocumentText(ctx, image, nil) if err != nil { log.Fatalf("Failed to detect document text: %v", err) } // 遍历获取文本块的归一化顶点 for _, page := range annotation.Pages { for _, block := range page.Blocks { for _, paragraph := range block.Paragraphs { vertices := paragraph.BoundingBox.NormalizedVertices // 处理顶点数据 } } }
- 调整DetectTexts的请求参数:如果必须使用
DetectTexts,需要在请求中指定使用最新模型,确保返回边界框特征:
// 构建自定义Feature配置 features := []*vision.Feature{ { Type: vision.FeatureTextDetection, Model: "builtin/latest", // 指定最新模型 }, } // 传入配置调用DetectTexts texts, err := client.DetectTexts(ctx, image, features, 10) if err != nil { log.Fatalf("Failed to detect texts: %v", err) } // 此时再尝试获取归一化顶点 if len(texts) > 0 { poly := texts[0].GetBoundingPoly() if poly != nil { vertices := poly.GetNormalizedVertices() // 处理顶点数据 } }
- 排查图片本身问题:确认图片中的文本清晰、尺寸足够,无过度模糊、扭曲或遮挡,否则OCR服务可能无法识别并返回边界框信息。
内容的提问来源于stack exchange,提问作者Manuelarte
相关产品推荐
相关产品推荐

