当前位置: 首页 > news >正文

10 基于深度学习的目标检测

首次完成时间:2024 年 11月 20 日


1. 使用OpenCV的dnn模块实现图像分类。

1)程序代码:

import numpy as np
import cv2# 解析标签文件
row = open("model1/synset_words.txt").read().strip().split("\n")
class_label = [r[r.find(" "):].split(",")[0] for r in row]# 载入caffe所需的配置文件
net = cv2.dnn.readNetFromCaffe("model1/bvlc_googlenet.prototxt","model1/bvlc_googlenet.caffemodel")# 读取待分类图像
img = cv2.imread("photos/cat.jpg")  # 确保这里的路径是正确的# 转换格式
blob = cv2.dnn.blobFromImage(img, 1, (224, 224), (104, 117, 123))# 加载图像
net.setInput(blob)# 预测
preds = net.forward()# 排序,取概率最大的结果
idx = np.argsort(preds[0])[-1]# 获取图片的原始尺寸
(h, w) = img.shape[:2]# 等比例缩减图片大小
resized_img = cv2.resize(img, (w // 4, h // 4))# 可视化处理,显示图像类别、置信度等信息
text = "label: {}-{:.2f}%".format(class_label[idx], preds[0][idx] * 100)
cv2.putText(resized_img, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0))# 显示缩减后的图片
cv2.imshow("resized_result", resized_img)# 保存缩减后的图片
cv2.imwrite("photos/resized_result.jpg", resized_img)cv2.waitKey(0)
cv2.destroyAllWindows()

2)代码分析:

2. 使用OpenCV实现目标检测,实现发现不明车辆或行人进入检测区,即进行报警。

1)程序代码:

import numpy as np
import cv2def prepareDataSet():# 准备数据集args = {}args["prototxt"] = "model2/MobileNetSSD_deploy.prototxt"args["model"] = "model2/MobileNetSSD_deploy.caffemodel"return argsdef createNet():# 构建网络模型对象args = prepareDataSet()# load our serialized model from diskprint("[INFO] loading model...")net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])return netif __name__ == "__main__":# 定义类别名称序列CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog","horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]# 定义边框颜色序列COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))# 打开摄像头或视频文件camera = cv2.VideoCapture("videos/12686501_3840_2160_60fps.mp4")# 构建网络模型net = createNet()while True:ret, frame = camera.read()if ret:# 将帧的尺寸调整为1080pframe = cv2.resize(frame, (1920, 1080))(h, w) = frame.shape[:2]blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5)net.setInput(blob)detections = net.forward()# 遍历结果for i in np.arange(0, detections.shape[2]):# 获得置信度confidence = detections[0, 0, i, 2]# 根据置信度阈值过滤执行度if confidence > 0.2:# 根据最大置信度获取类别下标idx = int(detections[0, 0, i, 1])# 获取位置信息box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])(startX, startY, endX, endY) = box.astype("int")# 显示类别信息和位置边框label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)print("[INFO] {}".format(label))cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2)y = startY - 15 if startY - 15 > 15 else startY + 15cv2.putText(frame, label, (startX, y),cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)if CLASSES[idx] == "person":print("raise the alarm")# 显示结果cv2.imshow("result", frame)# 按下空格退出 or escif cv2.waitKey(1) == ord(' ') or cv2.waitKey(1) == 27:breakelse:breakcamera.release()cv2.destroyAllWindows()

2)代码分析:

http://www.lryc.cn/news/490848.html

相关文章:

  • leetcode top100中的30道递归和贪心
  • 非常简单实用的前后端分离项目-仓库管理系统(Springboot+Vue)part 2
  • shell脚本(完)—脚本互调重定向的学习
  • ant-design-vue中table某一列进行合并
  • 基于Springboot+Vue社区养老服务管理系统(源码+lw+讲解部署+PPT)
  • 大数据调度组件之Apache DolphinScheduler
  • 介绍一下strlwr(arr);(c基础)
  • meterpreter常用命令 上
  • 【kubernetes】kubernetes各组件的调用关系
  • Java-08 深入浅出 MyBatis - 多对多模型 SqlMapConfig 与 Mapper 详细讲解测试
  • Vue.js修饰符
  • 【数据分享】2024年我国省市县三级的住宿服务设施数量(8类住宿设施/Excel/Shp格式)
  • 【含文档】基于.NET的医院医保管理系统(含源码+数据库+lw)
  • c++源码阅读__smart_ptr__正文阅读
  • 图形化界面MySQL(MySQL)(超级详细)
  • 【2024 Optimal Control 16-745】Julia语法
  • Opencv+ROS实现摄像头读取处理画面信息
  • 网络安全,文明上网(2)加强网络安全意识
  • 深度学习实战图像缺陷修复
  • jenkins 2.346.1最后一个支持java8的版本搭建
  • 【数据库原理】创建与维护表,DDL数据定义语言
  • 驾驭Go语言中的不确定性:深入错误处理机制
  • 3D Gaussian Splatting在鱼眼相机中的应用与投影变换
  • 【Unity踩坑】在Mac上安装Cocoapods失败
  • uni-app 认识条件编译,了解多端部署
  • SPA 首屏加载慢的原因及解决方案:结合实际项目的详细讲解
  • vue3+ts el-tabel 搜索组件
  • leetcode 排序算法汇总
  • 【C】错误的变量定义导致sprintf()‌输出错误
  • python基础导包