我目前正在尝试将图像从我的树莓派发送到我的电脑,并使用套接字库将某些内容发送回PI。发送图像的工作原理,如果我不发送一些东西。但是,如果我尝试向PI发送响应,则两个代码似乎都卡住了。任何人都可以帮我这个吗?
这是我的服务器代码:
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('192.168.137.1', 5005))
server_socket.listen(5)
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # om tekst van tensorflow import niet te laten zien
import tensorflow as tf
import cv2
import matplotlib.pyplot as plt
import numpy as np
model = tf.keras.models.load_model("C:/Users/antho/Bachelorproef/Models/(BLOK1&2&3)3-conv-128-nodes-2-dense-1585522274")
print("model loaded")
IMG_SIZE = 100
while (1):
client_socket, address = server_socket.accept()
print("Connected to - ",address,"\n")
fp = open("C:/Users/antho/Bachelorproef/Socket_Tests/test.jpeg",'wb')
while True:
strng = client_socket.recv(512)
if not strng:
break
fp.write(strng)
fp.close()
print("Image received")
img = cv2.imread("C:/Users/antho/Bachelorproef/Socket_Tests/test.jpeg", cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (IMG_SIZE , IMG_SIZE))
img = np.array(img).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
prediction = model.predict(img)
print(np.argmax(prediction))
pred = str(np.argmax(prediction))
client_socket.sendall(bytes(pred, "utf-8"))
print("send pred")
这是我的客户端代码:
#!/usr/bin/python
# TCP client example
import socket,os
import io
import time
import picamera
# Create an in-memory stream
my_stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.start_preview()
# Camera warm-up time
time.sleep(0.2)
camera.capture(my_stream, format='jpeg')
my_stream.seek(0) # stream terug op begin zetten
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.137.1', 5005))
size = 1024
while(1):
strng = my_stream.readline(512)
# print(strng)
if not strng:
break
client_socket.send(strng)
如果我注释掉此代码中的最后几行,图像将从我的计算机发送到目录。有了它,它就会卡住。
临摹微笑
相关分类