首页 > 解决方案 > 使用 python 控制 ESP32 的问题

问题描述

有谁知道我的代码出了什么问题?我正在尝试使用 Python 来控制 esp32cam,但我不知道出了什么问题。

抱歉,我不确定我是否正确发布此内容,因为这是我第一次询问!这是我的代码。

在 Dr.Casual 提供帮助后,我更改了代码(非常感谢!),但我仍然遇到错误。更新了代码。

 import cv2 as cv
 import numpy as np
 import urllib.request


# change to your ESP32-CAM ip

 url = 'http://192.168.99.119/'       # hi or lo
 winName = 'CAM'
 cv.namedWindow("winName")

 while 1:
  imgResponse = urllib.request.urlopen(url)
  imgNp = np.array(bytearray(imgResponse.read()), dtype=np.uint8)
  img = cv.imdecode(imgNp, -1)

 cv.imshow("winName", img)
 tecla = cv.waitKey(5) & 0xFF
 if tecla == 27:
    break


 cv.destroyALLWINDOWS()

错误代码如下

        Traceback (most recent call last):
  File "/Users/fangyaoting/PycharmProjects/pythonProject/TSET.py", line 10, in <module>
cv.namedWindow("winName")
AttributeError: module 'cv2' has no attribute 'namedWindow'

标签: python

解决方案


尝试这个:

import cv2 as cv
import numpy as np
import urllib.request


# change to your ESP32-CAM ip

url = 'http://192.168.99.10/cam-lo.jpg'       #hi or lo
winName = 'CAM'
cv.namedWindow("winName")

while 1:
  imgResponse = urllib.request.urlopen(url)
  imgNp=np.array(bytearray(imgResponse.read()),dtype=np.uint8)
  img=cv.imdecode(imgNp, -1)

  cv.imshow("winName",img)
  tecla = cv.waitKey(5) & 0xFF
  if tecla == 27:
    break


cv.destroyALLWINDOWS()

在 python 中,缩进非常重要:)

编辑:请注意,我已更改以下内容

while 1:
  imgResponse = urllib.request.urlopen(url)
  imgNp=np.array(bytearray(imgResponse.read()),dtype=np.uint8)
  img=cv.imdecode(imgNp, -1)

缩进时,空格应始终为 2(或 4)。您使用了 1,这会导致语法错误。

也缩进了这一点,因为这似乎应该是循环的一部分:

  cv.imshow("winName",img)
  tecla = cv.waitKey(5) & 0xFF
  if tecla == 27:
    break

推荐阅读