Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2020-045
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
2020-045
2020-045
Commits
58cb3987
Commit
58cb3987
authored
May 17, 2020
by
Peiris M.D.P.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lying position recognition model training.
parent
a1e69526
Pipeline
#1083
failed with stages
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
98 additions
and
0 deletions
+98
-0
classify_image.py
classify_image.py
+98
-0
No files found.
classify_image.py
0 → 100644
View file @
58cb3987
# USAGE
# python classify_image.py --image images/soccer_ball.jpg --model vgg16
# import the necessary packages
from
keras.applications
import
ResNet50
from
keras.applications
import
InceptionV3
from
keras.applications
import
Xception
# TensorFlow ONLY
from
keras.applications
import
VGG16
from
keras.applications
import
VGG19
from
keras.applications
import
imagenet_utils
from
keras.applications.inception_v3
import
preprocess_input
from
keras.preprocessing.image
import
img_to_array
from
keras.preprocessing.image
import
load_img
import
numpy
as
np
import
argparse
import
cv2
# construct the argument parse and parse the arguments
ap
=
argparse
.
ArgumentParser
()
ap
.
add_argument
(
"-i"
,
"--image"
,
required
=
True
,
help
=
"path to the input image"
)
ap
.
add_argument
(
"-model"
,
"--model"
,
type
=
str
,
default
=
"vgg16"
,
help
=
"name of pre-trained network to use"
)
args
=
vars
(
ap
.
parse_args
())
# define a dictionary that maps model names to their classes
# inside Keras
MODELS
=
{
"vgg16"
:
VGG16
,
"vgg19"
:
VGG19
,
"inception"
:
InceptionV3
,
"xception"
:
Xception
,
# TensorFlow ONLY
"resnet"
:
ResNet50
}
# esnure a valid model name was supplied via command line argument
if
args
[
"model"
]
not
in
MODELS
.
keys
():
raise
AssertionError
(
"The --model command line argument should "
"be a key in the `MODELS` dictionary"
)
# initialize the input image shape (224x224 pixels) along with
# the pre-processing function (this might need to be changed
# based on which model we use to classify our image)
inputShape
=
(
224
,
224
)
preprocess
=
imagenet_utils
.
preprocess_input
# if we are using the InceptionV3 or Xception networks, then we
# need to set the input shape to (299x299) [rather than (224x224)]
# and use a different image processing function
if
args
[
"model"
]
in
(
"inception"
,
"xception"
):
inputShape
=
(
299
,
299
)
preprocess
=
preprocess_input
# load our the network weights from disk (NOTE: if this is the
# first time you are running this script for a given network, the
# weights will need to be downloaded first -- depending on which
# network you are using, the weights can be 90-575MB, so be
# patient; the weights will be cached and subsequent runs of this
# script will be *much* faster)
print
(
"[INFO] loading {}..."
.
format
(
args
[
"model"
]))
Network
=
MODELS
[
args
[
"model"
]]
model
=
Network
(
weights
=
"imagenet"
)
# load the input image using the Keras helper utility while ensuring
# the image is resized to `inputShape`, the required input dimensions
# for the ImageNet pre-trained network
print
(
"[INFO] loading and pre-processing image..."
)
image
=
load_img
(
args
[
"image"
],
target_size
=
inputShape
)
image
=
img_to_array
(
image
)
# our input image is now represented as a NumPy array of shape
# (inputShape[0], inputShape[1], 3) however we need to expand the
# dimension by making the shape (1, inputShape[0], inputShape[1], 3)
# so we can pass it through thenetwork
image
=
np
.
expand_dims
(
image
,
axis
=
0
)
# pre-process the image using the appropriate function based on the
# model that has been loaded (i.e., mean subtraction, scaling, etc.)
image
=
preprocess
(
image
)
# classify the image
print
(
"[INFO] classifying image with '{}'..."
.
format
(
args
[
"model"
]))
preds
=
model
.
predict
(
image
)
P
=
imagenet_utils
.
decode_predictions
(
preds
)
# loop over the predictions and display the rank-5 predictions +
# probabilities to our terminal
for
(
i
,
(
imagenetID
,
label
,
prob
))
in
enumerate
(
P
[
0
]):
print
(
"{}. {}: {:.2f}
%
"
.
format
(
i
+
1
,
label
,
prob
*
100
))
# load the image via OpenCV, draw the top prediction on the image,
# and display the image to our screen
orig
=
cv2
.
imread
(
args
[
"image"
])
(
imagenetID
,
label
,
prob
)
=
P
[
0
][
0
]
cv2
.
putText
(
orig
,
"Label: {}, {:.2f}
%
"
.
format
(
label
,
prob
*
100
),
(
10
,
30
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.8
,
(
0
,
0
,
255
),
2
)
cv2
.
imshow
(
"Classification"
,
orig
)
cv2
.
waitKey
(
0
)
\ 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