Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #128

Merged
merged 86 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
fe648d1
feature/UpdatedNeuralNet
Ian-u22488864 Sep 25, 2024
69e7124
feature/UpdatedNeuralNet
Ian-u22488864 Sep 27, 2024
cf5d5eb
Navbar test for Home Page working.
JimmyBee1 Sep 28, 2024
e2976ce
Home page Widget Working on Navbar Test.
JimmyBee1 Sep 28, 2024
f82b288
testing_rhevan: camera page widget tests
RhevanKruger Sep 29, 2024
394ab78
testing_rhevan: auth service unit testing
RhevanKruger Sep 29, 2024
f4569fe
testing_rhevan: log in page unit testing
RhevanKruger Sep 29, 2024
0c7d029
testing_rhevan: added SQL database testing
GerritPotgieter Sep 29, 2024
aafd79b
Merge remote-tracking branch 'origin/testing_rhevan' into testing_rhevan
GerritPotgieter Sep 29, 2024
6c2c567
Audio Integration
PineCone85 Sep 29, 2024
63a2633
testing_rhevan: added pubspec yaml dependancy for the database tests
GerritPotgieter Sep 29, 2024
331a4ff
testing_rhevan: sing up unit testing
RhevanKruger Sep 29, 2024
889756a
Merge remote-tracking branch 'origin/testing_rhevan' into testing_rhevan
RhevanKruger Sep 29, 2024
f7ca4b0
Navbar test for Home Page working.
JimmyBee1 Sep 28, 2024
c9dfe1c
Home page Widget Working on Navbar Test.
JimmyBee1 Sep 28, 2024
6edd44a
testing_rhevan: camera page widget tests
RhevanKruger Sep 29, 2024
ed8684e
testing_rhevan: auth service unit testing
RhevanKruger Sep 29, 2024
c4be430
testing_rhevan: log in page unit testing
RhevanKruger Sep 29, 2024
0511f30
testing_rhevan: sing up unit testing
RhevanKruger Sep 29, 2024
a2b8360
testing_rhevan: added SQL database testing
GerritPotgieter Sep 29, 2024
83810d4
Audio Integration
PineCone85 Sep 29, 2024
a27167a
testing_rhevan: added pubspec yaml dependancy for the database tests
GerritPotgieter Sep 29, 2024
4f2ba8e
Merge remote-tracking branch 'origin/testing_rhevan' into testing_rhevan
RhevanKruger Sep 29, 2024
7289324
Added updated model
Ian-u22488864 Sep 29, 2024
c29d96f
Added updated model
Ian-u22488864 Sep 29, 2024
4ae60bf
Added updated model
Ian-u22488864 Sep 29, 2024
0fe1989
Added updated model
Ian-u22488864 Sep 29, 2024
0222248
Added updated model
Ian-u22488864 Sep 29, 2024
82dbce3
Added updated model
Ian-u22488864 Sep 29, 2024
3da56ee
Added updated model
Ian-u22488864 Sep 29, 2024
574d341
Added updated model
Ian-u22488864 Sep 29, 2024
38b99ab
Added updated model
Ian-u22488864 Sep 29, 2024
895a3d8
Added updated model
Ian-u22488864 Sep 29, 2024
59337d2
Added updated model
Ian-u22488864 Sep 29, 2024
1c4c3b5
Added updated model
Ian-u22488864 Sep 29, 2024
104487c
Added updated model
Ian-u22488864 Sep 29, 2024
fe4972e
Added updated model
Ian-u22488864 Sep 29, 2024
b31995c
Added updated model
Ian-u22488864 Sep 29, 2024
42d032b
Added updated model
Ian-u22488864 Sep 29, 2024
10d6d72
Added updated model
Ian-u22488864 Sep 29, 2024
59bb289
Added updated model
Ian-u22488864 Sep 29, 2024
e1c14e5
Added updated model
Ian-u22488864 Sep 29, 2024
da893de
Added updated model
Ian-u22488864 Sep 29, 2024
14d65f6
testing_rhevan: non static spotify auth for testing
RhevanKruger Sep 29, 2024
361a6c7
Added updated model
Ian-u22488864 Sep 29, 2024
bda26b2
Added updated model
Ian-u22488864 Sep 29, 2024
18b9f7c
Added updated model
Ian-u22488864 Sep 29, 2024
7eab1a8
Added updated model
Ian-u22488864 Sep 29, 2024
4cbf3b7
Added new test model
Ian-u22488864 Sep 29, 2024
2cc3727
Added new test model
Ian-u22488864 Sep 29, 2024
9e0ba10
Added new test model
Ian-u22488864 Sep 29, 2024
8207f33
Added saved model in res/raw
Ian-u22488864 Sep 29, 2024
5c7d573
Added saved model in res/raw
Ian-u22488864 Sep 29, 2024
04fd1b4
Added saved model in res/raw
Ian-u22488864 Sep 29, 2024
553753c
testing_rhevan: user profile unit testing
RhevanKruger Sep 29, 2024
0c36a88
Added TensorFlow dependency
Ian-u22488864 Sep 29, 2024
020f3d4
Changed NeuralNetService to use tensorflow models
Ian-u22488864 Sep 29, 2024
64fbd4e
Added tensorflow test model
Ian-u22488864 Sep 29, 2024
d82eb32
Added tensorflow stress test model
Ian-u22488864 Sep 29, 2024
eb4a4ea
Added tensorflow version of original deeplearning4j model
Ian-u22488864 Sep 29, 2024
43cb0e4
Added current best tensorflow model
Ian-u22488864 Sep 29, 2024
a7154bd
Deleted bad deeplearning4j models
Ian-u22488864 Sep 29, 2024
27d3c4f
Cleaned NeuralNetService.java to use tensorflow models
Ian-u22488864 Sep 29, 2024
13c8f18
Copied old NeuralNetService.java as NeuralNetService_old.java
Ian-u22488864 Sep 29, 2024
d179fca
testing_rhevan: user playlist testing
RhevanKruger Sep 29, 2024
1192912
Merge pull request #120
Ian-u22488864 Sep 29, 2024
7093ed7
Audio Integration
PineCone85 Sep 29, 2024
caeea9f
Audio Integration
PineCone85 Sep 29, 2024
105ae9d
Audio Integration
PineCone85 Sep 29, 2024
1fea01b
Merge pull request #121 from COS301-SE-2024/testing_rhevan
PineCone85 Sep 29, 2024
9b7912f
cleanup/generalFinalization: overflow fix
RhevanKruger Sep 29, 2024
d4a7db6
Merge pull request #122 from COS301-SE-2024/cleanup/generalFinalization
RhevanKruger Sep 29, 2024
9307cc7
testing_rhevan: removed unused imports for linting purposes
GerritPotgieter Sep 29, 2024
9fabb4c
Merge pull request #123
GerritPotgieter Sep 29, 2024
b16f0d4
testing_rhevan: removed database prints
GerritPotgieter Sep 29, 2024
6fc6039
Merge pull request #124
GerritPotgieter Sep 29, 2024
bc6ff6e
Audio Integration
PineCone85 Sep 29, 2024
b782c45
Merge pull request #125 from COS301-SE-2024/NonFuncTesting
PineCone85 Sep 29, 2024
c063ef7
cleanup/generalFinalization: fixed first time video capturing not sho…
RhevanKruger Sep 29, 2024
bbe5bcc
Merge branch 'development' into cleanup/generalFinalization
RhevanKruger Sep 29, 2024
b980f0a
cleanup/generalFinalization: disabled recording button during loading…
RhevanKruger Sep 29, 2024
c84bb8b
Merge pull request #126 from COS301-SE-2024/cleanup/generalFinalization
RhevanKruger Sep 29, 2024
9ad0ea6
Merge remote-tracking branch 'origin/cleanup/generalFinalization' int…
RhevanKruger Sep 29, 2024
685702a
Merge pull request #127 from COS301-SE-2024/cleanup/generalFinalization
RhevanKruger Sep 29, 2024
3a774f2
testing updates
RhevanKruger Sep 29, 2024
327ef02
Merge remote-tracking branch 'origin/development' into development
RhevanKruger Sep 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:
prefer_const_constructors: ignore
prefer_const_literals_to_create_immutables: ignore
deprecated_member_use: ignore
unused_local_variable: ignore
include: package:flutter_lints/flutter.yaml

linter:
Expand Down
6 changes: 6 additions & 0 deletions frontend/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ android {
}



packagingOptions {

exclude 'META-INF/native-image/ios-x86_64/jnijavacpp/reflect-config.json'
Expand Down Expand Up @@ -123,6 +124,11 @@ android {
}

dependencies {

implementation 'org.tensorflow:tensorflow-lite:2.12.0' // Use the latest version



implementation (group: 'org.deeplearning4j', name: 'deeplearning4j-core', version: '1.0.0-beta6') {
exclude group: 'org.bytedeco', module: 'opencv-platform'
exclude group: 'org.bytedeco', module: 'leptonica-platform'
Expand Down
Binary file added frontend/android/app/src/main/assets/model.tflite
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
Expand All @@ -16,17 +15,6 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.util.ModelSerializer;
import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.datavec.image.loader.NativeImageLoader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import javax.swing.*;
import java.io.File;
import java.util.Arrays;
import java.util.List;
Expand All @@ -35,102 +23,153 @@
import android.content.Context;
import android.content.res.AssetManager;

//import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;



import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import org.tensorflow.lite.Interpreter;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import org.tensorflow.lite.Interpreter;





public class NeuralNetService {
private Context context;
private Interpreter tflite;

public NeuralNetService(Context context) {
this.context = context;
// loadModel();
}



public byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384]; // 16KB buffer

while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}

buffer.flush();
return buffer.toByteArray();
}


public Bitmap convertToGrayscale(Bitmap originalBitmap) {
int width, height;
height = originalBitmap.getHeight();
width = originalBitmap.getWidth();

Bitmap grayscaleBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(grayscaleBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorFilter);
canvas.drawBitmap(originalBitmap, 0, 0, paint);

return grayscaleBitmap;
private MappedByteBuffer loadModelFile(AssetManager assetManager, String modelPath) throws IOException {
try (AssetFileDescriptor fileDescriptor = assetManager.openFd(modelPath)) {
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
}




public String getMood(byte[] imageBytes) {
InputStream inputStream = new ByteArrayInputStream(imageBytes);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

int image_height = 48; // pixel size
int image_width = 48; // pixel size
int color_channels = 1; // 1 means grayscale

//List<String> labelList = Arrays.asList("angry", "happy", "neutral", "sad");
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

String[] emotions = {"angry", "happy", "neutral", "sad"};
// Step 2: Preprocess the Bitmap
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 48, 48, true);

// Convert to grayscale if necessary
int width = resizedBitmap.getWidth();
int height = resizedBitmap.getHeight();
float[][][][] inputData = new float[1][48][48][1]; // 1 sample, 48x48 image, 1 channel

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
// Get pixel value and convert to grayscale
int pixel = resizedBitmap.getPixel(i, j);
int grayValue = (int) (0.299 * Color.red(pixel) + 0.587 * Color.green(pixel) + 0.114 * Color.blue(pixel));
inputData[0][i][j][0] = grayValue / 255.0f; // Normalize to [0, 1]
}
}




try{

// Load the model from assets
MultiLayerNetwork model = null;
try {
AssetManager assetManager = context.getAssets();
InputStream modelInputStream = assetManager.open("savedNeuralNet_new.zip");
model = ModelSerializer.restoreMultiLayerNetwork(modelInputStream);
MappedByteBuffer modelBuffer = loadModelFile(assetManager, "new_model_9_modified_newest.tflite");


tflite = new Interpreter(modelBuffer);

Log.d("NeuralNetService", "MODEL LOADED!!!!!!!!!!!!");

} catch (IOException e) {
e.printStackTrace();
return "Error loading model";
}

} catch (Exception e) {
e.printStackTrace();

}

NativeImageLoader loader = new NativeImageLoader(image_height, image_width, color_channels);
INDArray image = null;

// Bitmap grayscaleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//
// for (int y = 0; y < bitmap.getHeight(); y++) {
// for (int x = 0; x < bitmap.getWidth(); x++) {
// int pixel = bitmap.getPixel(x, y);
// int red = (pixel >> 16) & 0xff;
// int green = (pixel >> 8) & 0xff;
// int blue = pixel & 0xff;
// int gray = (red + green + blue) / 3;
// grayscaleBitmap.setPixel(x, y, Color.rgb(gray, gray, gray));
// }
// }

Bitmap grayscaleBitmap = convertToGrayscale(bitmap);
float[][] outputData = new float[1][3];


try {
// Load the image as INDArray
image = loader.asMatrix(bitmap);
} catch (IOException e) {
tflite.run(inputData, outputData); // run the model with the input data

// output the results
Log.d("NeuralNetService", "Inference Output: " + Arrays.toString(outputData[0]));
} catch (Exception e) {
e.printStackTrace();
return "Error loading image";
}

DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
scaler.transform(image);
//[0.32354924, 0.35912168, 0.3173291]

INDArray output = model.output(image); // Pass image to neural net
String[] emotions = {"angry", "happy", "sad"};

float highestProbability = Float.MIN_VALUE;
int highestProbabilityIndex = -1;
int current_highest_index = 0;
float current_highest_value = 0;

for (int i = 0; i < output.length(); i++) {
float currentProbability = output.getFloat(i);
if (currentProbability > highestProbability) {
highestProbability = currentProbability;
highestProbabilityIndex = i;
for(int i = 0; i < 3; i++){
if(outputData[0][i] > current_highest_value){
current_highest_index = i;
current_highest_value = outputData[0][i];
}
}

return emotions[highestProbabilityIndex];

return emotions[current_highest_index];
}
}
}
Loading
Loading