ReadMe:运行本程序前需配置相应的文件、库等,请阅读程序了解详情。
识别图片中绿色框内有无物体,且识别物体种类及获取其位置坐标与置信度。
图片样张如下,左、右分别为有饮料瓶和无饮料瓶的情况:
Marking:读取图片
Res:绿色区域识别
Output:识别结果(Something)及置信度
Marking:读取图片
Res:绿色区域识别
Output:识别结果(Nothing)
没时间写注解,先把代码放这了。
#!/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()