0.源碼:
import cv2 as cv
# 視頻人臉檢測f
def video_face_detection():
face_detector = cv.CascadeClassifier("D:/WorkTool/ins_Anaconda/Lib/site-packages/cv2/data"
"/haarcascade_frontalface_alt2.xml")
capture = cv.VideoCapture(0)
while True:
ret, value = capture.read()
if ret is False:
break
value = cv.flip(value, 1)
faces = face_detector.detectMultiScale(value, 1.06, 25)
for (x, y, w, h) in faces:
cv.rectangle(value, (x, y), (x + y, y + h), (0, 255, 0), 2)
cv.imshow("video_face_detection", value)
key = cv.waitKey(10)
if key == ord('q'): # 按q鍵退出
print("over")
break
if __name__ == "__main__":
video_face_detection()
cv.destroyAllWindows()
1.注意事項
cv.CascadeClassifier() : 裏面可以填寫你python安裝的
Lib/site-packages/cv2/data/haarcascade_frontalface_alt2.xml
如果你pip有opencv的話
關於
face_detector.detectMultiScale(value, 1.06, 25)
:
def detectMultiScale(self,
image: Any,
scaleFactor: Any = None,
minNeighbors: Any = None,
flags: Any = None,
minSize: Any = None,
maxSize: Any = None) -> None
1.image:要檢測的圖片;
2.scaleFactor:比例因子,表示在前後兩次相繼的掃描中,搜索窗口的比例係數。默認爲1.1即每次搜索窗口依次擴大10%;
3.minNeighbors:表示構成檢測目標的相鄰矩形的最小個數(默認爲3個);
4.flags:要麼使用默認值,要麼使用CV_HAAR_DO_CANNY_PRUNING,如果設置爲CV_HAAR_DO_CANNY_PRUNING,那麼函數將會使用Canny邊緣檢測來排除邊緣過多或過少的區域,因此這些區域通常不會是人臉所在區域;
5、6:minSize和maxSize用來限制得到的目標區域的範圍。
參考來源:https://blog.csdn.net/itismelzp/article/details/50379359