如何在Flask-SocketIO

问题描述

我正在使用Flask-SocketIO开发用于人脸识别的Flask应用程序。我想在后台启动face_recognition应用程序,当识别到人时,它将名称从服务器发送给JS客户端。当我在app.py文件中导入face_rec.py文件时,出现以下错误错误

Using TensorFlow backend.
Server initialized for gevent.
Traceback (most recent call last):
  File "E:/Innovo/face_recog/Recoginition/app.py",line 77,in <module>
    socketio.run(app)
  File "C:\ProgramData\Anaconda3\lib\site-packages\flask_socketio\__init__.py",line 609,in run
    monkey.patch_thread()
  File "C:\ProgramData\Anaconda3\lib\site-packages\gevent\monkey.py",line 178,in ignores
    return func(*args,**kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\gevent\monkey.py",line 588,in patch_thread
    _patch_existing_locks(threading_mod)
  File "C:\ProgramData\Anaconda3\lib\site-packages\gevent\monkey.py",line 487,in _patch_existing_locks
    if isinstance(o,rlock_type):
ReferenceError: weakly-referenced object no longer exists

我的代码是这个 app.py:

import faceRec_wihtout_mask
from flask_socketio import SocketIO,emit
from flask import Flask,render_template,url_for
from random import random
from time import sleep
from threading import Thread,Event 

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
#turn the flask app into a socketio app
socketio = SocketIO(app,async_mode=None,logger=True,engineio_logger=True)


thread = Thread()
thread_stop_event = Event()

def face_rec():
    while not thread_stop_event.isSet():
        faceRec_wihtout_mask.main('video')
        



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

@socketio.on('connect',namespace='/test')
def test_connect():
    # need visibility of the global thread object
  global thread
    print('Client connected')
    

    if not thread.isAlive():
        print("Starting Thread")
        thread = socketio.start_background_task(face_rec)

@socketio.on('disconnect',namespace='/test')
def test_disconnect():
    print('Client disconnected')


if __name__ == '__main__':
    socketio.run(app)




解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)