在烧瓶中处理文件而不退出

时间:2020-05-06 17:49:38

标签: python python-3.x opencv flask

这是对上传文件进行处理的烧瓶代码:

        if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(file_path)
        print(file_path)
        print("File saved successfully")

        workonthefilefunction(file_path)
        flash('File successfully uploaded')
        return redirect('/')

    else:
        flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
        return redirect(request.url)

if __name__ == "__main__":
    app.run(port=5555, debug=True)

这是正在处理文件的opencv部分:

  with detection_graph.as_default():
    with tf.compat.v1.Session(graph=detection_graph) as sess:
        #while True:
            # Read frame from camera
            while cap.isOpened():
                ret, image_np = cap.read()
                print(ret)

                if not ret:
                    break
                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Extract image tensor
                image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
                # Extract detection boxes
                boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
                # Extract detection scores
                scores = detection_graph.get_tensor_by_name('detection_scores:0')
                # Extract detection classes
                classes = detection_graph.get_tensor_by_name('detection_classes:0')
                # Extract number of detectionsd
                num_detections = detection_graph.get_tensor_by_name(
                    'num_detections:0')
                # Actual detection.
                (boxes, scores, classes, num_detections) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})
                # Visualization of the results of a detection.
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
                # print([category_index.get(i) for i in classes[0]])
                # print(scores)
                # Display output
                cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))

                if cv2.waitKey(25) & 0xFF == ord('q'):
                    cv2.destroyAllWindows()
                    break


            cap.release()
            cv2.destroyAllWindows

在处理完文件后,flask服务器退出,因此停止了文件上传所需的Web服务,并且将要求再次执行命令,部署后将无法执行该命令。如何防止Flask在处理上传的文件后停止并安全退出,并且仍然保持Flask应用程序运行,以便用户可以上传新文件,而不必再次手动重新启动服务器。

编辑:我可以将文件监视程序作为单独的flask脚本来执行,该脚本检查文件夹中是否有新文件并进行处理,但是我还希望用户立即查看结果,因此我必须将监视程序添加到原始脚本中它将让我执行一些数据库操作,但是我想知道是否还有其他选择。

0 个答案:

没有答案