Skip to content

Commit

Permalink
tes
Browse files Browse the repository at this point in the history
  • Loading branch information
satrio142 authored Dec 16, 2023
1 parent 9f0472f commit b083754
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 92 deletions.
34 changes: 17 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install --upgrade pip

RUN pip install -r requirements.txt

COPY . .

ENV PYTHONUNBUFFERED=1

EXPOSE 8080

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install --upgrade pip

RUN pip install -r requirements.txt

COPY . .

ENV PYTHONUNBUFFERED=1

EXPOSE 8080

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
![TF](https://github.com/Mubazir-Bangkit-2023/mubazir-machine-learning/assets/95016158/cf4884a9-2a4d-4148-a5a7-24a57c009da0)

# Food Image Classification
Developing a deep learning model for food image classification using Tensorflow: Creating a Convolutional Neural Network (CNN) model to categorize fruits and vegetables with Tensorflow.

## Project Overview
The objective of this project is to construct a model capable of precisely categorizing images of fruits and vegetables into predetermined classes. This model has the potential to be incorporated into applications, enabling the automatic identification and classification of the fruit or vegetable type and determining its freshness based on images uploaded by users.

## Dataset
The dataset employed in this project comprises images showcasing a variety of fruits and vegetables systematically arranged into distinct categories. Every image is annotated with the specific category of the fruit or vegetable and its corresponding freshness classification.

## Feature
- Data Augmentation
- CNN (Convolutional Neural Networks)

## Requirements
- Tensorflow
- Matplothlib
- Numpy
- Pillow
- Scikit-learn

## Results
The model achieved a test accuracy of XX% on the test dataset. The training and validation loss/accuracy plots can be found here.

## Future Work
- Expand the data set to include a wider variety of fruits and vegetables
- Explore advanced architectures and methodologies in experiments to enhance accuracy further.
138 changes: 77 additions & 61 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,77 @@
import os
import uvicorn
import numpy as np
import traceback
from io import BytesIO

from pydantic import BaseModel
from urllib.request import Request
from fastapi import FastAPI, Response, UploadFile
from utils import load_image_into_numpy_array
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img, img_to_array

app = FastAPI()

@app.post("/predict_image")
async def predict_image(img: UploadFile, response: Response):
try:
# Checking if it's an image
if img.content_type not in ["image/jpeg", "image/png"]:
response.status_code = 400
return "File is Not an Image"

# Read file content
file_content = await img.read()

# Convert bytes to a file-like object
file_like_object = BytesIO(file_content)

# Load the image from the file-like object
img = load_img(file_like_object, target_size=(150, 150))

# Load the model
model = load_model('model.h5')

# Prepare the image for prediction
image = img_to_array(img)
image = np.expand_dims(image, axis=0)

# Predict the class of the image
arr = model.predict(image)

if arr[0][0]==1:
labels='Fresh Apples'
elif arr[0][1]==1:
labels='Fresh Banana'
elif arr[0][2]==1:
labels='Fresh Oranges'
elif arr[0][3]==1:
labels='Rotten Apples'
elif arr[0][4]==1:
labels='Rotten Banana'
elif arr[0][5]==1:
labels='Rotten Oranges'

return {"result":labels}
except Exception as e:
traceback.print_exc()
response.status_code = 500
return str(e)
import os
import uvicorn
import numpy as np
import traceback
from io import BytesIO

from pydantic import BaseModel
from urllib.request import Request
from fastapi import FastAPI, Response, UploadFile
from utils import load_image_into_numpy_array
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img, img_to_array

app = FastAPI()

@app.post("/predict_image")
async def predict_image(img: UploadFile, response: Response):
try:
# Checking if it's an image
if img.content_type not in ["image/jpeg", "image/png"]:
response.status_code = 400
return "File is Not an Image"

# Read file content
file_content = await img.read()

# Convert bytes to a file-like object
file_like_object = BytesIO(file_content)

# Load the image from the file-like object
img = load_img(file_like_object, target_size=(150, 150))

# Load the model
model = load_model('model1.h5')

# Prepare the image for prediction
image = img_to_array(img)
image = np.expand_dims(image, axis=0)

# Predict the class of the image
arr = model.predict(image)

if arr[0][0]==1:
labels='Fresh Apples'
elif arr[0][1]==1:
labels='Fresh Banana'
elif arr[0][2]==1:
labels='Fresh Cucumber'
elif arr[0][3]==1:
labels='Fresh Okra'
elif arr[0][4]==1:
labels='Fresh Oranges'
elif arr[0][5]==1:
labels='Fresh Potato'
elif arr[0][6]==1:
labels='Fresh Tomato'
elif arr[0][7]==1:
labels='Rotten Apples'
elif arr[0][8]==1:
labels='Rotten Banana'
elif arr[0][9]==1:
labels='Rotten Cucumber'
elif arr[0][10]==1:
labels='Rotten Okra'
elif arr[0][11]==1:
labels='Rotten Oranges'
elif arr[0][12]==1:
labels='Rotten Potato'
elif arr[0][13]==1:
labels='Rotten Tomato'

return {"result":labels}
except Exception as e:
traceback.print_exc()
response.status_code = 500
return str(e)
2 changes: 2 additions & 0 deletions dataset
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM KAGGLE :
https://www.kaggle.com/datasets/raghavrpotdar/fresh-and-stale-images-of-fruits-and-vegetables
27 changes: 27 additions & 0 deletions labels.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'Fresh Apples'

'Fresh Banana'

'Fresh Cucumber'

'Fresh Okra'

'Fresh Oranges'

'Fresh Potato'

'Fresh Tomato'

'Rotten Apples'

'Rotten Banana'

'Rotten Cucumber'

'Rotten Okra'

'Rotten Oranges'

'Rotten Potato'

'Rotten Tomato'
Binary file added model1.h5
Binary file not shown.
16 changes: 8 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
fastapi
pydantic
uvicorn[standard]
python-multipart
numpy
Pillow
opencv-python
tensorflow
fastapi
pydantic
uvicorn[standard]
python-multipart
numpy
Pillow
opencv-python
tensorflow
keras
12 changes: 6 additions & 6 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

from PIL import Image
from io import BytesIO

def load_image_into_numpy_array(data):
import numpy as np

from PIL import Image
from io import BytesIO

def load_image_into_numpy_array(data):
return np.array(Image.open(BytesIO(data)))

0 comments on commit b083754

Please sign in to comment.