logo

WIFI图传代码里有个小循环,一直循环发视频流,用了WIFI后该怎么用其他功能?试过直接加进去但不知道为啥图传就没了?

2022-11-23 14:26
F
xvwt
if gjz>2 :
                imggggg = sensor.snapshot() # Take a picture and return the image.
                yellow_thresholddddd   =(5, 50, 8, 130, 0,130)  #设置颜色的阈值
                blobsssss = imggggg.find_blobs([yellow_thresholddddd])
                if len(blobsssss) == 1:     #如果视野中只识别到一个目标(一个色块)

    #    blob[0]:返回色块外框的x坐标(int)
    #    blob[1]:返回色块的外框的y坐标(int)
    #    blob[2]:返回色块的外框的宽度w(int)
    #    blob[3]:返回色块的外框的高度h(int)
    #    blob[4]:返回色块的像素数量(int)
    #    blob[5]:返回色块的外框的中心x坐标(int)
    #    blob[6]:返回色块的外框的中心y坐标(int)

            # 先假定在距离length处有个小球作为参照物
                    bbbbb = blobsssss[0]    #blobs[0]就是我们识别到的第一个色块 即取第一个色块的值赋给b数组中的x、y、w、h在图像周围画框框
                    imggggg.draw_rectangle(bbbbb[0:4]) # 利用
                    imggggg.draw_cross(bbbbb[5], bbbbb[6]) # 在物体的中心点(cx,xy)画一个十字
                    Lmmmmm = (bbbbb[2]+bbbbb[3])/2  #取长和宽的平均值,也就是把长和宽的像素点的个数赋给LM(小球是圆形,因此长宽大致相等)

    #           length = K/Lm   #在第一次运行时要先确定K的值!
                    sizeeeee = KKKKK2*Lmmmmm
                    print(Lmmmmm)      #测量K时先打开
                    print(sizeeeee)

            #计算出K2后我们就可以测量高度和宽度啦
                    hhhhh = KKKKK2*bbbbb[3]  #K2乘高度方向的像素点
                    wwwww = KKKKK2*bbbbb[2]  #K2乘宽度方向的像素点
                    print(f"高为:{h},宽为:{w}")

                    yyyyy=hhhhh*wwwww

                    if sht<2 and(yyyyy)<10:
                                      uarttttt.write("8")
                                      time.sleep_ms(1000)
                                      uarttttt.write("9")
                                      sht=sht+2
                                      time.sleep_ms(30000)#延时30秒
                    if sht>2 and(yyyyy)<5:
                                    uarttttt.write("8")
                                    time.sleep_ms(1000)
                                    uarttttt.write("10")
                                    sht=sht-2
2022-11-23 18:41
I
网红小智智 星瞳科技

如果涉及代码,需要报错提示与全部代码文本,请注意不要贴代码图片

2022-11-23 19:51
F
xvwt
import sensor, image, time, os, tf, math, uos, gc,ustruct,machine, network, usocket, sys

from pyb import LED,UART
import pyb
SSID ='OPENMV_AP'    # Network SSID
KEY  ='1234567890'   # wifi密码(必须为10字符)
HOST = ''           # 使用第一个可用的端口
PORT = 8080         # 任意非特权端口
uart = UART(3,9600,timeout_char = 1000)
uart.init(9600, bits=8, parity=None, stop=1, timeout_char=1000)
sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=10000)
sensor.set_auto_gain(False)  # 必须关闭此功能,以防止图像冲洗…
sensor.set_auto_whitebal(False)  # 必须关闭此功能,以防止图像冲洗…          # Let the camera adjust.
wlan = network.WINC(mode=network.WINC.MODE_AP)
wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2)
net = None
labels = None
min_confidence = 0.5
uart = UART(3,9600,timeout_char = 1000)
uart.init(9600, bits=8, parity=None, stop=1, timeout_char=1000)
gjz=1
sht=1
K=357     #测距离:先设置一个初始值基准设置为5000后让目标距离摄像头10cm后,((k/打印出的数字)*10)替换设定值。
K2=0.08#测大小:同样先设置一个初始值
clock = time.clock() # Tracks FPS.
while (True):
     if uart.any():                          #判断是否接收到数据
        a = uart.read(5).decode()           #uart.read()为一个字节串,加.decode() 变成字符串
        print(a)
        if a == 'sleep':                        #如果接收达字符0
            print('sleep')
            gjz=gjz+2
            uart.write("zzz")                            #在OpenMV的串行终端中打印
     while(gjz == 3):
        LED(3).on() # 创建服务器套接字
        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)
            uart.write("1")
            #sys.print_exception(e)
     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(gjz == 1):
            LED(2).on()
            if uart.any():                          #判断是否接收到数据
                a = uart.read(5).decode()           #uart.read()为一个字节串,加.decode() 变成字符串
                print(a)
                if a == 'sleep':                        #如果接收达字符0
                    print('sleep')
                    gjz=gjz+2
                    uart.write("zzz")
            clock.tick() # clock.tick()开始追踪运行时间
            img = sensor.snapshot() # Take a picture and return the image.
            yellow_threshold   =(5, 50, 8, 130, 0,130)  #设置颜色的阈值
            blobs = img.find_blobs([yellow_threshold])
            if len(blobs) == 1:     #如果视野中只识别到一个目标(一个色块)

#    blob[0]:返回色块外框的x坐标(int)
#    blob[1]:返回色块的外框的y坐标(int)
#    blob[2]:返回色块的外框的宽度w(int)
#    blob[3]:返回色块的外框的高度h(int)
#    blob[4]:返回色块的像素数量(int)
#    blob[5]:返回色块的外框的中心x坐标(int)
#    blob[6]:返回色块的外框的中心y坐标(int)

        # 先假定在距离length处有个小球作为参照物
                b = blobs[0]    #blobs[0]就是我们识别到的第一个色块 即取第一个色块的值赋给b数组中的x、y、w、h在图像周围画框框
                img.draw_rectangle(b[0:4]) # 利用
                img.draw_cross(b[5], b[6]) # 在物体的中心点(cx,xy)画一个十字
                Lm = (b[2]+b[3])/2  #取长和宽的平均值,也就是把长和宽的像素点的个数赋给LM(小球是圆形,因此长宽大致相等)

#           length = K/Lm   #在第一次运行时要先确定K的值!
                size = K2*Lm
                print(Lm)      #测量K时先打开
                print(size)

        #计算出K2后我们就可以测量高度和宽度啦
                h = K2*b[3]  #K2乘高度方向的像素点
                w = K2*b[2]  #K2乘宽度方向的像素点
                print(f"高为:{h},宽为:{w}")

                y=h*w

                if sht<2 and(y)<10:
                                   uart.write("8")
                                   time.sleep_ms(100)
                                   uart.write("9")
                                   sht=sht+2
                                   #time.sleep_ms(30000)#延时30秒
                if sht>2 and(y)<5:
                                uart.write("8")
                                time.sleep_ms(100)
                                uart.write("10")
                                sht=sht-2
#靠岸  测大小



            f_x = (2.8 / 3.984) * 160 # find_apriltags 如果没有设置,则默认为这个
            f_y = (2.8 / 2.952) * 120 # find_apriltags 如果没有设置,则默认为这个
            c_x = 160 * 0.5 # find_apriltags 如果没有设置,则默认为这个 (the image.w * 0.5)
            c_y = 120 * 0.5 # find_apriltags 如果没有设置,则默认为这个 (the image.h * 0.5)

            def degrees(radians):
                return (180 * radians) / math.pi

            clock.tick()
            img = sensor.snapshot()
            for tag in img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y): # 默认为 TAG36H11
                img.draw_rectangle(tag.rect(), color = (255, 0, 0))
                img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
                print_args = (tag.x_translation(), tag.y_translation(), tag.z_translation(), \
                    degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
                print("Tx: %f, Ty %f, Tz %f, Rx %f, Ry %f, Rz %f" % print_args)
                print(clock.fps())
            #kz=实际距离/tz   (通过测量实际距离除以tz得到比例后可得)
                k=-12
                s=math.sqrt(tag.x_translation()**2+tag.y_translation()**2+tag.z_translation()**2)#未知距离时的apriltag的坐标
                length2=s*-k#利用成像原理数学公式计算真实长度

                print(f"距离为:{length2}")


                if length2<5:
                    uart.write("off")

代码是这样,想知道能不能通过uart控制那个WiFi图传,试了很久都不行,谢谢谢谢

2022-11-24 19:27
I
网红小智智 星瞳科技

你可以考虑在80行的地方(snapshot()上面)加入uart.read(),但是这样只是在图传的时候读入串口。