添加到人脸识别数据路径新图像,而不是覆盖它们
Adding to the Face Recognition data path new images , and not overwriting them
我目前正在使用 OpenCV 和 python 从事人脸识别项目。
问题是,面部识别的准确性不是很好,所以我正在考虑在数据路径中添加更多图像,使用不同的照明、背景等来改进它。
这里的问题是每当我使用
cv2.imwrite("data/User."+str(face_ID)+"."+str(count)+".jpg", gray[y:y+h, x:x+w])
它会覆盖路径中以前保存的图像。
我工作正常,但我只想做一些事情,比如在每次 运行 函数时将新图像附加到路径。
这是数据生成器部分。
def data_generator():
count = 0
# asking user for data input
face_ID = input("[INFO] Please enter user ID and press <return> ")
print("[INFO] Thank you\n Now please look at the camera and wait.")
# start the video capture
cap = cv2.VideoCapture(0)
try:
while True:
# Here we detect the face
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detecting faces
faces = detector.detectMultiScale(gray,
scaleFactor = 1.3,
minNeighbors = 5,
minSize= (20, 20)
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_img = img[y:y+h, x:x+w]
count += 1
cv2.imwrite("data/User."+str(face_ID)+"."+str(count)+".jpg", gray[y:y+h, x:x+w])
cv2.imshow('img', img)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
elif count >= 30:
break
except KeyboardInterrupt:
pass
print("[INFO] Data gathered.")
print("[INFO] Saving Data.")
print("[INFO] Exiting program and cleanup stuff")
cap.release()
cv2.destroyAllWindows()
我认为问题是您的 cv2.imwrite("data/User."+str(face_ID)+"."+str(count)+".jpg", gray[y:y+h, x:x+w])
在 while True:
循环之外。
我还使用 os.path.join
和 .format
来保存图像。因此,您可以在循环外定义一个目录并像这样使用 .format
以获得更好的 IMO 概览:
cv2.imwrite(os.path.join(directory, 'User.{}.{}.jpg'.format(face_ID, count)), gray[y:y+h, x:x+w])
我目前正在使用 OpenCV 和 python 从事人脸识别项目。 问题是,面部识别的准确性不是很好,所以我正在考虑在数据路径中添加更多图像,使用不同的照明、背景等来改进它。 这里的问题是每当我使用
cv2.imwrite("data/User."+str(face_ID)+"."+str(count)+".jpg", gray[y:y+h, x:x+w])
它会覆盖路径中以前保存的图像。
我工作正常,但我只想做一些事情,比如在每次 运行 函数时将新图像附加到路径。
这是数据生成器部分。
def data_generator():
count = 0
# asking user for data input
face_ID = input("[INFO] Please enter user ID and press <return> ")
print("[INFO] Thank you\n Now please look at the camera and wait.")
# start the video capture
cap = cv2.VideoCapture(0)
try:
while True:
# Here we detect the face
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detecting faces
faces = detector.detectMultiScale(gray,
scaleFactor = 1.3,
minNeighbors = 5,
minSize= (20, 20)
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_img = img[y:y+h, x:x+w]
count += 1
cv2.imwrite("data/User."+str(face_ID)+"."+str(count)+".jpg", gray[y:y+h, x:x+w])
cv2.imshow('img', img)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
elif count >= 30:
break
except KeyboardInterrupt:
pass
print("[INFO] Data gathered.")
print("[INFO] Saving Data.")
print("[INFO] Exiting program and cleanup stuff")
cap.release()
cv2.destroyAllWindows()
我认为问题是您的 cv2.imwrite("data/User."+str(face_ID)+"."+str(count)+".jpg", gray[y:y+h, x:x+w])
在 while True:
循环之外。
我还使用 os.path.join
和 .format
来保存图像。因此,您可以在循环外定义一个目录并像这样使用 .format
以获得更好的 IMO 概览:
cv2.imwrite(os.path.join(directory, 'User.{}.{}.jpg'.format(face_ID, count)), gray[y:y+h, x:x+w])