# cv2.error: OpenCV(4.8.0) /modules/dnn/src/tensorflow/tf_io.cpp:89: error: (-2:未指定错误) 无法读取 .pb 文件：无效的 GraphDef

- **ID:** `opencv/dnn-readnet-from-tensorflow-failed`
- **领域:** opencv
- **类别:** module_error
- **错误码:** `-2`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

TensorFlow .pb 模型文件损坏、与 OpenCV 的 protobuf 版本不兼容，或使用不同的操作集导出。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |
| 4.10.0 | active | — | — |

## 解决方案

1. ```
   # In TensorFlow:
import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
# Freeze and save as frozen graph
model = tf.saved_model.load('model_dir')
frozen_func = convert_variables_to_constants_v2(model.signatures['serving_default'])
tf.io.write_graph(frozen_func.graph, '.', 'frozen_model.pb', as_text=False)
   ```
2. ```
   net = cv2.dnn.readNetFromTensorflow('model.pb', 'model.pbtxt')  # Provide text protobuf for graph structure
   ```

## 无效尝试

- **** — If the model was exported with an incompatible TensorFlow version, re-downloading won't change the GraphDef format. (50% 失败率)
- **** — ONNX conversion may fail if the original GraphDef is malformed or contains unsupported operations. (70% 失败率)
- **** — OpenCV's bundled protobuf may still be incompatible with newer TensorFlow exports; the error persists. (60% 失败率)
