当前位置:   article > 正文

识别特定区域内有无物品_区域内有没有物体怎么识别

区域内有没有物体怎么识别

ReadMe:运行本程序前需配置相应的文件、库等,请阅读程序了解详情。

0 功能

识别图片中绿色框内有无物体,且识别物体种类及获取其位置坐标与置信度。
图片样张如下,左、右分别为有饮料瓶和无饮料瓶的情况:

在这里插入图片描述在这里插入图片描述

1 有饮料瓶的情况

在这里插入图片描述
Marking:读取图片
在这里插入图片描述
Res:绿色区域识别
在这里插入图片描述
Output:识别结果(Something)及置信度

2 无饮料瓶的情况

在这里插入图片描述
Marking:读取图片
在这里插入图片描述
Res:绿色区域识别
在这里插入图片描述
Output:识别结果(Nothing)

3 代码

没时间写注解,先把代码放这了。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# python3.7
# author:Harden Qiu
# Date: 2019/8/19

import cv2
import numpy as np


def marking(img):
    lower = np.array([35, 43, 46], dtype="uint8")
    upper = np.array([77, 255, 255], dtype="uint8")
    converted = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    markingMask = cv2.inRange(converted, lower, upper)
    markingMask = cv2.GaussianBlur(markingMask, (5, 5), 0)
    marking = cv2.bitwise_and(img, img, mask=markingMask)
    return marking


def main():
    img_rgb = cv2.imread("./images/example_01.jpg")
    cv2.imshow("Marking",img_rgb)
    img_green = marking(img_rgb)
    img_gray = cv2.cvtColor(img_green, cv2.COLOR_BGR2GRAY)
    img_gray = cv2.GaussianBlur(img_gray, (5, 5), 0)
    ret, img_binary = cv2.threshold(img_gray, 50, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(img_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    print("number of contours:%d" % len(contours))
    cv2.drawContours(img_binary, contours, -1, (0, 0, 255), 2)
    area = []
    for i in range(len(contours)):
        area.append(cv2.contourArea(contours[i]))
    max_idx = np.argmax(area)
    for i in range(max_idx - 1):
        cv2.fillConvexPoly(img_binary, contours[max_idx - 1], 0)
    cv2.fillConvexPoly(img_binary, contours[max_idx], 255)
    x, y, w, h = cv2.boundingRect(contours[max_idx])
    cv2.rectangle(img_binary, (x, y), (x + w, y + h), (0, 255, 0), 2)
    rect = cv2.minAreaRect(contours[max_idx])
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    xmax = np.max([box[0][0], box[1][0], box[2][0], box[3][0]])
    xmin = np.min([box[0][0], box[1][0], box[2][0], box[3][0]])
    ymax = np.max([box[0][1], box[1][1], box[2][1], box[3][1]])
    ymin = np.min([box[0][1], box[1][1], box[2][1], box[3][1]])
    res_widths = abs(xmax - xmin)
    res_heights = abs(ymax - ymin)
    res = np.zeros([res_heights-50, res_widths-50, 3], np.uint8)
    Xmin = xmin
    Ymin = ymin
    para = 30
    for xmin in range(para,res_widths-para):
        for ymin in range(para,res_heights-para):
            res[ymin-para, xmin-para] = img_rgb[ymin + Ymin, xmin + Xmin]
    cv2.imshow("Res", res)
    CLASSES = ["background", "aeroplane", "bicycle", "bird", "shelf",
               "bottle", "box", "robot", "cat", "chair", "cow", "diningtable",
               "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
               "sofa", "train", "tvmonitor"]
    COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
    print("[INFO] loading model...")
    net = cv2.dnn.readNetFromCaffe("./MobileNetSSD_deploy.prototxt.txt", "./MobileNetSSD_deploy.caffemodel")
    print("------", res)
    (h, w) = res.shape[:2]
    blob = cv2.dnn.blobFromImage(cv2.resize(res, (300, 300)), 0.007843, (300, 300), 127.5)
    print("[INFO] computing object detections...")
    net.setInput(blob)
    detections = net.forward()
    for i in np.arange(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        idx = int(detections[0, 0, i, 1])
        if CLASSES[idx] == "bottle":
            if confidence > 0.4:
                box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                (startX, startY, endX, endY) = box.astype("int")
                print("瓶子的位置:起始坐标(%d,%d)终点坐标(%d,%d)"% (startX, startY, endX, endY))
                label = "Something--{}: {:.2f}%".format((startX, startY), confidence * 100)
                print("[有货物在货架上] {}".format(label))
                cv2.rectangle(res, (startX, startY), (endX, endY), COLORS[idx], 2)
                y = startY - 15 if startY - 15 > 15 else startY + 15
                cv2.putText(res, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
        else:
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
            label = "Nothing"
            cv2.putText(res, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
    cv2.imshow("Output", res)
    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/651884
推荐阅读
相关标签
  

闽ICP备14008679号