我想做一个应用程序client-server(客户端是一个android studio应用程序,而服务器是树莓派上的python)将图像从应用程序发送到服务器。如果我在笔记本电脑上运行服务器代码,则可以完美运行,但是如果我在覆盆子上运行,则在出现第一个字符后停止运行(无任何错误)
我在android studio中有以下代码用于发送图像
package com.ersek.opensesame;
import java.io.*;
import java.net.Socket;
public class ImageSender implements Runnable{
private String filename;
public ImageSender(String filename) {
this.filename = filename;
}
private byte[] intToByteArray(int value) {
return new byte[] {
(byte)(value >>> 24),
(byte)(value >>> 16),
(byte)(value >>> 8),
(byte)value};
}
private void sendImage() throws IOException {
String TCP_IP = "192.abc.a.ab";
int TCP_PORT = 1240;
Socket socket = new Socket(TCP_IP, TCP_PORT);
InputStream inputStream = new FileInputStream(filename);
OutputStream outputStream = socket.getOutputStream();
int BUFFER_SIZE = 4096;
byte[] buffer = new byte[BUFFER_SIZE];
int currentBufferSize = inputStream.read(buffer);
while(currentBufferSize != -1) {
outputStream.write(intToByteArray(currentBufferSize));
outputStream.write(buffer);
currentBufferSize = inputStream.read(buffer);
}
outputStream.write(intToByteArray(-20));
socket.close();
}
@Override
public void run() {
try {
sendImage();
} catch (IOException e) {
e.printStackTrace();
}
}
我的python服务器代码+一个简单的面部识别部分是:
import socket
#import face_recognition
TCP_IP = '192.abc.a.ab'
TCP_PORT = 1240
def face_recognition_fun():
picture_of_me = face_recognition.load_image_file("path")
my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
# my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!
unknown_picture = face_recognition.load_image_file("path")
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
# Now we can see the two face encodings are of the same person with `compare_faces`!
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)
if results[0] == True:
print("It's a picture of me!")
else:
print("It's not a picture of me!")
def receive_image(filename):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((TCP_IP, TCP_PORT))
server_socket.listen(1)
while True:
connection, address = server_socket.accept()
try:
file = open(filename, "wb")
current_buffer_size = connection.recv(4)
current_buffer_size = int.from_bytes(current_buffer_size, byteorder='big')
while current_buffer_size > 0:
print(current_buffer_size)
buffer = connection.recv(current_buffer_size)
file.write(buffer)
current_buffer_size = connection.recv(4)
current_buffer_size = int.from_bytes(current_buffer_size, byteorder='big')
file.close()
#face_recognition_fun()
connection.close()
except:
pass
receive_image("path")