FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python-Image-Classifier-with-RESTful-API/app.py at main · Oliver0804/Python-Image-Classifier-with-RESTful-API · GitHub
Oliver0804
/
Python-Image-Classifier-with-RESTful-API
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
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
Python-Image-Classifier-with-RESTful-API
/
app.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (56 loc) · 3.08 KB
Breadcrumbs
Python-Image-Classifier-with-RESTful-API
/
app.py
Copy path
File metadata and controls
61 lines (56 loc) · 3.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
from
flask
import
Flask
,
request
,
render_template
,
jsonify
import
cv2
import
numpy
as
np
app
=
Flask
(
__name__
)
@
app
.
route
(
'/'
,
methods
=
[
'GET'
])
def
index
():
return
render_template
(
'index.html'
)
@
app
.
route
(
'/upload'
,
methods
=
[
'POST'
])
def
upload
():
file
=
request
.
files
[
'image'
]
img
=
cv2
.
imdecode
(
np
.
fromstring
(
file
.
read
(),
np
.
uint8
),
cv2
.
IMREAD_UNCHANGED
)
#img = cv2.imread('image.jpg', cv2.IMREAD_UNCHANGED)
if
img
.
shape
[
2
]
==
4
:
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_RGBA2RGB
)
classes
=
[
'person'
,
'bicycle'
,
'car'
,
'motorbike'
,
'aeroplane'
,
'bus'
,
'train'
,
'truck'
,
'boat'
,
'traffic light'
,
'fire hydrant'
,
'stop sign'
,
'parking meter'
,
'bench'
,
'bird'
,
'cat'
,
'dog'
,
'horse'
,
'sheep'
,
'cow'
,
'elephant'
,
'bear'
,
'zebra'
,
'giraffe'
,
'backpack'
,
'umbrella'
,
'handbag'
,
'tie'
,
'suitcase'
,
'frisbee'
,
'skis'
,
'snowboard'
,
'sports ball'
,
'kite'
,
'baseball bat'
,
'baseball glove'
,
'skateboard'
,
'surfboard'
,
'tennis racket'
,
'bottle'
,
'wine glass'
,
'cup'
,
'fork'
,
'knife'
,
'spoon'
,
'bowl'
,
'banana'
,
'apple'
,
'sandwich'
,
'orange'
,
'broccoli'
,
'carrot'
,
'hot dog'
,
'pizza'
,
'donut'
,
'cake'
,
'chair'
,
'sofa'
,
'pottedplant'
,
'bed'
,
'diningtable'
,
'toilet'
,
'tvmonitor'
,
'laptop'
,
'mouse'
,
'remote'
,
'keyboard'
,
'cell phone'
,
'microwave'
,
'oven'
,
'toaster'
,
'sink'
,
'refrigerator'
,
'book'
,
'clock'
,
'vase'
,
'scissors'
,
'teddy bear'
,
'hair drier'
,
'toothbrush'
]
#net = cv2.dnn.readNet('yolov3-tiny.weights', 'yolov3-tiny.cfg')
net
=
cv2
.
dnn
.
readNetFromDarknet
(
"./yolov3-tiny.cfg"
,
"./yolov3-tiny.weights"
)
net
.
setPreferableBackend
(
cv2
.
dnn
.
DNN_BACKEND_OPENCV
)
net
.
setPreferableTarget
(
cv2
.
dnn
.
DNN_TARGET_CPU
)
blob
=
cv2
.
dnn
.
blobFromImage
(
img
,
scalefactor
=
1
/
255
,
size
=
(
416
,
416
),
swapRB
=
True
)
net
.
setInput
(
blob
)
output_layers
=
net
.
getUnconnectedOutLayersNames
()
layer_outputs
=
net
.
forward
(
output_layers
)
boxes
=
[]
confidences
=
[]
class_ids
=
[]
for
output
in
layer_outputs
:
for
detection
in
output
:
scores
=
detection
[
5
:]
class_id
=
np
.
argmax
(
scores
)
confidence
=
scores
[
class_id
]
if
confidence
>
0.5
:
center_x
=
int
(
detection
[
0
]
*
img
.
shape
[
1
])
center_y
=
int
(
detection
[
1
]
*
img
.
shape
[
0
])
width
=
int
(
detection
[
2
]
*
img
.
shape
[
1
])
height
=
int
(
detection
[
3
]
*
img
.
shape
[
0
])
left
=
int
(
center_x
-
width
/
2
)
top
=
int
(
center_y
-
height
/
2
)
boxes
.
append
([
left
,
top
,
width
,
height
])
confidences
.
append
(
float
(
confidence
))
class_ids
.
append
(
class_id
)
indices
=
cv2
.
dnn
.
NMSBoxes
(
boxes
,
confidences
,
0.5
,
0.4
)
results
=
[]
label
=
''
# 定義變數 label 並賦予一個空字符串
for
i
in
indices
:
i
=
i
[
0
]
box
=
boxes
[
i
]
left
=
box
[
0
]
top
=
box
[
1
]
width
=
box
[
2
]
height
=
box
[
3
]
label
=
classes
[
class_ids
[
i
]]
results
.
append
({
'label'
:
label
,
'left'
:
left
,
'top'
:
top
,
'right'
:
left
+
width
,
'bottom'
:
top
+
height
})
return
jsonify
(
results
)
if
__name__
==
'__main__'
:
app
.
run
(
debug
=
True
,
host
=
'0.0.0.0'
)
Back
|
FazBrowse Home
|
New Git URL