Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-81
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-81
2022-81
Commits
0eaed9bd
Commit
0eaed9bd
authored
Nov 10, 2022
by
P.Y.D Jayasinghe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Deleted
parent
c68fa316
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
0 additions
and
371 deletions
+0
-371
app/src/main/assets/temp.jpg
app/src/main/assets/temp.jpg
+0
-0
app/src/main/java/com/example/resnetnew/ImageNetClasses.java
app/src/main/java/com/example/resnetnew/ImageNetClasses.java
+0
-15
app/src/main/java/com/example/resnetnew/MainActivity.java
app/src/main/java/com/example/resnetnew/MainActivity.java
+0
-107
app/src/main/java/com/example/resnetnew/Unet.java
app/src/main/java/com/example/resnetnew/Unet.java
+0
-141
app/src/main/res/layout/activity_main.xml
app/src/main/res/layout/activity_main.xml
+0
-41
app/src/main/res/layout/activity_unet.xml
app/src/main/res/layout/activity_unet.xml
+0
-67
No files found.
app/src/main/assets/temp.jpg
deleted
100644 → 0
View file @
c68fa316
25 KB
app/src/main/java/com/example/resnetnew/ImageNetClasses.java
deleted
100644 → 0
View file @
c68fa316
package
com.example.resnetnew
;
public
class
ImageNetClasses
{
public
static
String
[]
IMAGENET_CLASSES
=
new
String
[]{
"Snake weed"
,
"Siam weed"
,
"Rubber vine"
,
"Prickly acacia"
,
"Parthenium"
,
"Parkinsonia"
,
"Negative"
,
"Lantana"
,
"Chinee apple"
};
}
app/src/main/java/com/example/resnetnew/MainActivity.java
deleted
100644 → 0
View file @
c68fa316
package
com.example.resnetnew
;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.content.Context
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.ImageView
;
import
android.widget.TextView
;
import
org.pytorch.IValue
;
import
org.pytorch.LiteModuleLoader
;
import
org.pytorch.MemoryFormat
;
import
org.pytorch.Module
;
import
org.pytorch.Tensor
;
import
org.pytorch.torchvision.TensorImageUtils
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
public
class
MainActivity
extends
AppCompatActivity
{
private
Bitmap
bitmap
=
null
;
private
Module
module
=
null
;
public
static
String
assetFilePath
(
Context
context
,
String
assetName
)
throws
IOException
{
File
file
=
new
File
(
context
.
getFilesDir
(),
assetName
);
if
(
file
.
exists
()
&&
file
.
length
()
>
0
)
{
return
file
.
getAbsolutePath
();
}
try
(
InputStream
is
=
context
.
getAssets
().
open
(
assetName
))
{
try
(
OutputStream
os
=
new
FileOutputStream
(
file
))
{
byte
[]
buffer
=
new
byte
[
4
*
1024
];
int
read
;
while
((
read
=
is
.
read
(
buffer
))
!=
-
1
)
{
os
.
write
(
buffer
,
0
,
read
);
}
os
.
flush
();
}
return
file
.
getAbsolutePath
();
}
}
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_main
);
try
{
// creating bitmap from packaged into app android asset 'image.jpg',
// app/src/main/assets/image.jpg
bitmap
=
BitmapFactory
.
decodeStream
(
getAssets
().
open
(
"temp.jpg"
));
// loading serialized torchscript module from packaged into app android asset model.pt,
// app/src/model/assets/model.pt
module
=
LiteModuleLoader
.
load
(
assetFilePath
(
this
,
"optimized.ptl"
));
}
catch
(
IOException
e
)
{
Log
.
e
(
"PytorchHelloWorld"
,
"Error reading assets"
,
e
);
finish
();
}
// showing image on UI
ImageView
imageView
=
findViewById
(
R
.
id
.
imageView
);
imageView
.
setImageBitmap
(
bitmap
);
final
Button
button
=
findViewById
(
R
.
id
.
button
);
button
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
// preparing input tensor
final
Tensor
inputTensor
=
TensorImageUtils
.
bitmapToFloat32Tensor
(
bitmap
,
TensorImageUtils
.
TORCHVISION_NORM_MEAN_RGB
,
TensorImageUtils
.
TORCHVISION_NORM_STD_RGB
,
MemoryFormat
.
CHANNELS_LAST
);
// running the model
final
Tensor
outputTensor
=
module
.
forward
(
IValue
.
from
(
inputTensor
)).
toTensor
();
// getting tensor content as java array of floats
final
float
[]
scores
=
outputTensor
.
getDataAsFloatArray
();
// searching for the index with maximum score
float
maxScore
=
-
Float
.
MAX_VALUE
;
int
maxScoreIdx
=
-
1
;
for
(
int
i
=
0
;
i
<
scores
.
length
;
i
++)
{
if
(
scores
[
i
]
>
maxScore
)
{
maxScore
=
scores
[
i
];
maxScoreIdx
=
i
;
}
}
String
className
=
ImageNetClasses
.
IMAGENET_CLASSES
[
maxScoreIdx
];
// showing className on UI
TextView
textView
=
findViewById
(
R
.
id
.
text
);
textView
.
setText
(
className
);
}
});
}
}
\ No newline at end of file
app/src/main/java/com/example/resnetnew/Unet.java
deleted
100644 → 0
View file @
c68fa316
package
com.example.resnetnew
;
import
androidx.annotation.Nullable
;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.Manifest
;
import
android.content.Intent
;
import
android.content.pm.PackageManager
;
import
android.graphics.Bitmap
;
import
android.media.ThumbnailUtils
;
import
android.net.Uri
;
import
android.os.Bundle
;
import
android.provider.MediaStore
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.ImageView
;
import
android.widget.TextView
;
import
com.example.unet.ml.Model
;
import
org.tensorflow.lite.DataType
;
import
org.tensorflow.lite.support.tensorbuffer.TensorBuffer
;
import
java.io.IOException
;
import
java.nio.ByteBuffer
;
import
java.nio.ByteOrder
;
public
class
Unet
extends
AppCompatActivity
{
Button
camera
,
gallery
;
ImageView
imageView
;
TextView
result
;
int
imageSize
=
256
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_unet
);
camera
=
findViewById
(
R
.
id
.
button
);
gallery
=
findViewById
(
R
.
id
.
button2
);
result
=
findViewById
(
R
.
id
.
result
);
imageView
=
findViewById
(
R
.
id
.
imageView
);
camera
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
if
(
checkSelfPermission
(
Manifest
.
permission
.
CAMERA
)
==
PackageManager
.
PERMISSION_GRANTED
)
{
Intent
cameraIntent
=
new
Intent
(
MediaStore
.
ACTION_IMAGE_CAPTURE
);
startActivityForResult
(
cameraIntent
,
3
);
}
else
{
requestPermissions
(
new
String
[]{
Manifest
.
permission
.
CAMERA
},
100
);
}
}
});
gallery
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
view
)
{
Intent
cameraIntent
=
new
Intent
(
Intent
.
ACTION_PICK
,
MediaStore
.
Images
.
Media
.
EXTERNAL_CONTENT_URI
);
startActivityForResult
(
cameraIntent
,
1
);
}
});
}
@Override
protected
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
@Nullable
Intent
data
)
{
if
(
resultCode
==
RESULT_OK
){
if
(
requestCode
==
3
){
Bitmap
image
=
(
Bitmap
)
data
.
getExtras
().
get
(
"data"
);
int
dimension
=
Math
.
min
(
image
.
getWidth
(),
image
.
getHeight
());
image
=
ThumbnailUtils
.
extractThumbnail
(
image
,
dimension
,
dimension
);
imageView
.
setImageBitmap
(
image
);
image
=
Bitmap
.
createScaledBitmap
(
image
,
imageSize
,
imageSize
,
false
);
classifyImage
(
image
);
}
else
{
Uri
dat
=
data
.
getData
();
Bitmap
image
=
null
;
try
{
image
=
MediaStore
.
Images
.
Media
.
getBitmap
(
this
.
getContentResolver
(),
dat
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
imageView
.
setImageBitmap
(
image
);
image
=
Bitmap
.
createScaledBitmap
(
image
,
imageSize
,
imageSize
,
false
);
classifyImage
(
image
);
}
}
super
.
onActivityResult
(
requestCode
,
resultCode
,
data
);
}
public
void
classifyImage
(
Bitmap
image
){
try
{
Model
model
=
Model
.
newInstance
(
getApplicationContext
());
// Creates inputs for reference.
TensorBuffer
inputFeature0
=
TensorBuffer
.
createFixedSize
(
new
int
[]{
1
,
256
,
256
,
3
},
DataType
.
FLOAT32
);
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocateDirect
(
4
*
imageSize
*
imageSize
*
3
);
byteBuffer
.
order
(
ByteOrder
.
nativeOrder
());
int
[]
intValues
=
new
int
[
imageSize
*
imageSize
];
image
.
getPixels
(
intValues
,
0
,
image
.
getWidth
(),
0
,
0
,
image
.
getWidth
(),
image
.
getHeight
());
int
pixel
=
0
;
//iterate over each pixel and extract R, G, and B values. Add those values individually to the byte buffer.
for
(
int
i
=
0
;
i
<
imageSize
;
i
++){
for
(
int
j
=
0
;
j
<
imageSize
;
j
++){
int
val
=
intValues
[
pixel
++];
// RGB
byteBuffer
.
putFloat
(((
val
>>
16
)
&
0xFF
)
*
(
1
.
f
/
1
));
byteBuffer
.
putFloat
(((
val
>>
8
)
&
0xFF
)
*
(
1
.
f
/
1
));
byteBuffer
.
putFloat
((
val
&
0xFF
)
*
(
1
.
f
/
1
));
}
}
inputFeature0
.
loadBuffer
(
byteBuffer
);
// Runs model inference and gets result.
Model
.
Outputs
outputs
=
model
.
process
(
inputFeature0
);
TensorBuffer
outputFeature0
=
outputs
.
getOutputFeature0AsTensorBuffer
();
float
[]
confidences
=
outputFeature0
.
getFloatArray
();
// find the index of the class with the biggest confidence.
int
maxPos
=
0
;
float
maxConfidence
=
0
;
for
(
int
i
=
0
;
i
<
confidences
.
length
;
i
++)
{
if
(
confidences
[
i
]
>
maxConfidence
)
{
maxConfidence
=
confidences
[
i
];
maxPos
=
i
;
}
}
String
[]
classes
=
{
"unlabeled"
,
"weed"
,
"rice"
,
"sand"
};
result
.
setText
(
classes
[
maxPos
]);
// Releases model resources if no longer used.
model
.
close
();
}
catch
(
IOException
e
)
{
// TODO Handle the exception
}
}
}
\ No newline at end of file
app/src/main/res/layout/activity_main.xml
deleted
100644 → 0
View file @
c68fa316
<?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:layout_width=
"match_parent"
android:layout_height=
"match_parent"
tools:context=
".MainActivity"
>
<ImageView
android:id=
"@+id/imageView"
android:layout_width=
"156dp"
android:layout_height=
"165dp"
android:layout_marginTop=
"36dp"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintHorizontal_bias=
"0.498"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
app:srcCompat=
"@drawable/ic_launcher_background"
/>
<Button
android:id=
"@+id/button"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginTop=
"12dp"
android:text=
"Button"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintHorizontal_bias=
"0.498"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/imageView"
/>
<TextView
android:id=
"@+id/textView"
android:layout_width=
"164dp"
android:layout_height=
"144dp"
android:layout_marginTop=
"8dp"
android:text=
"TextView"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintHorizontal_bias=
"0.497"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/button"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
app/src/main/res/layout/activity_unet.xml
deleted
100644 → 0
View file @
c68fa316
<?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:layout_width=
"match_parent"
android:layout_height=
"match_parent"
tools:context=
".Unet"
>
<ImageView
android:id=
"@+id/imageView"
android:layout_width=
"370sp"
android:layout_height=
"370sp"
android:layout_centerHorizontal=
"true"
app:layout_constraintTop_toTopOf=
"parent"
tools:layout_editor_absoluteX=
"25dp"
/>
<Button
android:id=
"@+id/button"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_above=
"@id/button2"
android:layout_centerInParent=
"true"
android:text=
"Take Picture"
android:textAllCaps=
"false"
android:textSize=
"21sp"
android:textStyle=
"bold"
app:layout_constraintTop_toBottomOf=
"@+id/imageView"
tools:layout_editor_absoluteX=
"0dp"
/>
<Button
android:id=
"@+id/button2"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:layout_alignParentBottom=
"true"
android:layout_centerInParent=
"true"
android:text=
"Launch Gallery"
android:textAllCaps=
"false"
android:textSize=
"21sp"
android:textStyle=
"bold"
tools:layout_editor_absoluteX=
"0dp"
tools:layout_editor_absoluteY=
"477dp"
/>
<TextView
android:id=
"@+id/classified"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_below=
"@+id/imageView"
android:layout_centerHorizontal=
"true"
android:text=
"Classified as:"
android:textSize=
"20sp"
android:textStyle=
"bold"
app:layout_constraintTop_toBottomOf=
"@+id/button"
tools:layout_editor_absoluteX=
"133dp"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_centerHorizontal=
"true"
android:text=
""
android:textColor=
"#C30000"
android:textStyle=
"bold"
android:textSize=
"27sp"
android:id=
"@+id/result"
android:layout_below=
"@+id/classified"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment