I've developed a video recording app in Android with Camera 2 in order to develop some experiments of imagem processing.
Since now, my approach to process videos was to download it from emulator and process in MATLAB code that accepts an .mp4 video as input using following sample code:
function [I] = Android4(Vrecorded)
%#codegen
% Loads and created pretrained YOLO v4 DL NW detector customized
% to detect LED array.
vidObj = VideoReader(char(Vrecorded)); % lê o vídeo capturado pela câmera e o divide em quadros
% Read video properties
numFrames = vidObj.NumFrames;
height = vidObj.Height;
width = vidObj.Width;
% Preallocate the 3D array to store the video frames
videoArray = zeros(height, width, 3, numFrames,'uint8');
% Iterate through each frame and store it in the array
for frameIdx = 1:numFrames
frame = readFrame(vidObj);
videoArray(:, :, :, frameIdx) = frame;
end
detector = coder.loadDeepLearningNetwork("trainedDetector_23022023.mat");
% Read all frames as grayscale images
frames = reshape(videoArray(:,:,1,:), size(videoArray,1), size(videoArray,2), []);
% extrai o número de quadros do vídeo capturado (NoF - Number of Frames)
NoF = size(frames,3);
%Detect objects in an unknown image / video by using the pretrained YOLO v4
FrameForDetection = zeros(size(frames,1),size(frames,2),3);
FrameForDetection(:,:,:) = videoArray(:,:,:,1);
%object detector
[bboxes,~,~] = detect(detector,FrameForDetection);
% This cell contains the cropped frames containing RoI after YOLO v4 object
% detection algoritm process the frames
croppedFrameVectors = cell(1,1);
sizes = imcrop(frames(:,:,1),bboxes(1,:));
croppedFrames = zeros(size(sizes,1),size(sizes,2),NoF,'uint8');
for n = 1:1
croppedFrameVectors{n} = zeros(0,0,0,'uint8');
for k = 1:NoF
% crops the frames in each detected object according to bounding
% boxes results captured on the first frame
croppedFrames(:,:,k) = imcrop(frames(:,:,k),bboxes(1,:));
end
% Insert the cropped frames with bounding boxes within a cell to allow
% multiple cropped objects within the same variable
croppedFrameVectors{n} = croppedFrames;
% Erase variable to crop another object due to bbox different
% dimensions from one object to another (widht x height)
end
I = croppedFrameVectors{1};
However, my goal is to export this code to Android using C++ CMake support for Android (https://developer.android.com/studio/projects/add-native-code).
After generating C++ code from MATLAB previous code using MATLAB Coder, I have the following main() function:
// Include files
#include "main.h"
#include "Android5.h"
#include "Android5_terminate.h"
#include "rt_nonfinite.h"
#include "string1.h"
#include "coder_array.h"
// Function Declarations
static coder::array<char, 2U> argInit_1xUnbounded_char_T();
static char argInit_char_T();
static coder::rtString argInit_rtString();
// Function Definitions
static coder::array<char, 2U> argInit_1xUnbounded_char_T()
{
coder::array<char, 2U> result;
// Set the size of the array.
result.set_size(1, 2);
// Loop over the array to initialize each element.
for (int idx0{0}; idx0 < 1; idx0++) {
for (int idx1{0}; idx1 < result.size(1); idx1++) {
// Set the value of the array element.
result[idx1] = argInit_char_T();
}
}
return result;
}
static char argInit_char_T()
{
return '?';
}
static coder::rtString argInit_rtString()
{
coder::rtString result;
coder::array<char, 2U> r;
// Set the value of each structure field.
r = argInit_1xUnbounded_char_T();
result.init(r);
return result;
}
int main(int, char **)
{
main_Android5();
Android5_terminate();
return 0;
}
void main_Android5()
{
coder::rtString Vrecorded;
coder::array<unsigned char, 3U> frames;
// Initialize function 'Android5' input arguments.
// Initialize function input argument 'Vrecorded'.
Vrecorded = argInit_rtString();
// Call 'Android5'.
Android5(&Vrecorded, frames);
}
So my question is: How would I pass a video stored at internal/external storage from device to this function so it can be processed by the generated C++ code from MATLAB. Is it possible to do this?