我正在编写一个实时面部识别应用程序,该应用程序可以捕获未知的面部,但是图像保存似乎与网络摄像头上的流一起加载。或者,如果对未知的面部识别有任何想法和建议,请推荐。任何想法都欢迎。或实时人脸识别API,可以自定义并扩展到个人安全应用程序中。谢谢
import cv2
import subprocess
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
recognizer = cv2.face.createLBPHFaceRecognizer()
recognizer.load("trainer/trainer.yml")
font = cv2.FONT_HERSHEY_SIMPLEX
row=['',"Adam","Eve"] #names according to face dataset id
class VideoCamera(object): #for streaming on web
def __init__(self):
# Using OpenCV to capture from device 0. If you have trouble capturing
# from a webcam, comment the line below out and use a video file
# instead.
self.video = cv2.VideoCapture(0)
# If you decide to use video.mp4, you must have this file in the folder
# as the main.py.
# self.video = cv2.VideoCapture('video.mp4')
def __del__(self):
self.video.release()
def get_frame(self):
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
#initiate id counter
id = 0
# Define min window size to be recognized as a face
minW = 0.1*self.video.get(3)
minH = 0.1*self.video.get(4)
ret, img =self.video.read()
img = cv2.flip(img, 1) # Flip vertically
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 5,
minSize = (int(minW), int(minH)),
)
for(x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
# Check if confidence is less them 100 ==> "0" is perfect match
if (confidence < 100):
name = row[id]
confidence = " {0}%".format(round(100 - confidence))
else:
name = "unknown"
confidence = " {0}%".format(round(100 - confidence))
count=0
while(True) :
count +=1
cv2.imwrite("path" + str(count) + ".jpg", gray[y:y+h,x:x+w])
cv2.imshow('image', img)
if count >= 20:
break
cv2.putText(img, str(name), (x+5,y-5), font, 1, (255,255,255), 2)
cv2.putText(image, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1)
ret, jpeg = cv2.imencode('.jpg', img)
return jpeg.tobytes()
我希望获得的是捕获一次被检测到的陌生人的图像,同时我们可以观察网络上的实时流。