Python 如何结合颜色检测器和SSD检测器来识别一个人?
我想结合一个颜色检测器和SSD检测器来实时识别一个人,但当有多个人和多个区域用于检测颜色时,我遇到了一些问题,我不知道原因是什么 更新: 我知道原因,因为它用SSD探测器的边界框计算了一个颜色区域框,忽略了其他框,但我无法解决它 我使用了联合上的交集(IoU)来组合它们,并在等式中进行更改以适应我的问题 这是我的全部代码:Python 如何结合颜色检测器和SSD检测器来识别一个人?,python,python-2.7,opencv,image-processing,object-detection,Python,Python 2.7,Opencv,Image Processing,Object Detection,我想结合一个颜色检测器和SSD检测器来实时识别一个人,但当有多个人和多个区域用于检测颜色时,我遇到了一些问题,我不知道原因是什么 更新: 我知道原因,因为它用SSD探测器的边界框计算了一个颜色区域框,忽略了其他框,但我无法解决它 我使用了联合上的交集(IoU)来组合它们,并在等式中进行更改以适应我的问题 这是我的全部代码: from imutils.video import VideoStream from imutils.video import FPS import numpy as np
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import imutils
import time
import cv2
from shapely.geometry import Polygon
global x1 , y1 , w1, h1, endX, startX, endY, startY , area1
x1 = 0
y1 = 0
w1 = 0
h1 = 0
endY = 0
endX = 0
startX = 0
startY = 0
area1 = 0
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "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('prototxt.txt', 'caffemodel')
print("[INFO] starting video stream...")
vs = cv2.VideoCapture(0)
time.sleep(2.0)
fps = FPS().start()
width = vs.get(3) # float
height = vs.get(4) # float
print width, height
fps = FPS().start()
while True:
ret, frame = vs.read()
frame = imutils.resize(frame, width=400)
#==============================================color detector==================
detected_color = 0
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#black_color
# green_lower = np.array([0, 0, 0], np.uint8)
# green_upper = np.array([180, 255, 30], np.uint8)
# black_red
green_lower = np.array([136, 87, 111], np.uint8)
green_upper = np.array([180, 255, 255], np.uint8)
# green_lower = np.array([36, 25, 25], np.uint8)
# green_upper = np.array([70, 255, 255], np.uint8)
green = cv2.inRange(hsv, green_lower, green_upper)
# Morphological Transform, Dilation
kernal = np.ones((5, 5), "uint8")
red = cv2.dilate(green, kernal)
res_red = cv2.bitwise_and(frame, frame, mask=green)
# Tracking blue
(_, contours, hierarchy) = cv2.findContours(green, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area1 = cv2.contourArea(contour)
if (area1 > 300):
x1, y1, w1, h1 = cv2.boundingRect(contour)
img = cv2.rectangle(frame, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 2)
cv2.putText(frame, "green Colour", (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0))
detected_color = 1
# ==============================================SSD detector==================
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5)
net.setInput(blob)
detections = net.forward()
big_area = 0
big_center = 320
detected = 0
for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
object_type = int(detections[0, 0, i, 1])
if object_type == 15 and confidence > 0.2:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
label = "{}: {:.2f}%".format('person', confidence * 100)
cv2.rectangle(frame, (startX, startY), (endX, endY), [0, 0, 255], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 0, 255], 2)
# ==============================================IOU==================
a = Polygon([(x1, y1 + h1), (x1 + w1, y1 + h1), (x1 + w1, y1), (x1, y1)])
b = Polygon([(startX, endY), (endX, endY), (endX, startY), (startX, startY)])
iou = (a.intersection(b).area / (a.area + 1))
print iou
# print 'a.area', a.area, 'hullArea', hullArea
if detected_color:
if iou >= 0.7:
# print iou
cv2.rectangle(frame, (startX, startY), (endX, endY), [0, 0, 0], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 0, 0], 2)
else:
cv2.rectangle(frame, (startX, startY), (endX, endY), [0, 0, 255], 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
fps.update()
fps.stop()
vs.release()
cv2.destroyAllWindows()
数据,或
输出:
第一个1(仅SDD探测器)适合我:
这些数字(用于人体检测的红色探测器和SDD探测器)对我来说没有问题,因此人体边界框内的颜色区域,这意味着IOU=1
,我在代码中选择了IOU>=0.7
:
这是一个数字(用于人体检测的红色探测器和SDD探测器)对我来说是正常的,因此人体边界框外的颜色区域,这意味着IOU=0
,其中我在代码中选择了IOU>=0.7
:
所有这些对我来说都不是问题,问题是如果有很多像这个数字的颜色区域,
你可以看到右边的人被识别,但左边的人没有被识别,假设它被识别,因为在人类的边界框中有一个颜色区域
注:
蓝色框表示快速颜色检测
红色框表示“快速人体检测”
黑匣子将它们快速组合在一起
我想识别所有人当人类边界框内有一个颜色区域时,无论人数或颜色检测器的区域数量如何,每个人内部有一个区域足以识别他/她