相关介绍
- 选择性搜索(Select Search)算法属于候选区域算法,用分割不同区域的办法来识别潜在的物体。在分割的时候,我们要合并那些在某些方面(如颜色、纹理)类似的小区域。相比滑窗法在不同位置和大小的穷举,候选区域算法将像素分配到少数的分割区域中。所以最终候选区域算法产生的数量比滑窗法少的多,从而大大减少运行物体识别算法的次数。同时候选区域算法所选定的范围兼顾了不同的大小和长宽比。
实验环境
- Python 3.6.2
- Tesorflow 2.3.0
- Numpy 1.18.5
- Opencv 3.4.2
基本思路
- 输入测试图片
- 用选择性搜索(Select Search)方法,对输入图片选出N个候选区域
- 用训练好的CNN模型预测每个候选区域,保留一个得分最高的候选区域
- 输出预测结果图片
代码实现
import sys
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
img = cv2.imread( './car3.jpg' )
newHeight = 200
newWidth = int( img.shape[1] * 200 / img.shape[0] )
img = cv2.resize( img, (newWidth, newHeight) )
ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage( img )
ss.switchToSelectiveSearchFast()
rects = ss.process()
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('LeNet_classify_model.h5')
while True:
new_img = img.copy()
region_score = []
max_rect = 0
max_name = ""
max_score = 0
for i, rect in enumerate( rects ):
x, y, w, h = rect
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))
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), 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
if k == 113:
break
cv2.destroyAllWindows()
输出结果