如何在openCV中命名检测到的对象?
我有一个简单的代码,可以在openCV中使用Haar cascad来检测人脸。效果很好,但我需要命名一个检测到的对象。 例如,当我检测到人脸时,在终端(或矩形上方)中,将我的面孔和相似图片打印给另一个对象。
下面,我粘贴我的简单代码。
感谢每个答案。
import cv2
import sys
class HaarClassifier:
def __init__(self):
self.faceCascade =
cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
def detectFace(self, grayFrame, outputFrame):
colaScaleFactor = 1.2
colaMinNeighs = 5
faces = self.faceCascade.detectMultiScale(grayFrame, colaScaleFactor, colaMinNeighs)
for (x, y, w, h) in faces:
cv2.rectangle(outputFrame, (x, y), (x + w, y + h), (255, 0, 0), 2)
class Recognition(HaarClassifier):
def __init__(self):
super().__init__()
self.haarClassifier = HaarClassifier()
def capture(self):
cap = cv2.VideoCapture(0)
if cap.isOpened():
return cap
else:
print("Failed on camera capture")
sys.exit()
def start(self):
cap = self.capture()
while True:
ret, img = cap.read()
if ret:
grayScale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
self.haarClassifier.detectFace(grayScale, img)
cv2.imshow('img', img)
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
recognition = Recognition()
recognition.start()