无法从OpenCVPython访问WifiCam

2024-06-16 08:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个名为Esp32 Cam的Wifi摄像头,并试图用openCv连接到它。Node server.js代码可以通过模块的IP地址连接到模块。server.js的代码如下所示:

SERVER.JS:

  const path = require('path');
  const express = require('express');
  const WebSocket = require('ws');
  const app = express();

  const WS_PORT  = 8888;
  const HTTP_PORT = 8000;

  const wsServer = new WebSocket.Server({port: WS_PORT}, ()=> console.log(`WS Server is listening at 
  ${WS_PORT}`));

  let connectedClients = [];
  wsServer.on('connection', (ws, req)=>{
  console.log('Connected');
  connectedClients.push(ws);

   ws.on('message', data => {
    connectedClients.forEach((ws,i)=>{
        if(ws.readyState === ws.OPEN){
            ws.send(data);
        }else{
            connectedClients.splice(i ,1);
        }
    })
});
});

app.get('/client',(req,res)=>res.sendFile(path.resolve(__dirname, './client.html')));
app.listen(HTTP_PORT, ()=> console.log(`HTTP server listening at ${HTTP_PORT}`));  

CLİENT.HTML:

 <html>
<head>
    <title>Client</title>
</head>
<body>
    <img src="">
    <script>
        const img = document.querySelector('img');
        const WS_URL = 'ws:///192.168.1.33:8888';
        const ws = new WebSocket(WS_URL);
        let urlObject;
        ws.onopen = () => console.log(`Connected to ${WS_URL}`);
        ws.onmessage = message => {
            const arrayBuffer = message.data;
            if(urlObject){
                URL.revokeObjectURL(urlObject);
            }
            urlObject = URL.createObjectURL(new Blob([arrayBuffer]));
            img.src = urlObject;
        }
    </script>
</body>

我可以使用浏览器访问地址“http://192.168.1.33:8000/client”上的流式视频,但不能在opencv上访问。下面是opencvpython中试图到达摄像机的代码

OPENCV代码:

cap = cv2.VideoCapture('http://192.168.1.33:8000/client')
print(cap.isOpened())  //it prints FALSE
while(True):
    ret, frame = cap.read()
    cv2.imshow('framee',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
       cv2.destroyAllWindows()
       break

我不明白为什么我不能访问摄像头,因为这个esp32模块就像一个IP摄像头?我将感谢任何帮助

多谢各位


Tags: 代码clientloghttpurlimgwsport