Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,26 @@
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
-
|
| 4 |
-
import numpy as np
|
| 5 |
-
import tensorflow_hub as hub
|
| 6 |
-
from tensorflow.keras.models import load_model
|
| 7 |
-
import cv2
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
# Disable GPU (optional, for CPU-only execution)
|
| 11 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
# Load
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"Model loading failed with error: {e}")
|
| 21 |
-
exit(1)
|
| 22 |
-
|
| 23 |
-
# Define your class labels or categories for predictions
|
| 24 |
-
train_info = [] # Replace with your actual class labels
|
| 25 |
|
| 26 |
-
|
| 27 |
-
with open('labelwithspace.txt', 'r') as file:
|
| 28 |
-
train_info = [line.strip() for line in file.read().splitlines()]
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
img = img[np.newaxis, ...] # Add batch dimension
|
| 35 |
-
predictions = model.predict(img)[0] # Get predictions
|
| 36 |
-
top_class = np.argmax(predictions) # Get the index of the top prediction
|
| 37 |
-
label = train_info[top_class] # Use the index to retrieve the label
|
| 38 |
-
return label
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
gr.Interface(fn=
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
+
__all__ = ['bird', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load categories from label.txt
|
| 6 |
+
with open('label.txt', 'r') as file:
|
| 7 |
+
categories = [line.strip() for line in file.readlines()]
|
| 8 |
|
| 9 |
+
# Load the model
|
| 10 |
+
from fastai.vision.all import *
|
| 11 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
learn = load_learner('model.pkl')
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Define the classification function
|
| 16 |
+
def classify_image(img):
|
| 17 |
+
preds, idx, probs = learn.predict(img)
|
| 18 |
+
return dict(zip(categories, map(float, probs)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Gradio components
|
| 21 |
+
image = gr.components.Image()
|
| 22 |
+
label = gr.components.Label(num_top_classes=3)
|
| 23 |
|
| 24 |
+
# Create and launch the interface
|
| 25 |
+
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)
|
| 26 |
+
intf.launch(inline=False, share=True)
|