import cv2
save_path = 'D:/YOURSPHOTOS/'
cv2.namedWindow("camera", 1)
# 开启ip摄像头
video = "http://admin:[email protected]:8081/" # 此处@后的ipv4 地址需要改为app提供的地址
# 打开摄像头
cap = cv2.VideoCapture(video)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
i = 0
while (True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
# 成功框选则样本数增加
i += 1
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
roiImg = frame[y:y + h, x:x + w]
gray = cv2.cvtColor(roiImg, cv2.COLOR_BGR2GRAY)
cv2.imwrite(save_path + str(i) + '.jpg', gray)
cv2.imshow("camera", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
相关文章: