TensorFlow Computer Vision (CV) 是 TensorFlow 生态系统的一部分,它提供了许多用于计算机视觉任务的工具和库。以下是一些 TensorFlow CV 相关的文档和资源。
安装 TensorFlow CV
首先,确保你已经安装了 TensorFlow。你可以使用以下命令来安装:
pip install tensorflow-cv
快速开始
要使用 TensorFlow CV 进行图像分类,你可以按照以下步骤操作:
- 导入所需的库:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
- 加载图像:
img = image.load_img('path_to_your_image', target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = tf.expand_dims(img_array, axis=0) # Create a batch
- 使用预训练的模型进行预测:
predictions = MobileNetV2().predict(img_array)
- 解析预测结果:
print('Predicted:', decode_predictions(predictions, top=3)[0])