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

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()

输出结果

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

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FriendshipT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值