释放通过opencv python访问的相机

我正在尝试将摄像机的帧流传输到flask api,并对其进行处理以检测所有帧中的面部并将其保存到磁盘。我的代码在这里,

def face(video_capture):

    # Grab the list of names and the list of encodings
    known_face_ids = list(all_face_encodings.keys())
    known_face_encodings = np.array(list(all_face_encodings.values()))

    while True:

        process_this_frame = True
        ret,frame = video_capture.read()

        small_frame = cv2.resize(frame,(0,0),fx=0.25,fy=0.25)

        rgb_small_frame = small_frame[:,:,::-1]

        if process_this_frame:
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame,face_locations)

            names_present = []
            for face_encoding in face_encodings:
                matches = face_recognition.compare_faces(known_face_encodings,face_encoding)
                name = "Unknown"

                if True in matches:
                    first_match_index = matches.index(True)
                    name = known_face_ids[first_match_index]
                names_present.append(name)

        process_this_frame = not process_this_frame

        # Hit 'q' on the keyboard to quit!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release handle to the webcam
    video_capture.release()
    cv2.destroyAllWindows()

    return names_present

@app.route('/start_matching',methods=['POST'])
def attendance():

    if request.method == 'POST':
        if 'camera_id' not in request.args:
            return 'Please provide the camera details'

        camera_id = request.args.get('camera_id')
        video_capture = cv2.VideoCapture(int(camera_id))
        names_present = face(video_capture)

我正在尝试让前端具有开始和停止按钮。 “开始”应将上述api称为“开始匹配”。这样可以很好地工作并捕获流,将其拆分为帧,然后将编码与已知编码进行比较以找出其中存在的名称。但是,“停止”应停止将相机流式传输到上述api,并返回“ names_present”。

我尝试了另一个api调用

@app.route('/stop_matching',methods=['POST'])
def stop_attendance():
    camera_id = request.args.get('camera_id')
    video_capture = cv2.VideoCapture(int(camera_id))
    video_capture.release()
    return "Stopped the camera"

但是它不会停止相机并且不会返回“ names_present”列表。谁能指出我正确的方向?

RERR555 回答:释放通过opencv python访问的相机

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3130767.html

大家都在问