Flask SocketIO无法将消息发送到客户端

时间:2018-12-11 10:02:38

标签: python opencv flask socket.io keras

虽然很典型,但我确实很难将三个变量发送到客户端网页。我的项目是检测情绪,这种情绪将以每秒30帧的速度馈入keras cnn模型。这意味着,愤怒,悲伤和快乐的情绪百分比将保持每秒更新30次。因此,我正在使用Flask,websocketio和keras。

问题是,即使我发出了情绪愤怒(我先尝试仅一个变量),也无法输出到日志(我先尝试输出到控制台,但仍然失败)。请检查此代码,我无法将愤怒变量发送到客户端网页。已经尝试了stackoverflow中提供的几乎所有解决方案,但仍然失败。请注意,我的网页可以检测到网络摄像头,图像也可以输入到keras中。我什至在命令提示符下打印了预测,而且工作正常。

这是相机类, cam.py

import cv2
import numpy as np
#import time
from keras.models import load_model
import tensorflow as tf
import keras.backend as K
#global graph, model
#graph = tf.get_default_graph()
config = tf.ConfigProto()
config.gpu_options.allow_growth =True
sess = tf.Session(config=config)
cascade = cv2.CascadeClassifier('D:\\Code\\haarcascade_frontalface_default.xml')
modelPath = 'D:\\Code\\model 32 7 sigmoid 0.3 0.005.hdf5'
model = load_model(modelPath)
model._make_predict_function()
class vid():
def __init__(self):
    self.cam = cv2.VideoCapture(-1)
    self.cam.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
    self.cam.set(cv2.CAP_PROP_FRAME_HEIGHT,720)
    self.evalPredict = [0,0,0]
def __del__(self):
    self.cam.release()

def getFrame(self):
    work, img = self.cam.read()
    img = cv2.flip(img,1)
    self.gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    self.face = cascade.detectMultiScale(self.gray,scaleFactor=1.5,minNeighbors=5)
    for (x,y,w,h) in self.face:
        self.roi = self.gray[y:y+h,x:x+w]
        self.color = (255,255,0)
        self.stroke = 3
        self.endX = x+w
        self.endY = y+h
        img = cv2.rectangle(img,(x,y),(self.endX,self.endY),self.color,self.stroke)
        self.eval = self.evaluate(self.roi)
        self.evalPredict = self.eval
        #print(self.evalPredict)
    ret,jpeg = cv2.imencode('.jpeg',img)
    return jpeg.tobytes()

def evaluate(self,img):
    img = cv2.resize(img,(128,128))
    img = np.array(img)
    self.predict = self.prediction(img)
    self.predict = self.predict[0]
    return self.predict

def prediction(self,roi):
    self.predict = model.predict(roi.reshape(1,128,128,1))
    #K.clear_session()
    return self.predict

这是 main.py

from flask import Flask, render_template, Response
from cam import vid
from flask_socketio import SocketIO, send,emit
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)

app.config['SECRET_KEY'] ='secret'
socketio = SocketIO(app,message = 'http://127.0.0.1:500',async_mode='threading')
@app.route('/')
def index():
   return render_template('index.html')

def gen(cam):
   while True:
      frame = cam.getFrame()
      anger = cam.evalPredict[0]
      happy = cam.evalPredict[1]
      sad = cam.evalPredict[2]
      yield(b'--frame\r\n'
        b'Content-Type: image/jpeg\r\n\r\n'+frame+b'\r\n\r\n')
      handleMessage(str(anger))

@app.route('/video')

def video():
   return Response(gen(vid()),
            mimetype='multipart/x-mixed-replace; boundary=frame')
@socketio.on('message')
def handleMessage(anger):
   with app.test_request_context('/'):
      emit('Anger Response',anger,namespace='/anger',broadcast=True)
if __name__ == '__main__':
   socketio.run(app,debug = True)

这是 index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
   <meta charset="utf-8">
   <title>MICREX</title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.dev.js" charset="utf-8"></script>
   <script
   src="https://code.jquery.com/jquery-3.3.1.js"
   integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
   crossorigin="anonymous"></script>
  </head>
  <body>
   <script type="text/javascript">
     $(document).ready(function(){
       var socket = io.connect('http://'+document.domain+':'+location.port+'/anger');

    //socket.on('message',function(a){
      socket.on('Anger Response',function(a){
          $("#h2").html(a);
          console.log(a)
     });

    //});
    $('#h2').html('Working');
  });
</script>
<h1>Video Stream</h1>
<img src="{{ url_for('video') }}" alt="">
<h2 id="h2">ABC</h2>

0 个答案:

没有答案