import sensor, image, time, network, usocket, sys, pyb, math
from pyb import UART
import ustruct
x_threshold = (0, 50, -24,-1, -18, 6)
led = pyb.LED(3)
usb = pyb.USB_VCP()
while(not usb.isconnected()):
led.on()
time.sleep_ms(150)
led.off()
time.sleep_ms(100)
led.on()
time.sleep_ms(150)
led.off()
time.sleep_ms(600)
break
SSID ='OPENMV_AP' # Network SSID
KEY ='1234567890' # wifi密码(必须为10字符)
HOST = '' # 使用第一个可用的端口
PORT = 8080 # 任意非特权端口
# 重置传感器
sensor.reset()
sensor.set_contrast(1)
sensor.set_brightness(1)
sensor.set_saturation(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.skip_frames(10)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
# 在AP模式下启动wlan模块。
wlan = network.WINC(mode=network.WINC.MODE_AP)
wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2)
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
#您可以阻止等待客户端连接
#print(wlan.wait_for_sta(10000))
def start_streaming(s):
print ('Waiting for connections..')
client, addr = s.accept()
# 将客户端套接字超时设置为2秒
client.settimeout(2.0)
print ('Connected to ' + addr[0] + ':' + str(addr[1]))
# 从客户端读取请求
data = client.recv(1024)
# 应该在这里解析客户端请求
# 发送多部分head
client.send("HTTP/1.1 200 OK\r\n" \
"Server: OpenMV\r\n" \
"Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n" \
"Cache-Control: no-cache\r\n" \
"Pragma: no-cache\r\n\r\n")
# FPS clock
clock = time.clock()
# 开始流媒体图像
#注:禁用IDE预览以增加流式FPS。
while (True):
clock.tick() # 跟踪snapshots()之间经过的毫秒数。
frame = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.0)
blobs = frame.find_blobs([x_threshold], pixels_threshold=500, area_threshold=1550)
if blobs:
for b in blobs:
FH = bytearray([0xb3, 0xb3])
uart.write(FH)
img.draw_rectangle(b[0:4])
img.draw_cross(b[5], b[6])
print(b[5], b[6])
x = b.cx()
y = b.cy()
print(x, y)
data=bytearray([x, y, 0x1])
uart.write(data)
print(clock.fps())
print(data)
cframe = frame.compressed(quality=35)
header = "\r\n--openmv\r\n" \
"Content-Type: image/jpeg\r\n"\
"Content-Length:"+str(cframe.size())+"\r\n\r\n"
client.send(header)
client.send(cframe)
print(clock.fps())
while (True):
# 创建服务器套接字
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
try:
# Bind and listen
s.bind([HOST, PORT])
s.listen(5)
# 设置服务器套接字超时
# 注意:由于WINC FW bug,如果客户端断开连接,服务器套接字必须
# 关闭并重新打开。在这里使用超时关闭并重新创建套接字。
s.settimeout(3)
start_streaming(s)
except OSError as e:
s.close()
print("socket error: ", e)
#sys.print_exception(e)
L
l3kf 发布的帖子
-
加入了寻找色块的代码后,无线图传就出问题了,进不去或者进去就卡住了
-
帮我看看这代码有什么问题,wifi图传卡在这页面进不去,进去也没图像
import sensor, image, time, network, usocket, sys, pyb, math from pyb import UART import ustruct x_threshold = (0, 50, -24,-1, -18, 6) led = pyb.LED(3) usb = pyb.USB_VCP() while(not usb.isconnected()): led.on() time.sleep_ms(150) led.off() time.sleep_ms(100) led.on() time.sleep_ms(150) led.off() time.sleep_ms(600) break SSID ='OPENMV_AP' KEY ='1234567890' HOST = '' PORT = 8080 sensor.reset() sensor.set_contrast(1) sensor.set_brightness(1) sensor.set_saturation(1) sensor.set_gainceiling(16) sensor.set_vflip(True) sensor.set_hmirror(True) sensor.set_framesize(sensor.QVGA) sensor.set_pixformat(sensor.RGB565) sensor.skip_frames(10) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) sensor.set_auto_whitebal(False) # 在AP模式下启动wlan模块。 wlan = network.WINC(mode=network.WINC.MODE_AP) wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2) #您可以阻止等待客户端连接 #print(wlan.wait_for_sta(10000)) def start_streaming(s): print ('Waiting for connections..') client, addr = s.accept() # 将客户端套接字超时设置为2秒 client.settimeout(2.0) print ('Connected to ' + addr[0] + ':' + str(addr[1])) # 从客户端读取请求 data = client.recv(1024) # 应该在这里解析客户端请求 # 发送多部分head client.send("HTTP/1.1 200 OK\r\n" \ "Server: OpenMV\r\n" \ "Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n" \ "Cache-Control: no-cache\r\n" \ "Pragma: no-cache\r\n\r\n") # FPS clock clock = time.clock() uart = UART(3, 115200) uart.init(115200, bits=8, parity=None, stop=1) # 开始流媒体图像 #注:禁用IDE预览以增加流式FPS。 while (True): clock.tick() frame = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.0) blobs = frame.find_blobs([x_threshold], pixels_threshold=500, area_threshold=1550) if blobs: for b in blobs: frame.draw_rectangle(b[0:4]) frame.draw_cross(b[5], b[6]) print(b[5], b[6]) x = b.cx() y = b.cy() print(x, y) TOU = bytearray([0xb3, 0xb3]) uart.write(TOU) data=bytearray([x, y, 1]) uart.write(data) print(clock.fps()) print(data) cframe = frame.compressed(quality=35) header = "\r\n--openmv\r\n" \ "Content-Type: image/jpeg\r\n"\ "Content-Length:"+str(cframe.size())+"\r\n\r\n" client.send(header) client.send(cframe) print(clock.fps()) while (True): # 创建服务器套接字 s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) try: # Bind and listen s.bind([HOST, PORT]) s.listen(5) # 设置服务器套接字超时 # 注意:由于WINC FW bug,如果客户端断开连接,服务器套接字必须 # 关闭并重新打开。在这里使用超时关闭并重新创建套接字。 s.settimeout(3) start_streaming(s) except OSError as e: s.close() print("socket error: ", e) #sys.print_exception(e)
-
怎么把这两个代码合起来,而且wifi图传和颜色识别功能都能用啊
from pyb import UART import json x_threshold = (0, 50, -24,-1, -18, 6) led = pyb.LED(3) usb = pyb.USB_VCP() while(not usb.isconnected()): led.on() time.sleep_ms(150) led.off() time.sleep_ms(100) led.on() time.sleep_ms(150) led.off() time.sleep_ms(600) break sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(10) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) sensor.set_auto_whitebal(False) clock = time.clock() uart = UART(3, 115200) uart.init(115200, bits=8, parity=None, stop=1) while(True): clock.tick() img = sensor.snapshot().lens_corr(strength = 1.6, zoom = 1.0) blobs = img.find_blobs([x_threshold], pixels_threshold=500, area_threshold=1550) if blobs: for b in blobs: FH = bytearray([0xb3, 0xb3]) uart.write(FH) img.draw_rectangle(b[0:4]) img.draw_cross(b[5], b[6]) print(b[5], b[6]) x = b.cx() y = b.cy() print(x, y) data=bytearray([x, y, 0x1]) uart.write(data) print(clock.fps()) print(data) 请在这里粘贴代码 ```import sensor, image, time, network, usocket, sys SSID ='OPENMV_AP' # Network SSID KEY ='1234567890' # wifi密码(必须为10字符) HOST = '' # 使用第一个可用的端口 PORT = 8080 # 任意非特权端口 # 重置传感器 sensor.reset() # 设置传感器设置 sensor.set_contrast(1) sensor.set_brightness(1) sensor.set_saturation(1) sensor.set_gainceiling(16) sensor.set_framesize(sensor.QQVGA) sensor.set_pixformat(sensor.GRAYSCALE) # 在AP模式下启动wlan模块。 wlan = network.WINC(mode=network.WINC.MODE_AP) wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2) #您可以阻止等待客户端连接 wlan.wait_for_sta(10000) def start_streaming(s): print ('Waiting for connections..') client, addr = s.accept() # 将客户端套接字超时设置为2秒 client.settimeout(2.0) print ('Connected to ' + addr[0] + ':' + str(addr[1])) # 从客户端读取请求 data = client.recv(1024) # 应该在这里解析客户端请求 # 发送多部分head client.send("HTTP/1.1 200 OK\r\n" \ "Server: OpenMV\r\n" \ "Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n" \ "Cache-Control: no-cache\r\n" \ "Pragma: no-cache\r\n\r\n") # FPS clock clock = time.clock() # 开始流媒体图像 #注:禁用IDE预览以增加流式FPS。 while (True): clock.tick() # 跟踪snapshots()之间经过的毫秒数。 frame = sensor.snapshot() cframe = frame.compressed(quality=35) header = "\r\n--openmv\r\n" \ "Content-Type: image/jpeg\r\n"\ "Content-Length:"+str(cframe.size())+"\r\n\r\n" client.send(header) client.send(cframe) print(clock.fps()) while (True): # 创建服务器套接字 s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) try: # Bind and listen s.bind([HOST, PORT]) s.listen(5) # 设置服务器套接字超时 # 注意:由于WINC FW bug,如果客户端断开连接,服务器套接字必须 # 关闭并重新打开。在这里使用超时关闭并重新创建套接字。 s.settimeout(3) start_streaming(s) except OSError as e: s.close() print("socket error: ", e) #sys.print_exception(e)