【发布时间】:2018-11-15 21:22:36
【问题描述】:
我目前正在使用 OpenCV 和 python 进行人脸识别项目。 问题是,人脸识别的准确性不是很好,所以我正在考虑在数据路径中添加更多图像,使用不同的照明、背景等来改进它。 这里的问题是,每当我使用
cv2.imwrite("数据/用户"+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()
【问题讨论】:
-
如果您想拥有新图像,它们需要具有与以前不同的名称。可能你的
face_ID和count总是一样的 -
@samorr 是的,这就是我的想法,但我有点坚持如何去做,而不必每次都更改代码
-
您需要为每个文件名添加另一个字符串。它可以是一个全局计数器,您可以将其值保存在一些辅助文件中,并且在添加新图像时始终将其递增。那么每张图片都会有唯一的名字。
-
所以每次添加新照片或新人物时都要重新训练?
标签: python opencv face-recognition