0

嗨,我正在开发一个进行人脸识别的烧瓶服务器,为了将人添加到数据库中,我制作了一个脚本,如果满足条件,它会从网络摄像头拍摄 30 张照片,这是我的脚本

TOTAL = 0
shape_predictor= "shape_predictor_68_face_landmarks.dat" #dace_landmark
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(shape_predictor)

(jStart,jEnd) = face_utils.FACIAL_LANDMARKS_IDXS["jaw"]
(nStart,nEnd)= face_utils.FACIAL_LANDMARKS_IDXS["nose"]

print("[INFO] starting video stream thread...")
vs = VideoStream(src=0).start()
fileStream = False
time.sleep(1.0)
fps= FPS().start()
cv2.namedWindow("test")

while True:
    frame = vs.read()        
    frame = imutils.resize(frame, width=450)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 0)
    for rect in rects:
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)            
        jaw = shape[jStart:jEnd]            
        nose = shape[nStart:nEnd]

        l1= math.sqrt((jaw[0][0]-nose[1][0])**2+(jaw[0][1]-nose[1][1])**2)
        l2 = math.sqrt((jaw[16][0]-nose[1][0])**2+(jaw[16][1]-nose[1][1])**2)
        ratio = l1/l2
        m = (jaw[0][1]-jaw[16][1])/(jaw[0][0]-jaw[16][0])

        if (ratio <= 1.1 or ratio > .9) & (jaw[0][1]==nose[1][1]) &(m <= .05 or m >-.05 ):
            cond = True
        else:
            cond= False

        jawhHull = cv2.convexHull(jaw)
        noseHull = cv2.convexHull(nose)       
        cv2.drawContours(frame, [jawhHull, noseHull], -1, (0, 255, 0), 1)            
        if cond == True:
            TOTAL += 1
            frame = vs.read()
            time.sleep(.1)
            frame2= frame.copy()
            img_name = "opencv_frame_{}.png".format(TOTAL)
            cv2.imwrite(img_name, frame)
            print("{} written!".format(img_name))

    cv2.imshow("Frame", frame)
    fps.update()
    if TOTAL == 30:
        break
    key2 = cv2.waitKey(1) & 0xFF
    if key2 == ord('q'):
        break
fps.stop()
cv2.destroyAllWindows()
vs.stop()

我的烧瓶 main.py 看起来像这样

app = Flask(__name__)                                  
@app.route('/face_rec', methods=['POST'])
def face_recognition():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' in request.files:
            file = request.files.get('file')                          
            dict_res = face_rec(file)       
            return json.dumps(dict_res)
app.run(host='0.0.0.0', port='5001', debug=True)

face_rec 路由是用于人脸验证的 api,所以我的问题是如何在弹出的 opencv 窗口中流式传输网络摄像头,但在烧瓶路由中可以说 /enrollment ?

我试过这个教程

https://medium.com/datadriveninvestor/video-streaming-using-flask-and-opencv-c464bf8473d6

但根本不明白,但我不知道这个模板是否是一个好的起点?

<html>
  <head>
    <title>Video Streaming Demonstration</title>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
    <img id="bg" src="{{ url_for( enrollment') }}">
  </body>
</html>

提前感谢您的帮助

4

1 回答 1

0

完成它我更改了 main.py 以使其看起来像这样:

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')  

def gen():

    TOTAL = 0  
    shape_predictor= "shape_predictor_68_face_landmarks.dat" #dace_landmark
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(shape_predictor)  
    (jStart,jEnd) = face_utils.FACIAL_LANDMARKS_IDXS["jaw"]
    (nStart,nEnd)= face_utils.FACIAL_LANDMARKS_IDXS["nose"]

    vs = VideoStream(src=0).start()
    fileStream = False
    time.sleep(1.0)
    fps= FPS().start()
    while True:

        frame = vs.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        rects = detector(gray, 0)
        for rect in rects:

            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)
            jaw = shape[jStart:jEnd]
            nose = shape[nStart:nEnd]

            l1= math.sqrt((jaw[0][0]-nose[1][0])**2+(jaw[0][1]-nose[1][1])**2)
            l2 = math.sqrt((jaw[16][0]-nose[1][0])**2+(jaw[16][1]-nose[1][1])**2)
            ratio = l1/l2
            m = (jaw[0][1]-jaw[16][1])/(jaw[0][0]-jaw[16][0])

            if (ratio <= 1.1 and ratio > .9) :
                cond1 = True
            else:
                cond1= False
            if (jaw[0][1]<= (nose[1][1]+5) and jaw[0][1]> (nose[1][1]-5)):
                cond2 = True
            else:
                cond2= False
            if (m <= .05 and m >-.05 ):
                cond3 = True
            else:
                cond3= False

            jawhHull = cv2.convexHull(jaw)
            noseHull = cv2.convexHull(nose)                   

            if cond1 == True & cond2 == True & cond3 == True:
                TOTAL += 1
                frame = vs.read()                    
                time.sleep(.01)
                frame2= frame.copy()
                img_name = "pics/opencv_frame_{}.png".format(TOTAL)
                cv2.imwrite(img_name, frame2)
                print("{} written!".format(img_name), end="\r")

            if cond1 == False: cv2.putText(frame, "La nariz no se encuentra centrada", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 
            if cond3 == False: cv2.putText(frame, "el angulo de la sien no esta alineado", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
            if cond2 == False: cv2.putText(frame, "quijada no alineada con la nariz", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
            cv2.drawContours(frame, [mouthHull,left_eyeHull, right_eyehHull, jawhHull, right_eyebrowHull, noseHull, left_eyebrowHull], -1, (0, 255, 0), 1)
            cv2.putText(frame, "{} por ciento completado".format(TOTAL), (200, 350), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

            image = cv2.rectangle(frame, (150,50), (550,400), (150, 150, 50), 2) 

        fps.update()
        if TOTAL == 101:
            break
        key2 = cv2.waitKey(1) & 0xFF
        if key2 == ord('q'):
            break            
        (flag, encodedImage) = cv2.imencode(".jpg", frame)
        yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')
    fps.stop()
    cv2.destroyAllWindows()
    vs.stop()

@app.route('/video_feed')
def video_feed():
    return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    # defining server ip address and port
    app.run(host='0.0.0.0',port='5000', debug=True)

它工作得很好。

如果有任何可以帮助我让它在一个单独的文件中工作,比如 camera.py 并将它放在 main.py 上,我将非常感激

于 2020-05-21T15:37:17.440 回答