给我一个基于深度学习Faster-CNN的小目标检测python代码
时间: 2023-04-08 10:03:30 浏览: 163
以下是一个基于深度学习Faster-CNN的小目标检测Python代码示例:
```python
import cv2
import numpy as np
import tensorflow as tf
# 加载模型
model = tf.keras.models.load_model('faster_cnn.h5')
# 加载标签
with open('labels.txt', 'r') as f:
labels = [line.strip() for line in f.readlines()]
# 加载图像
image = cv2.imread('test.jpg')
# 图像预处理
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224, 224))
image = image / 255.0
image = np.expand_dims(image, axis=0)
# 目标检测
predictions = model.predict(image)
class_id = np.argmax(predictions)
class_label = labels[class_id]
confidence = predictions[0][class_id]
# 显示结果
cv2.putText(image, f'{class_label}: {confidence:.2f}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请注意,这只是一个示例代码,您需要根据您的具体需求进行修改和优化。