FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Image_classificationUsingCNN/main.py at main · Aadi-stack/Image_classificationUsingCNN · GitHub
Aadi-stack
/
Image_classificationUsingCNN
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Image_classificationUsingCNN
/
main.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (55 loc) · 2.08 KB
Breadcrumbs
Image_classificationUsingCNN
/
main.py
Copy path
File metadata and controls
83 lines (55 loc) · 2.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import
os
import
tensorflow
as
tf
from
flask
import
Flask
,
render_template
,
request
,
send_from_directory
app
=
Flask
(
__name__
,
template_folder
=
'template'
)
dir_path
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
UPLOAD_FOLDER
=
"Uploads"
STATIC_FOLDER
=
"static"
# Load model
cnn_model
=
tf
.
keras
.
models
.
load_model
(
STATIC_FOLDER
+
"/models/"
+
"dog_cat_M.h5"
)
IMAGE_SIZE
=
256
# Preprocess an image
def
preprocess_image
(
image
):
image
=
tf
.
image
.
decode_jpeg
(
image
,
channels
=
3
)
image
=
tf
.
image
.
resize
(
image
, [
IMAGE_SIZE
,
IMAGE_SIZE
])
image
/=
255.0
# normalize to [0,1] range
return
image
# Read the image from path and preprocess
def
load_and_preprocess_image
(
path
):
image
=
tf
.
io
.
read_file
(
path
)
return
preprocess_image
(
image
)
# Predict & classify image
def
classify
(
model
,
image_path
):
preprocessed_imgage
=
load_and_preprocess_image
(
image_path
)
preprocessed_imgage
=
tf
.
reshape
(
preprocessed_imgage
, (
1
,
IMAGE_SIZE
,
IMAGE_SIZE
,
3
)
)
prob
=
cnn_model
.
predict
(
preprocessed_imgage
)
label
=
"Cat"
if
prob
[
0
][
0
]
>=
0.5
else
"Dog"
classified_prob
=
prob
[
0
][
0
]
if
prob
[
0
][
0
]
>=
0.5
else
1
-
prob
[
0
][
0
]
return
label
,
classified_prob
# home page
@
app
.
route
(
"/"
)
def
home
():
return
render_template
(
"home.html"
)
@
app
.
route
(
"/classify"
,
methods
=
[
"POST"
,
"GET"
])
def
upload_file
():
if
request
.
method
==
"GET"
:
return
render_template
(
"home.html"
)
else
:
file
=
request
.
files
[
"image"
]
upload_image_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
file
.
filename
)
print
(
upload_image_path
)
file
.
save
(
upload_image_path
)
label
,
prob
=
classify
(
cnn_model
,
upload_image_path
)
prob
=
round
((
prob
*
100
),
2
)
return
render_template
(
"classify.html"
,
image_file_name
=
file
.
filename
,
label
=
label
,
prob
=
prob
)
@
app
.
route
(
"/classify/<filename>"
)
def
send_file
(
filename
):
return
send_from_directory
(
UPLOAD_FOLDER
,
filename
)
if
__name__
==
"__main__"
:
app
.
debug
=
True
app
.
run
(
debug
=
True
)
app
.
debug
=
True
Back
|
FazBrowse Home
|
New Git URL