Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,26 @@ import gradio as gr
|
|
| 2 |
from fastai.vision.all import *
|
| 3 |
import skimage
|
| 4 |
from fastai.vision.all import PILImage
|
|
|
|
| 5 |
|
| 6 |
learn = load_learner('export.pkl')
|
| 7 |
|
| 8 |
labels = learn.dls.vocab
|
| 9 |
|
| 10 |
-
|
| 11 |
def predict(img):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
examples = ['image1.jpg', 'image2.jpg']
|
| 18 |
|
| 19 |
gr.Interface(fn=predict, inputs=gr.components.Image(), outputs=gr.components.Label(num_top_classes=3), examples=examples).launch()
|
| 20 |
-
|
|
|
|
| 2 |
from fastai.vision.all import *
|
| 3 |
import skimage
|
| 4 |
from fastai.vision.all import PILImage
|
| 5 |
+
import datetime # Import datetime to generate unique filenames
|
| 6 |
|
| 7 |
learn = load_learner('export.pkl')
|
| 8 |
|
| 9 |
labels = learn.dls.vocab
|
| 10 |
|
|
|
|
| 11 |
def predict(img):
|
| 12 |
+
img = PILImage.create(img)
|
| 13 |
+
img = img.resize((512, 512))
|
| 14 |
+
pred, pred_idx, probs = learn.predict(img)
|
| 15 |
+
|
| 16 |
+
# Check if the prediction is "elephant"
|
| 17 |
+
if pred == "elephant":
|
| 18 |
+
# Generate a unique filename using the current timestamp
|
| 19 |
+
filename = f"elephant_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 20 |
+
# Save the image
|
| 21 |
+
img.save(filename)
|
| 22 |
+
|
| 23 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 24 |
|
| 25 |
examples = ['image1.jpg', 'image2.jpg']
|
| 26 |
|
| 27 |
gr.Interface(fn=predict, inputs=gr.components.Image(), outputs=gr.components.Label(num_top_classes=3), examples=examples).launch()
|
|
|