我希望将这两个代码组合起来只运行一次,当我将 save_detected_face.py 代码保存到jpg文件中时,检测到OpenCV CascadeClassifier,保存到jpg文件后,只要检测过程完成, train_save_face.py 就会自动运行。
这是我的示例代码: save_detected_face.py
import os
import urllib.request
import cv2
import numpy as np
from PIL import Image
path='dataset'
recognizer=cv2.face.LBPHFaceRecognizer_create();
def main():
#cap = cv2.VideoCapture("../index.htm?clientIpAddr=192.168.1.12&IsRemote=0")
a=0
cap = "http://192.168.1.43:8080/shot.jpg"
id = input('enter user id: ')
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
sampleNum=0
while True:
a = a+1
imgResp=urllib.request.urlopen(cap)
imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
img=cv2.imdecode(imgNp,-1)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
sampleNum = sampleNum+1
cv2.rectangle(img, (x,y),(x+w,y+h),(255,0,0),2)
# roi_gray= gray[y:y+h, x:x+w]
roi_color= img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_color)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])
cv2.imshow('frame', img)
# Training code
def getImagesWithID(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
for imagePath in imagePaths:
faceImg=Image.open(imagePath).convert('L');
faceNp=np.array(faceImg,'uint8')
ID=int(os.path.split(imagePath)[-1].split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow('training',faceNp)
cv2.waitKey(10)
return np.array(IDs), faces
Ids, faces=getImagesWithID(path)
recognizer.train(faces,Ids)
recognizer.save('recognizer/trainingData.yml')
if cv2.waitKey(100) & 0xff == ord('q'):
break
elif(sampleNum>20):
break
exit(0)
#
if __name__=='__main__':
main()
代码: train_save_face.py
import os
import cv2
import numpy as np
from PIL import Image
recognizer=cv2.face.LBPHFaceRecognizer_create();
path='dataset'
def getImagesWithID(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
for imagePath in imagePaths:
faceImg=Image.open(imagePath).convert('L');
faceNp=np.array(faceImg,'uint8')
ID=int(os.path.split(imagePath)[-1].split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow('training',faceNp)
cv2.waitKey(10)
return np.array(IDs), faces
Ids, faces=getImagesWithID(path)
recognizer.train(faces,Ids)
recognizer.save('recognizer/trainingData.yml')
exit(0)
print('done training')
save_detected_face.py工作正常,但是当我想训练面部时,它将无法正常工作。
答案 0 :(得分:0)
虽然我不确定这是否正确但现在正在运行。
# -*- coding: cp1252 -*-
import os
import urllib.request
import cv2
import numpy as np
from PIL import Image
recognizer=cv2.face.LBPHFaceRecognizer_create();
path='dataset'
def main():
#cap = cv2.VideoCapture("../index.htm?clientIpAddr=192.168.1.12&IsRemote=0")
a=0
cap = "http://192.168.1.43:8080/shot.jpg"
id = input('enter user id: ')
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
sampleNum=0
while True:
a = a+1
imgResp=urllib.request.urlopen(cap)
imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8)
img=cv2.imdecode(imgNp,-1)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
sampleNum = sampleNum+1
cv2.rectangle(img, (x,y),(x+w,y+h),(255,0,0),2)
# roi_gray= gray[y:y+h, x:x+w]
roi_color= img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_color)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])
cv2.imshow('frame', img)
if cv2.waitKey(100) & 0xff == ord('q'):
break
elif(sampleNum>20):
break
exit(0)
def getImagesWithID(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
for imagePath in imagePaths:
faceImg=Image.open(imagePath).convert('L');
faceNp=np.array(faceImg,'uint8')
ID=int(os.path.split(imagePath)[-1].split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow('training',faceNp)
cv2.waitKey(10)
return np.array(IDs), faces
Ids, faces=getImagesWithID(path)
recognizer.train(faces,Ids)
recognizer.save('recognizer/trainingData.yml')
exit(0)
print('done training')
#
if __name__=='__main__':
main()
getImagesWithID(path)