Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
%
|
| 3 |
+
def single_image(image,mode):
|
| 4 |
+
image = image.resize((128, 128))
|
| 5 |
+
if mode=="rgb":
|
| 6 |
+
return np.array(img).flatten()
|
| 7 |
+
elif mode=="hsv":
|
| 8 |
+
return np.array(img.convert('HSV')).flatten()
|
| 9 |
+
else
|
| 10 |
+
kmeans = sio.load('kmeans.skops')
|
| 11 |
+
sift = cv2.SIFT_create()
|
| 12 |
+
img = img.convert("L")
|
| 13 |
+
keypoints, descriptors = sift.detectAndCompute(img, None)
|
| 14 |
+
words = kmeans.predict(descriptors)
|
| 15 |
+
hist, _ = np.histogram(words, bins=num_clusters, range=(0, num_clusters))
|
| 16 |
+
return np.array(hist)
|
| 17 |
+
def Classify(img, pre,model):
|
| 18 |
+
start_time = time.time()
|
| 19 |
+
image = PIL.Image.open(img)
|
| 20 |
+
file=model+'_'+pre+'.skops'
|
| 21 |
+
loaded_model = sio.load(file)
|
| 22 |
+
predictions = loaded_model.predict(single_image(image,pre))
|
| 23 |
+
end_time = time.time() # Record end time
|
| 24 |
+
elapsed_time_microseconds = (end_time - start_time) * 1_000_000_000 # Calculate elapsed time in microseconds
|
| 25 |
+
|
| 26 |
+
return f"{elapsed_time_microseconds/factor:.2f} ns",f"{elapsed_time_microseconds/1000:.2f} μs",factor, result
|
| 27 |
+
return result
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Create a Gradio interface
|
| 31 |
+
interface = gr.Interface(
|
| 32 |
+
fn=Classify, # Reference to the function
|
| 33 |
+
inputs=[
|
| 34 |
+
gr.Image(type="filepath"),
|
| 35 |
+
gr.Radio(
|
| 36 |
+
["rgb", "hsv", "BoVW"],
|
| 37 |
+
label="Preprocessing",
|
| 38 |
+
info="Choose one"
|
| 39 |
+
)
|
| 40 |
+
# Input for sorting methodgr.Radio(
|
| 41 |
+
["DT", "RF", "XGBoost"],
|
| 42 |
+
label="ML Model",
|
| 43 |
+
info="Choose one"
|
| 44 |
+
)
|
| 45 |
+
],
|
| 46 |
+
outputs=[
|
| 47 |
+
gr.Textbox(label="Class"),
|
| 48 |
+
gr.Textbox(label="Time")
|
| 49 |
+
|
| 50 |
+
]
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Launch the interface
|
| 54 |
+
interface.launch()
|