Spaces:
Runtime error
Runtime error
initiated
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
# from PIL import Image, ImageDraw
|
5 |
+
# import imutils
|
6 |
+
# import easyocr
|
7 |
+
# import os
|
8 |
+
from fastai.vision.all import *
|
9 |
+
import pathlib
|
10 |
+
import platform
|
11 |
+
import os
|
12 |
+
# import shutil
|
13 |
+
from src.fruit_classifier.config.configuration import ConfigurationManager
|
14 |
+
|
15 |
+
system_platform = platform.system()
|
16 |
+
if system_platform == 'Windows': pathlib.PosixPath = pathlib.WindowsPath
|
17 |
+
|
18 |
+
config_manager = ConfigurationManager()
|
19 |
+
config = config_manager.get_training_config()
|
20 |
+
MODEL_ROOT = config.trained_model_path
|
21 |
+
MODEL_NAME = config.params_model_name
|
22 |
+
MODEL_PATH = os.path.join(MODEL_ROOT, MODEL_NAME)
|
23 |
+
|
24 |
+
def main():
|
25 |
+
st.title("Fruit Classifier")
|
26 |
+
|
27 |
+
# Use st.camera to capture images from the user's camera
|
28 |
+
img_file_buffer = st.camera_input(label='Please, take a photo of a fruit', key='fruit')
|
29 |
+
|
30 |
+
# Check if an image is captured
|
31 |
+
if img_file_buffer is not None:
|
32 |
+
# Convert the image to a NumPy array
|
33 |
+
# image = Image.open(img_file_buffer)
|
34 |
+
# image_np = np.array(image)
|
35 |
+
# resized_image = cv2.resize(image_np, (640, 640))
|
36 |
+
# resized_image = resized_image.astype(np.uint8)
|
37 |
+
# resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
|
38 |
+
image = cv2.imread(img_file_buffer)
|
39 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
40 |
+
cv2.imwrite('fruit_image.jpg', image)
|
41 |
+
|
42 |
+
model = load_learner(MODEL_PATH)
|
43 |
+
model_output = model.predict('fruit_image.jpg')
|
44 |
+
|
45 |
+
prob_idx = model.dls.vocab.index(model_output[0])
|
46 |
+
st.write(f'{model_output[0].title()} is depicted in the photo with {model_output[-1][prob_idx]:.4f} confidence.')
|
47 |
+
|
48 |
+
st.session_state.pop("fruit")
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
main()
|