python - 使用 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'
解决方案
尝试这个:
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
推荐阅读
- sql - INNER JOIN 和计数 POSTGRESQL
- javascript - 闪亮的反应性 JS 元素
- design-patterns - 在 Rust 中传递类型的惯用方式是什么?
- typescript - Adonisjs 上次更新(v5)模型创建命令不起作用
- python - 在 Python 中拆分 test-val 数据的“种子”是什么以及如何得出正确的数字?
- spring - 基于多态类结构的 Spring Cloud Stream 消息
- r - GGplot 绘制每个点两次
- dataframe - 如何添加多个数据框加入如何
- python - 如何从巨大的 txt 文件中删除/删除与文本匹配的特定行
- xml - 在火花中读取多个xml文件时获取文件名