识别特定区域内有无物品

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()
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值