jQuery ajax请求在错误后继续运行

时间:2019-02-13 08:51:49

标签: javascript jquery html ajax html5

这是我的代码

early_face_detect=$.ajax({
type: "POST",
    url: "earlydetect.py",
    timeout: 15000,
    success: function(respond) { 
    var s=grab_early_details(respond);
},
error: function(xmlhttprequest, textstatus, message) {
    alert('close');
    }
});

发生错误后,它进入“错误”块,但在后台“ earlydetect.py”继续运行。一旦发生错误,是否有任何方法可以终止其执行?

这是我的Earlydetect.py代码段

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    while bool:
        # Grab a single frame of video
        ret, frame = video_capture.read()

        # Resize frame of video to 1/4 size for faster face recognition processing
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # Only process every other frame of video to save time
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition_api.face_locations(small_frame)
            face_encodings = face_recognition_api.face_encodings(small_frame, face_locations)

            face_names = []
            predictions = []
            if len(face_encodings) > 0:
                closest_distances = clf.kneighbors(face_encodings, n_neighbors=1)

                is_recognized = [closest_distances[0][i][0] <= 0.5 for i in range(len(face_locations))]

                # predict classes and cull classifications that are not with high confidence
                predictions = [(le.inverse_transform(int(pred)).title(), loc) if rec else ("Unknown", loc) for pred, loc, rec in
                               zip(clf.predict(face_encodings), face_locations, is_recognized)]

            # # Predict the unknown faces in the video frame
            # for face_encoding in face_encodings:
            #     face_encoding = face_encoding.reshape(1, -1)
            #
            #     # predictions = clf.predict(face_encoding).ravel()
            #     # person = le.inverse_transform(int(predictions[0]))
            #
            #     predictions = clf.predict_proba(face_encoding).ravel()
            #     maxI = np.argmax(predictions)
            #     person = le.inverse_transform(maxI)
            #     confidence = predictions[maxI]
            #     print(person, confidence)
            #     if confidence < 0.7:
            #         person = 'Unknown'
            #
            #     face_names.append(person.title())

        process_this_frame = not process_this_frame


        # Display the results
        for name, (top, right, bottom, left) in predictions:
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4

            # Draw a box around the face
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            # Draw a label with a name below the face
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
            if name!="Unknown":
                bool = False
                name_face = name
        # Display the resulting image
        cv2.imshow('Video', 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()

它有助于检测人脸,如果发现它会打印回接收到ajax请求的名称,否则会设置ajax超时,从而导致错误。但是即使超时后,earlydetect.py仍会继续执行

1 个答案:

答案 0 :(得分:0)

收到错误后必须中止

early_face_detect=$.ajax({
type: "POST",
    url: "earlydetect.py",
    timeout: 15000,
    success: function(respond) { 
    var s=grab_early_details(respond);
},
error: function(xmlhttprequest, textstatus, message) {
    alert('close');
    // Abort xmlhttprequest
    xmlhttprequest.abort();
    }
});