• 使用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)

  • 相关阅读:
    【Linux】基本指令(四)
    数据库复习
    PS与PL与PG082
    在Linux和Windows上安装分布式事务seata
    借助GPU算力编译Android
    深入理解final关键字
    【LeetCode每日一题】——844.比较含退格的字符串
    docker-compose 之 redis-stack
    Linux的进程互调技术(多语言互调)
    大二暑假 + 大三上
  • 原文地址:https://blog.csdn.net/mldxs/article/details/133581109