logo

我想把深度学习检测到的结果通过WiFi模块发送到电脑上

2023-05-06 17:43
A
左左
import sensor, image, time, os, tf, uos, gc, network, usocket, sys, json

SSID ='OPENMV_AP'    # Network SSID
KEY  ='1234567890'    # Network key (must be 10 chars)
HOST = ''           # Use first available interface
PORT = 8080         # Arbitrary non-privileged port



# Reset sensor
sensor.reset()
# Set sensor settings
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.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=2000)          # Let the camera adjust.

net = None
labels = None

try:
    # load the model, alloc the model file on the heap if we have at least 64K free after loading
    net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
except Exception as e:
    print(e)
    raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')

try:
    labels = [line.rstrip('\n') for line in open("labels.txt")]
except Exception as e:
    raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')')

clock = time.clock()


# Init wlan module in AP mode.
wlan = network.WINC(mode=network.WINC.MODE_AP)
wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2)

# You can block waiting for client to connect
#print(wlan.wait_for_sta(10000))

def response(s):
    print ('Waiting for connections..')
    client, addr = s.accept()
    # set client socket timeout to 2s
    client.settimeout(2.0)
    print ('Connected to ' + addr[0] + ':' + str(addr[1]))

    # Read request from client
    data = client.recv(1024)
    # Should parse client request here

    # Send multipart header
    client.send("HTTP/1.1 200 OK\r\n" \
                "Server: OpenMV\r\n" \
                "Content-Type: application/json\r\n" \
                "Cache-Control: no-cache\r\n" \
                "Pragma: no-cache\r\n\r\n")

    # FPS clock
    clock = time.clock()

    # Start streaming images
    # NOTE: Disable IDE preview to increase streaming FPS.

    clock.tick()

    img = sensor.snapshot()

    # default settings just do one detection... change them to search the image...
    for obj in net.classify(img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
        #print("**********\nPredictions at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())
        img.draw_rectangle(obj.rect())
        # This combines the labels and confidence values into a list of tuples
        predictions_list = list(zip(labels, obj.output()))

        for i in range(len(predictions_list)):
            #print("%s = %f" % (predictions_list[i][0], predictions_list[i][1]))
            a=obj.output()[0]
            b=obj.output()[1]
            c=obj.output()[2]
            d=obj.output()[3]
            f=obj.output()[4]
            g=obj.output()[5]
            if a>0.95:
                h=1
                print(h)
    client.send(json.dumps(h))
    client.close()

while (True):
    # Create server socket
    s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
    try:
        # Bind and listen
        s.bind([HOST, PORT])
        s.listen(5)

        # Set server socket timeout
        # NOTE: Due to a WINC FW bug, the server socket must be closed and reopened if
        # the client disconnects. Use a timeout here to close and re-create the socket.
        s.settimeout(3)
        response(s)
    except OSError as e:
        s.close()
        print("socket error: ", e)
        #sys.print_exception(e)
c3153639-df2f-478e-a5f8-9dae9ccdcb3c.png
2023-05-06 18:40
I
网红小智智 星瞳科技

在73行:img = sensor.snapshot()下面,添加一句h=0

2023-05-06 20:18
A
左左

@kidswong999

我加了那个可是现在的问题就是那个196.168.1.1都进不去

A
左左

@kidswong999 我感觉我这个模型训练和这个WiFi传输没有加好,还有一个问题就是我这个程序运行起来没几分钟摄像头就不动了,就像卡住了一样,但是不报错

2023-05-08 15:58
I
网红小智智 星瞳科技

你的程序是,每次访问才采集和运行一次。

2023-05-09 09:58
A
左左

@kidswong999 那我需要怎么改才能像那个传输小球坐标一样一直传呢

2023-05-09 19:22
I
网红小智智 星瞳科技

@k5r1 传输小球坐标,也是访问一次识别一次。你可以写一个js,每秒循环访问。

2023-05-10 22:00
A
左左

@kidswong999 不太懂应该怎么写啊 关键是现在我这个代码就是那个网站都进不去水为什么呢