0

大家好,我有一台在 Python 脚本上运行 Deepface 的机器,它应该将结果提供给客户端网站。我目前在我的 Python 服务器上收到“代码 = 1006(连接异常关闭 [内部]),没有原因”,我有点迷路了。这一切都非常简单,我想我错过了一些非常微不足道的东西,因此我再次在这里寻求您的帮助。

Python:

#!/usr/bin/env python

import asyncio
import websockets
import sys
import numpy as np

import cv2
import matplotlib.pyplot as plt
from deepface import DeepFace
from time import time


async def start(websocket, path):
    address = await websocket.recv()
    worry_counter = 0
    worry_index = 0
    top_par = 3.47
    bot_par = 1.86
    frame = []
    Capture = []
    ThreadActive = True
    Capture = cv2.VideoCapture(0)
    start = time()
    while ThreadActive:
        ret, frame = Capture.read()
        if ret:

            result = DeepFace.analyze(
                frame, actions=["emotion"], enforce_detection=False
            )
            emotions = result["emotion"]
            worry = (
                1.4 * emotions["angry"]
                + 0.9 * emotions["disgust"]
                + 1.2 * emotions["fear"]
                + 1.43 * emotions["sad"]
            ) - (0.83 * emotions["happy"])
            if worry < 0:
                worry = 0

            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            font = cv2.FONT_HERSHEY_SIMPLEX
            current_time = time()
            if current_time - start <= 5:
                worry_counter += 1
                worry_index += worry
                print(worry_index)
            else:
                res_emotion = (
                    100
                    if (top_par * worry_index) / (worry_counter * bot_par) > 100
                    else (top_par * worry_index) / (worry_counter * bot_par)
                )
                ThreadActive = False
    await websocket.send(str(res_emotion))


async def main():
    async with websockets.serve(start, "localhost", 8888, ping_interval=None):
        await asyncio.Future()  # run forever


asyncio.run(main())

HTML+JS 客户端:

<!DOCTYPE html>
<html>

<head>

    <script src="Dependencies/jquery.js"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="Dependencies/bootstrap-4.3.1-dist/css/bootstrap.css">

    <!-- Latest compiled and minified JavaScript -->
    <script src="Dependencies/bootstrap-4.3.1-dist/js/bootstrap.js"></script>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="Brigometar" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="style.css">

</head>

<body>
    <div id="outer" class="container">
        <div id="inner">
            <a href="ai.html" onclick="startPressed()" class="btn btn-light btn-lg active btn-block" role="button"
                aria-pressed="true">Start</a>
        </div>
    </div>
    <script>

        const ws = new WebSocket("ws://localhost:8888");
        function startPressed() {
            ws.send("172.132.1.1");
        }

        ws.onmessage = function (event) {
            console.log(event);
        };
    </script>
</body>
<script src="/Dependencies/jquery.js"></script>

</html>

输出:

0.6214318384147515
1.407929039009777
1.9553925836934325
2.7462658132474664
3.7744760031809257
4.998507034718125
5.707205323681767
6.070567907393982
6.440335301018088
6.736555141017516
7.1465979382828575
8.21655880781684
10.094864830947675
15.973965548303894
26.798210105962546
50.4165441135131
91.27202742432459
127.46092472774916
141.52072348938927
142.39793139643177
142.42815203136354
142.43325100650938
142.44452093058456
144.96232798644186
286.10208014206535
425.14424522610705
557.2168636684308
675.9000562773901
801.5925986208542
935.1956495200236
1074.3234839624008
1214.17933749819
1354.03544174955
1493.9019048591504
1633.6058058747064
1773.3901160597632
1913.2601424651782
2052.9667237286253
2192.7430928386766
Error in closing handshake
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/websockets/server.py", line 199, in handler
    await self.close()
  File "/usr/lib/python3/dist-packages/websockets/protocol.py", line 676, in close
    await asyncio.wait_for(
  File "/usr/lib/python3.9/asyncio/tasks.py", line 481, in wait_for
    return fut.result()
  File "/usr/lib/python3/dist-packages/websockets/protocol.py", line 1101, in write_close_frame
    await self.write_frame(True, OP_CLOSE, data, _expected_state=State.CLOSING)
  File "/usr/lib/python3/dist-packages/websockets/protocol.py", line 1083, in write_frame
    await self.ensure_open()
  File "/usr/lib/python3/dist-packages/websockets/protocol.py", line 803, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason
4

0 回答 0