Spaces:
Running
Running
wookimchye
commited on
Upload 3 files
Browse files- .env +5 -0
- app.py +54 -0
- requirements.txt +5 -0
.env
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
PredictionEndpoint=https://iti109sectiona-prediction.cognitiveservices.azure.com/
|
3 |
+
PredictionKey=2R51EjhFY9DBGDZfvD3M7LFZbmF1OgqquIVyOh9dJAJbrPpIUQAZJQQJ99ALACqBBLyXJ3w3AAAIACOGYTID
|
4 |
+
ProjectID=6c578dc2-4535-4143-b9b7-a6508ff90b85
|
5 |
+
ModelName=fruit-classifier
|
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
|
2 |
+
from msrest.authentication import ApiKeyCredentials
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
import gradio as gr
|
7 |
+
from io import BytesIO
|
8 |
+
|
9 |
+
#global prediction_endpoint
|
10 |
+
#global prediction_key
|
11 |
+
#global project_id
|
12 |
+
#global model_name
|
13 |
+
|
14 |
+
# Get Configuration Settings
|
15 |
+
load_dotenv()
|
16 |
+
prediction_endpoint = os.getenv('PredictionEndpoint')
|
17 |
+
prediction_key = os.getenv('PredictionKey')
|
18 |
+
project_id = os.getenv('ProjectID')
|
19 |
+
model_name = os.getenv('ModelName')
|
20 |
+
|
21 |
+
def classifyImage(image):
|
22 |
+
|
23 |
+
try:
|
24 |
+
# Convert PIL Image to bytes
|
25 |
+
image_bytes = BytesIO()
|
26 |
+
image.save(image_bytes, format='JPEG')
|
27 |
+
image_bytes = image_bytes.getvalue()
|
28 |
+
|
29 |
+
# Authenticate a client for the training API
|
30 |
+
credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
|
31 |
+
prediction_client = CustomVisionPredictionClient(endpoint=prediction_endpoint, credentials=credentials)
|
32 |
+
|
33 |
+
# Classify test images
|
34 |
+
results = prediction_client.classify_image(project_id, model_name, image_bytes)
|
35 |
+
|
36 |
+
# Loop over each label prediction and print any with probability > 50%
|
37 |
+
for prediction in results.predictions:
|
38 |
+
if prediction.probability > 0.5:
|
39 |
+
summary = ('{} ({:.0%})'.format(prediction.tag_name, prediction.probability))
|
40 |
+
return summary
|
41 |
+
except Exception as ex:
|
42 |
+
return None, f"Error: {str(ex)}"
|
43 |
+
|
44 |
+
title = "Detect 3 types of fruits - Carrot, Cucumber, Pear"
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=classifyImage,
|
47 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
48 |
+
outputs=gr.Textbox(label="Object detection confidence level"),
|
49 |
+
title=title,
|
50 |
+
)
|
51 |
+
|
52 |
+
# Launch the interface
|
53 |
+
interface.launch(share=True)
|
54 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
azure-cognitiveservices-vision-customvision
|
2 |
+
msrest
|
3 |
+
huggingface_hub
|
4 |
+
gradio
|
5 |
+
pillow
|