Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-232
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
T.H.C. Heshan
2023-232
Commits
95a6bb68
Commit
95a6bb68
authored
May 24, 2023
by
thirani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
f:scene script
parent
507f8611
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
124 additions
and
0 deletions
+124
-0
Scene_Classify/classify_model/classifyIndoor.tflite
Scene_Classify/classify_model/classifyIndoor.tflite
+0
-0
Scene_Classify/classify_model/label.txt
Scene_Classify/classify_model/label.txt
+3
-0
Scene_Classify/detection_image3.py
Scene_Classify/detection_image3.py
+121
-0
No files found.
Scene_Classify/classify_model/classifyIndoor.tflite
0 → 100644
View file @
95a6bb68
File added
Scene_Classify/classify_model/label.txt
0 → 100644
View file @
95a6bb68
kitchen
livingroom
stairs
Scene_Classify/detection_image3.py
0 → 100644
View file @
95a6bb68
import
os
import
argparse
import
cv2
import
numpy
as
np
import
sys
import
importlib.util
# Define and parse input arguments
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'--modeldir'
,
help
=
'Folder the .tflite file is located in'
,
required
=
True
)
parser
.
add_argument
(
'--graph'
,
help
=
'Name of the .tflite file, if different than detect.tflite'
,
default
=
'detect.tflite'
)
parser
.
add_argument
(
'--labels'
,
help
=
'Name of the labelmap file, if different than labelmap.txt'
,
default
=
'label.txt'
)
parser
.
add_argument
(
'--threshold'
,
help
=
'Minimum confidence threshold for displaying detected objects'
,
default
=
0.5
)
parser
.
add_argument
(
'--edgetpu'
,
help
=
'Use Coral Edge TPU Accelerator to speed up detection'
,
action
=
'store_true'
)
args
=
parser
.
parse_args
()
MODEL_NAME
=
args
.
modeldir
GRAPH_NAME
=
args
.
graph
LABELMAP_NAME
=
args
.
labels
min_conf_threshold
=
float
(
args
.
threshold
)
use_TPU
=
args
.
edgetpu
# Import TensorFlow libraries
pkg
=
importlib
.
util
.
find_spec
(
'tflite_runtime'
)
if
pkg
:
from
tflite_runtime.interpreter
import
Interpreter
if
use_TPU
:
from
tflite_runtime.interpreter
import
load_delegate
else
:
from
tensorflow.lite.python.interpreter
import
Interpreter
if
use_TPU
:
from
tensorflow.lite.python.interpreter
import
load_delegate
# If using Edge TPU, assign filename for Edge TPU model
if
use_TPU
:
if
GRAPH_NAME
==
'detect.tflite'
:
GRAPH_NAME
=
'edgetpu.tflite'
# Get path to current working directory
CWD_PATH
=
os
.
getcwd
()
# Path to .tflite file, which contains the model that is used for object detection
PATH_TO_CKPT
=
os
.
path
.
join
(
CWD_PATH
,
MODEL_NAME
,
GRAPH_NAME
)
# Path to label map file
PATH_TO_LABELS
=
os
.
path
.
join
(
CWD_PATH
,
MODEL_NAME
,
LABELMAP_NAME
)
# Load the label map
with
open
(
PATH_TO_LABELS
,
'r'
)
as
f
:
labels
=
[
line
.
strip
()
for
line
in
f
.
readlines
()]
# Initialize previous label to None
prev_label
=
None
# Load the TensorFlow Lite model
if
use_TPU
:
interpreter
=
Interpreter
(
model_path
=
PATH_TO_CKPT
,
experimental_delegates
=
[
load_delegate
(
'libedgetpu.so.1.0'
)])
else
:
interpreter
=
Interpreter
(
model_path
=
PATH_TO_CKPT
)
interpreter
.
allocate_tensors
()
# Get model details
input_details
=
interpreter
.
get_input_details
()
output_details
=
interpreter
.
get_output_details
()
height
=
input_details
[
0
][
'shape'
][
1
]
width
=
input_details
[
0
][
'shape'
][
2
]
floating_model
=
(
input_details
[
0
][
'dtype'
]
==
np
.
float32
)
input_mean
=
127.5
input_std
=
127.5
# Create output folder to save labeled images
output_folder
=
'labeled_images'
os
.
makedirs
(
output_folder
,
exist_ok
=
True
)
# Iterate over the images
images_dir
=
'D:
\
ObjectDetection
\
Try_17.0_classification_mobilenet_balanceddataset
\
images'
# Path to the directory containing the images
image_files
=
os
.
listdir
(
images_dir
)
for
image_file
in
image_files
:
image_path
=
os
.
path
.
join
(
images_dir
,
image_file
)
frame
=
cv2
.
imread
(
image_path
)
frame_rgb
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2RGB
)
frame_resized
=
cv2
.
resize
(
frame_rgb
,
(
width
,
height
))
input_data
=
np
.
expand_dims
(
frame_resized
,
axis
=
0
)
# Normalize pixel values if using a floating model (i.e., if the model is non-quantized)
if
floating_model
:
input_data
=
(
np
.
float32
(
input_data
)
-
input_mean
)
/
input_std
# Perform model inference
interpreter
.
set_tensor
(
input_details
[
0
][
'index'
],
input_data
)
interpreter
.
invoke
()
# Get prediction from output tensor
output_data
=
interpreter
.
get_tensor
(
output_details
[
0
][
'index'
])
prediction
=
np
.
argmax
(
output_data
)
# Get label name
label_name
=
labels
[
prediction
]
# Only print label name if it's different from the previous label
if
label_name
!=
prev_label
:
print
(
label_name
)
prev_label
=
label_name
# Draw label on the image
cv2
.
putText
(
frame
,
label_name
,
(
50
,
50
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
1
,
(
0
,
0
,
255
),
2
,
cv2
.
LINE_AA
)
# Show the image in a window
cv2
.
imshow
(
'Indoor Scenes'
,
frame
)
cv2
.
waitKey
(
0
)
# Wait until a key is pressed to proceed to the next image
# Save the labeled image in the output folder
output_image_path
=
os
.
path
.
join
(
output_folder
,
image_file
)
cv2
.
imwrite
(
output_image_path
,
frame
)
# Clean up
cv2
.
destroyAllWindows
()
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