Skip to content

Commit

Permalink
Merge pull request #88 from khengyun/feat/backend/api-food-classifica…
Browse files Browse the repository at this point in the history
…tion

update detect model with camera
  • Loading branch information
tien-thanh-hua authored Oct 24, 2023
2 parents da38530 + e907e25 commit d3d473b
Show file tree
Hide file tree
Showing 21 changed files with 2,807 additions and 145 deletions.
36 changes: 35 additions & 1 deletion api/Food_Classifier/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
from fastapi import FastAPI
from src.predict import predict_dish_onnx
from src.predict import predict_dish_onnx, predict_dish_onnx_from_base64
from starlette.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
import logging
import onnxruntime
import uvicorn
import time
from pydantic import BaseModel

app = FastAPI()
class ImageBase64Request(BaseModel):
image_base64: str
# Thêm CORS Middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Thay đổi thành nguồn của ứng dụng JavaScript của bạn
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

# mount data
app.mount("/images", StaticFiles(directory="images"), name="images")
Expand Down Expand Up @@ -44,5 +57,26 @@ async def predict_dish(url: str):

return HTMLResponse(content=html_content)



@app.post("/predict_dish")
async def predict_dish(request_data: ImageBase64Request):
class_list_file = "model/food_model_labels.txt"

logging.info(f"Received image_base64: {request_data.image_base64}")

start_time = time.time()

predicted_dish = predict_dish_onnx_from_base64(request_data.image_base64, session_onnx, class_list_file)
end_time = time.time()

response_time = end_time - start_time

return {
"predicted_dish": predicted_dish,
"inference_time": response_time,
}


if __name__ == '__main__':
uvicorn.run('app:app', port=8000, host='0.0.0.0', reload=True)
Binary file added api/Food_Classifier/model/food_model.onnx
Binary file not shown.
28 changes: 27 additions & 1 deletion api/Food_Classifier/src/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import tensorflow as tf
import onnxruntime

import base64


def predict_dish_onnx(url, session, class_list_file):
Expand Down Expand Up @@ -32,6 +32,32 @@ def predict_dish_onnx(url, session, class_list_file):

return predicted_class, url

def predict_dish_onnx_from_base64(image_base64, session, class_list_file):
# image_base64 = image_base64.split(';')[1]
# Decode the base64 image data and convert it to a NumPy array
image_bytes = base64.b64decode(image_base64)
image = Image.open(io.BytesIO(image_bytes))


image = image.convert('RGB')

image = image.resize((224, 224))
image = np.array(image).astype(np.float32)
image = image / 255.0

input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name

input_data = np.expand_dims(image, axis=0)
predictions = session.run([output_name], {input_name: input_data})[0][0]

with open(class_list_file, 'r') as file:
class_list = file.read().split('\n')

predicted_class_index = np.argmax(predictions)
predicted_class = class_list[predicted_class_index]

return predicted_class

# convert code to __main__

Expand Down
24 changes: 12 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ services:
networks:
- my-network

tomcat-app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
restart: always
networks:
- my-network
depends_on:
- sqlserver
- sqlserver.configurator
# tomcat-app:
# build:
# context: .
# dockerfile: Dockerfile
# ports:
# - "8081:8080"
# restart: always
# networks:
# - my-network
# depends_on:
# - sqlserver
# - sqlserver.configurator

networks:
my-network:
Expand Down
124 changes: 0 additions & 124 deletions src/main/webapp/WEB-INF/jspf/LangOption/langviewbutton.jspf

This file was deleted.

14 changes: 7 additions & 7 deletions src/main/webapp/WEB-INF/jspf/common/components/header.jspf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<%-- any content can be specified here e.g.: --%>
<%@ include file="../imports/base.jspf" %>


<%@ page pageEncoding="UTF-8" %>

<!-- SUB NAVBAR -->
Expand All @@ -16,11 +14,11 @@

</div>
<div>
<a class="text-light px-3" href="https://github.com/tien-thanh-hua/FFood" target="_blank">
<a class="text-light px-3" href="https://github.com/khengyun/FFood-shop" target="_blank">
<!-- <i class="fab fa-facebook fa-sm fa-fw me-2"></i>-->
Facebook
</a>
<a class="text-light" href="https://github.com/tien-thanh-hua/FFood" target="_blank">
<a class="text-light" href="https://github.com/khengyun/FFood-shop" target="_blank">
<!-- <i class="fab fa-facebook fa-sm fa-fw me-2"></i>-->
Zalo
</a>
Expand Down Expand Up @@ -48,16 +46,18 @@
<input class="form-control border-1 input-box bg-100" type="search" placeholder="Tìm món ăn" aria-label="Tìm món ăn" id="search-bar"
value="" onblur="searchFoodByKeyword()"/>
</div>

<%@ include file="../../hyperfeat/fooddetechbutton.jspf" %>
<!-- Cart button -->
<button type="button" class="btn ps-1 pe-2 py-0 position-relative nav-icon text-decoration-none my-auto text-primary fs-2 me-4"
data-bs-toggle="modal" data-bs-target="#cart-modal">
<i class="fas fa-fw fa-cart-arrow-down"></i>
<span id="cart-badge" class="position-absolute top-0 start-100 translate-middle px-2 fs-0 bg-danger rounded-pill text-white">
<span class="visually-hidden">Giỏ hàng</span>
</span>
<!-- </span> -->
</button>
<%@ include file="../../LangOption/langviewbutton.jspf" %>

<%@ include file="../../hyperfeat/langviewbutton.jspf" %>



<!-- Login button -->
Expand Down
2 changes: 2 additions & 0 deletions src/main/webapp/WEB-INF/jspf/common/imports/resources.jspf
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@

<!-- JavaScript -->
<script src="<%= request.getContextPath() + "vendors/jquery-3.7.0.js"%>"></script>
<script src="<%= request.getContextPath() + "assets/js/lib/webcam.min.js"%>"></script>

<script src="https://unpkg.com/@phosphor-icons/web"></script>
Loading

0 comments on commit d3d473b

Please sign in to comment.