0

well i wrote code in python for server and i am applying simple aruco detection on the video from my webcam and i want to display the result in my android app . But i unable to do so.

After runnig the app it shows these errors Throwing OutOfMemoryError "Failed to allocate a 268435468 byte allocation with 25165824 free bytes and 380MB until OOM, target footprint 163122328, growth limit 536870912" (VmSize 6267440 kB)

i tried to change the bytes i am allocating to the array, i also tried solution present here --

Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

but still i am getting same error

here is my code for

mainactivity.java



package com.example.drone3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.ContactsContract;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.VideoView;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Arrays;




public class MainActivity extends AppCompatActivity {
    Button Land,up,forward,rotate;
    EditText editTextAddress, editTextPort;
    Button buttonConnect;
    Switch am;
    ImageView imageV;
    private ImageView screen;
    public void abort(View view){
        up.setEnabled(false);
        forward.setEnabled(false);
        rotate.setEnabled(false);
        am.setEnabled(false);

    }
    public void onclick(View view){
        Button b=(Button) view;
        String s=b.getText().toString();
        Log.i("command is:",s);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(getSupportActionBar()!=null)
            this.getSupportActionBar().hide();
        editTextAddress = (EditText)findViewById(R.id.address);
        editTextPort = (EditText)findViewById(R.id.port);
        buttonConnect = (Button)findViewById(R.id.connect);
        buttonConnect.setOnClickListener(buttonConnectOnClickListener);
        am=(Switch) findViewById(R.id.switch1);
        Land=(Button) findViewById(R.id.landbutton);
        up=(Button) findViewById(R.id.upbutton);
        forward=(Button) findViewById(R.id.forwardbutton);
        Log.i("this shit is working:","57");
        rotate=(Button) findViewById(R.id.rotatebutton);
        am.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(am.isChecked()){
                    Land.setEnabled(false);
                    up.setEnabled(false);
                    forward.setEnabled(false);
                    rotate.setEnabled(false);

                }
                else{
                    Land.setEnabled(true);
                    up.setEnabled(true);
                    forward.setEnabled(true);
                    rotate.setEnabled(true);
                }
            }
        });
        if(Build.VERSION.SDK_INT>9){
            StrictMode.ThreadPolicy BackgroundTask=
                    new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(BackgroundTask);
        }
    }

    View.OnClickListener buttonConnectOnClickListener =
            new View.OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    MyClientTask myClientTask = new MyClientTask(
                            editTextAddress.getText().toString(),
                            Integer.parseInt(editTextPort.getText().toString()));
                    myClientTask.execute();
                }};

    public class MyClientTask extends AsyncTask<Void, Void, Void> {

        String dstAddress;
        int dstPort;
        MyClientTask(String addr, int port){
            dstAddress = addr;
            dstPort = port;
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            InputStream inputStream;

                try {
                    Socket socket = new Socket(dstAddress, dstPort);
                    inputStream = socket.getInputStream();
                    ByteArrayOutputStream byteArrayOutputStream =
                            new ByteArrayOutputStream(4*1024);
                    byte[] buffer = new byte[4*1024 ];

                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        byteArrayOutputStream.write(buffer, 0, bytesRead);
                    }
                    Log.i("this shit is working:",""+buffer.length);
                    Bitmap bm = BitmapFactory.decodeByteArray(buffer, 0,buffer.length);

                    imageV.setImageBitmap(bm);
                    Log.i("this packet",buffer.toString());



                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

        }

    }

}


androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"/>



    <application
        android:usesCleartextTraffic="true"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:hardwareAccelerated="false"
        android:largeHeap="true"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Drone3"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/landbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginStart="16dp"
        android:onClick="abort"
        android:text="Land"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/rotatebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginStart="36dp"
        android:onClick="onclick"
        android:text="rotate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/landbutton" />

    <Button
        android:id="@+id/upbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginStart="40dp"
        android:onClick="onclick"
        android:text="Up"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/rotatebutton" />

    <Button
        android:id="@+id/forwardbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginStart="28dp"
        android:onClick="onclick"
        android:text="Forward"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/upbutton" />

    <Switch
        android:id="@+id/switch1"
        android:layout_width="132dp"
        android:layout_height="48dp"
        android:layout_marginBottom="40dp"
        android:layout_marginEnd="40dp"
        android:text="M/A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="162dp"
        android:layout_height="66dp"
        android:gravity="center"
        android:text="OmProduction"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.595" />

    <ImageView
        android:id="@+id/display"
        android:layout_width="705dp"
        android:layout_height="288dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_foreground" />

    <EditText
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:hint="dstAddress"
        android:minHeight="48dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/port"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:hint="dstPort"
        android:minHeight="48dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/address" />

    <Button
        android:id="@+id/connect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Connect..."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/port" />

</androidx.constraintlayout.widget.ConstraintLayout>


here is my python code for server

import socket
import cv2
import pickle
import struct
from cv2 import aruco
import numpy as np
import jpysocket
marker_dict = aruco.Dictionary_get(aruco.DICT_4X4_50)
param_markers = aruco.DetectorParameters_create()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_name = socket.gethostname()
print("host name", host_name)
host_ip = socket.gethostbyname(host_name)
print('HOST IP:', host_ip)
port = 9999
socket_address = (host_ip, port)
server_socket.bind(socket_address)
server_socket.listen(5)
print("LISTENING AT:", socket_address)

while True:
    client_socket, addr = server_socket.accept()
    print('GOT CONNECTION FROM:', addr)
    if client_socket:
        vid = cv2.VideoCapture(0)
        while(vid.isOpened()):
            ret, frame = vid.read()
            gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            marker_corners, marker_IDs, reject = aruco.detectMarkers(
            gray_frame, marker_dict, parameters=param_markers
            )
            if marker_corners:
                for ids, corners in zip(marker_IDs, marker_corners):
                    cv2.polylines(
                        frame, [corners.astype(np.int32)], True, (0, 255, 255), 4, cv2.LINE_AA
                    )
                    corners = corners.reshape(4, 2)
                    corners = corners.astype(int)
                    top_right = corners[0].ravel()
                    top_left = corners[1].ravel()
                    bottom_right = corners[2].ravel()
                    bottom_left = corners[3].ravel()
                    cv2.putText(
                        frame,
                        f"id: {ids[0]}",
                        top_right,
                        cv2.FONT_HERSHEY_PLAIN,
                        1.3,
                        (200, 100, 0),
                        2,
                        cv2.LINE_AA,
                    )
            a = pickle.dumps(frame)
            message = a
            print(len(message))
            client_socket.send(message)
            cv2.imshow('TRANSMITTING VIDEO', frame)
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                client_socket.close()

0 Answers0