|
import os |
|
import streamlit as st |
|
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel |
|
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc |
|
from clarifai_grpc.grpc.api.status import status_code_pb2 |
|
import io |
|
|
|
|
|
def get_image_concepts(image_bytes): |
|
channel = ClarifaiChannel.get_grpc_channel() |
|
stub = service_pb2_grpc.V2Stub(channel) |
|
|
|
|
|
PAT = os.getenv('CLARIFAI_PAT') |
|
|
|
USER_ID = 'clarifai' |
|
APP_ID = 'main' |
|
MODEL_ID = 'general-image-detection' |
|
MODEL_VERSION_ID = '1580bb1932594c93b7e2e04456af7c6f' |
|
|
|
metadata = (('authorization', 'Key ' + PAT),) |
|
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) |
|
|
|
post_model_outputs_response = stub.PostModelOutputs( |
|
service_pb2.PostModelOutputsRequest( |
|
user_app_id=userDataObject, |
|
model_id=MODEL_ID, |
|
version_id=MODEL_VERSION_ID, |
|
inputs=[ |
|
resources_pb2.Input( |
|
data=resources_pb2.Data( |
|
image=resources_pb2.Image( |
|
base64=image_bytes |
|
) |
|
) |
|
) |
|
] |
|
), |
|
metadata=metadata |
|
) |
|
|
|
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS: |
|
raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description) |
|
|
|
return post_model_outputs_response.outputs[0].data.regions |
|
|
|
|
|
st.title("Image Detection with Clarifai") |
|
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
if uploaded_file is not None: |
|
image_bytes = uploaded_file.getvalue() |
|
regions = get_image_concepts(image_bytes) |
|
for region in regions: |
|
|
|
for concept in region.data.concepts: |
|
name = concept.name |
|
value = round(concept.value, 4) |
|
st.write(f"{name}: {value}") |
|
|
|
|
|
|