File size: 2,068 Bytes
22152f1
55ea004
 
 
 
 
 
 
 
 
 
 
22152f1
 
 
55ea004
 
 
d910827
55ea004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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

# Function to call Clarifai API
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

# Streamlit interface
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:
        # Display each detected item
        for concept in region.data.concepts:
            name = concept.name
            value = round(concept.value, 4)
            st.write(f"{name}: {value}")

# Run this with `streamlit run your_script_name.py`