首先在anaconda配置tensorflow环境进行目标检测
打开anaconda prompt输入指令conda install tensorflow下载
输入conda list进行检测列表出现tensorflow相关的安装项目即可
然后进行目标检测
import sys
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
# 读取图片
img = cv2.imread('D:/vision/1.jpg')
# 按比例缩放图片
newHeight = 100
newWidth = int(img.shape[1] * 100/ img.shape[0])
img = cv2.resize(img, (newWidth, newHeight))
# 创建选择性搜索分割对象
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
# 设置输入图像,我们将运行分割
ss.setbaseImage(img)
# 快速但低召回选择性搜索方法
ss.switchToSelectiveSearchFast()
# 高召回但慢选择性搜索方法
ss.switchToSelectiveSearchQuality()
# 运行选择性搜索分割输入图像
rects = ss.process()
# print(rects)
print( 'Total Number of Region Proposals: {}'.format( len( rects ) ) )
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
# 加载创建完全相同的模型,包括其权重和优化程序
#loaded_model = tf.keras.models.load_model('D:/vision/water/1.jpg')
while True:
# 创建原始图像的副本
new_img = img.copy()
# print(new_img)
region_score = []
max_rect = 0
max_name = ""
max_score = 0
# 重复所有的区域建议
for i, rect in enumerate( rects ):
x, y, w, h = rect # 预测框的左上角坐标(x,y)以及框的宽w,高h
pre_img = new_img[y:y+h,x:x+w]
pre_img = cv2.resize(pre_img,(32,32))
pre_img = (np.expand_dims(pre_img,0))
# 输入的图片维度为(1,32,32,3)
#pred_arr = loaded_model.predict(pre_img)
# 预测标签
#pre_label = np.argmax(pred_arr[0])
# 预测得分
#score = np.max(pred_arr[0])
# 预测类名
#class_name = class_names[pre_label]
#if score > max_score:
# max_rect = rect
# max_name = class_name
# max_score = score
#print([max_rect,max_name,max_score])
#x,y,w,h = max_rect
# cv2.rectangle(new_img, (x, y), (x + w, y + h), (0, 255, 0), 1, cv2.LINE_AA )
cv2.rectangle(new_img, (x, y), (x + w, y + h), (0, 255, 0), 2, cv2.LINE_AA)
font = cv2.FONT_HERSHEY_SIMPLEX
text = max_name+" "+str(max_score*100)[0:4]+"%"
cv2.putText(new_img, text, (x, y-5), font, 0.5, (0,0,255), 2)
# 显示输出
cv2.imshow("Output", new_img)
# 等待按键输入
k = cv2.waitKey( 0 ) & 0xFF
# q键
if k == 113:
break
# 关闭所有窗口
cv2.destroyAllWindows()



