Spaces:
Runtime error
Runtime error
initial version of sticker-defect app
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import time
|
4 |
+
from fastai.vision.all import load_learner
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load the exported model
|
8 |
+
model = load_learner("model.pkl")
|
9 |
+
|
10 |
+
# Function to classify an image
|
11 |
+
def classify_images(imgs):
|
12 |
+
start_time = time.time()
|
13 |
+
results = []
|
14 |
+
for img in imgs:
|
15 |
+
# Convert gradio image to PIL Image
|
16 |
+
pil_img = Image.fromarray(img.astype('uint8'), 'RGB')
|
17 |
+
|
18 |
+
# Perform inference
|
19 |
+
pred_class, pred_idx, pred_probs = model.predict(pil_img)
|
20 |
+
|
21 |
+
# Format output
|
22 |
+
output = f"Image Name: {img.name} - Category: {pred_class}"
|
23 |
+
results.append(output)
|
24 |
+
|
25 |
+
# Calculate total inference time
|
26 |
+
inference_time = time.time() - start_time
|
27 |
+
|
28 |
+
# Append total inference time to results
|
29 |
+
results.append(f"Total Inference Time: {inference_time:.2f} seconds")
|
30 |
+
|
31 |
+
return results
|
32 |
+
|
33 |
+
# Create Gradio interface
|
34 |
+
input_component = gr.inputs.Image(label="Upload Image", type="file", multiple_files=True)
|
35 |
+
output_component = gr.outputs.Textbox(label="Classification Results")
|
36 |
+
interface = gr.Interface(fn=classify_images, inputs=input_component, outputs=output_component, title="Image Classifier")
|
37 |
+
|
38 |
+
# Launch the Gradio interface
|
39 |
+
interface.launch()
|