Python实现一个简单的目标检测

相关介绍

  • 选择性搜索(Select Search)算法属于候选区域算法,用分割不同区域的办法来识别潜在的物体。在分割的时候,我们要合并那些在某些方面(如颜色、纹理)类似的小区域。相比滑窗法在不同位置和大小的穷举,候选区域算法将像素分配到少数的分割区域中。所以最终候选区域算法产生的数量比滑窗法少的多,从而大大减少运行物体识别算法的次数。同时候选区域算法所选定的范围兼顾了不同的大小和长宽比。

实验环境

  • Python 3.6.2
  • Tesorflow 2.3.0
  • Numpy 1.18.5
  • Opencv 3.4.2

基本思路

在这里插入图片描述

  1. 输入测试图片
  2. 用选择性搜索(Select Search)方法,对输入图片选出N个候选区域
  3. 训练好的CNN模型预测每个候选区域,保留一个得分最高的候选区域
  4. 输出预测结果图片

代码实现

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()
# 高召回但慢选择性搜索方法
# 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('LeNet_classify_model.h5')
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()

输出结果

图片源于网络,如有侵权,请联系删除!

版权声明:本文为CSDN博主「FriendshipT」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/FriendshipTang/article/details/113737709

FriendshipT

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

在Android上部署TF目标检测模型

在移动设备上部署机器学习模型是ML即将开始的新阶段。目标检测模型,已经与语音识别、图像分类等模型一起应用于移动设备。这些模型通常运行在支持GPU的计算机上,部署在移动设备上时也有大量用例。为了演示如何将ML模型&#x