码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 使用python-opencv检测图片中的人像


    最简单的方法进行图片中的人像检测

    使用python-opencv配合yolov3模型进行图片中的人像检测

    1、安装python-opencv、numpy

    1. pip install opencv-python
    2. pip install numpy

    2、下载yolo模型文件和配置文件:

    下载地址:

     https://download.csdn.net/download/mldxs/88396654yicon-default.png?t=N7T8https://download.csdn.net/download/mldxs/88396654

    yolo官网:

    YOLO: Real-Time Object DetectionYou only look once (YOLO) is a state-of-the-art, real-time object detection system.icon-default.png?t=N7T8https://pjreddie.com/darknet/yolo/3、搬砖:代码比较简单并且带注释,不过多介绍

    1. import cv2
    2. import numpy as np
    3. # 读取输入图像
    4. image = cv2.imread('input.jpeg')
    5. # 加载YOLOv3模型和类别标签
    6. net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
    7. classes = []
    8. with open('coco.data', 'r') as f:
    9. classes = f.read().strip().split('\n')
    10. # 获取YOLO模型的输出层名称
    11. layer_names = net.getLayerNames()
    12. output_layers = []
    13. unconnected_layers = net.getUnconnectedOutLayers()
    14. # 根据输出层索引获取输出层名称
    15. for i in unconnected_layers:
    16. output_layers.append(layer_names[i - 1])
    17. # 为每个类别生成随机颜色
    18. colors = np.random.uniform(0, 255, size=(len(classes), 3))
    19. # 获取图像的尺寸
    20. height, width, channels = image.shape
    21. # 创建YOLO模型的输入blob
    22. blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
    23. # 将blob设置为模型的输入
    24. net.setInput(blob)
    25. outs = net.forward(output_layers)
    26. class_ids = []
    27. confidences = []
    28. boxes = []
    29. # 处理YOLO模型的输出
    30. for out in outs:
    31. for detection in out:
    32. scores = detection[5:]
    33. class_id = np.argmax(scores)
    34. confidence = scores[class_id]
    35. # 如果置信度大于0.5并且类别是"person"(0对应COCO数据集中的"person"类)
    36. if confidence > 0.5 and class_id == 0:
    37. center_x = int(detection[0] * width)
    38. center_y = int(detection[1] * height)
    39. w = int(detection[2] * width)
    40. h = int(detection[3] * height)
    41. x = int(center_x - w / 2)
    42. y = int(center_y - h / 2)
    43. boxes.append([x, y, w, h])
    44. confidences.append(float(confidence))
    45. class_ids.append(class_id)
    46. # 使用非极大值抑制获取最终的检测结果
    47. indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
    48. margin = 30 # 定义边框扩展的边距大小
    49. # 绘制边框和类别标签
    50. for i in range(len(boxes)):
    51. if i in indexes:
    52. x, y, w, h = boxes[i]
    53. label = str(classes[class_ids[i]])
    54. color = colors[i]
    55. # 扩展边框的坐标
    56. x_new = max(0, x - margin)
    57. y_new = max(0, y - margin)
    58. w_new = min(image.shape[1], w + 2 * margin)
    59. h_new = min(image.shape[0], h + 2 * margin)
    60. # 绘制扩展后的边框
    61. cv2.rectangle(image, (x_new, y_new), (x_new + w_new, y_new + h_new), color, 2)
    62. cv2.putText(image, label, (x_new, y_new - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
    63. # 显示带有边框的图像
    64. cv2.imshow('Detected Bodies', image)
    65. cv2.waitKey(0)
    66. cv2.destroyAllWindows()

    最终效果:

    yolo有很多检测类别,上述代码只对人像进行检测,就是检测类别里的第一项(person)

  • 相关阅读:
    分类预测 | MATLAB实现SSA-CNN-LSTM麻雀算法优化卷积长短期记忆神经网络数据分类预测
    【EI会议征稿】2024年智慧城市与信息系统国际学术会议 (ICSCIS 2024)
    多线程 Leetcode 打印零与奇偶数
    java中的注解
    常用docker镜像启动了解
    js 定时器 setInterval(图片的自动变换)
    Unity 实现原神中的元素反应
    全表扫描、全索引扫描、覆盖索引
    Rancher系列文章-Rancher v2.6使用脚本实现导入集群
    Google Chrome(谷歌浏览器)安装使用
  • 原文地址:https://blog.csdn.net/mldxs/article/details/133581109
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号