Android студийный клиент - связь через сокет raspberry pi - PullRequest
0 голосов
/ 30 мая 2020

Я хочу создать приложение клиент-сервер (клиент - это приложение android studio, а сервер - python в raspberry pi) для отправки изображений из приложения на сервер. Если я запускаю свой серверный код на ноутбуке, он работает отлично, но если я запускаю на малине, он останавливается после первого чарма (без каких-либо ошибок)

У меня есть следующий код в 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")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...