Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-323
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
1
Merge Requests
1
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
2023-323
2023-323
Commits
4844f75c
Commit
4844f75c
authored
May 29, 2023
by
Kareshaan Logeswaran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
created It20236946 backend
parent
11f1b39b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
0 deletions
+47
-0
api 2/harvest.py
api 2/harvest.py
+47
-0
No files found.
api 2/harvest.py
0 → 100644
View file @
4844f75c
from
fastapi
import
FastAPI
,
File
,
UploadFile
from
fastapi.middleware.cors
import
CORSMiddleware
from
fastapi.responses
import
FileResponse
,
HTMLResponse
import
uvicorn
import
numpy
as
np
from
io
import
BytesIO
from
PIL
import
Image
import
tensorflow
as
tf
app
=
FastAPI
()
# CORS middleware for handling cross-origin requests
app
.
add_middleware
(
CORSMiddleware
,
allow_origins
=
[
"*"
],
allow_methods
=
[
"*"
],
allow_headers
=
[
"*"
],
)
MODEL
=
tf
.
keras
.
models
.
load_model
(
"../Models_main/version11"
)
CLASS_NAMES
=
[
"Flowering Stage"
,
"Maturity Stage"
,
"Out of Domain"
,
"Re-productive Stage"
,
"Ripening Stage"
]
def
read_file_as_image
(
data
)
->
np
.
ndarray
:
image
=
np
.
array
(
Image
.
open
(
BytesIO
(
data
)))
return
image
@
app
.
post
(
"/predict"
)
async
def
predict
(
file
:
UploadFile
=
File
(
...
)):
image
=
read_file_as_image
(
await
file
.
read
())
img_batch
=
np
.
expand_dims
(
image
,
0
)
predictions
=
MODEL
.
predict
(
img_batch
)
predicted_class
=
CLASS_NAMES
[
np
.
argmax
(
predictions
[
0
])]
confidence
=
np
.
max
(
predictions
[
0
])
return
{
'class'
:
predicted_class
,
'confidence'
:
float
(
confidence
)
}
@
app
.
get
(
"/"
,
response_class
=
HTMLResponse
)
async
def
get_index
():
with
open
(
'paddyStage.html'
,
'r'
)
as
f
:
return
f
.
read
()
if
__name__
==
"__main__"
:
uvicorn
.
run
(
app
,
host
=
'localhost'
,
port
=
8010
)
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