File size: 5,566 Bytes
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
79bf7a7
 
 
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
import os
import sys
import boto3
from botocore.config import Config
import getpass
import streamlit as st


from opensearchpy import OpenSearch, RequestsHttpConnection


rekog_client = boto3.client('rekognition',aws_access_key_id=st.secrets['user_access_key'],
                aws_secret_access_key=st.secrets['user_secret_key'],region_name='us-east-1')


def extract_image_metadata(img):
    res = rekog_client.detect_labels(
    Features= [ "GENERAL_LABELS","IMAGE_PROPERTIES" ],
    Image = { 
        
        "Bytes":img
    },
    MaxLabels = 10,
    MinConfidence = 80.0,
    Settings = { 
    #       "GeneralLabels": { 
    #          "LabelCategoryExclusionFilters": [ "string" ],
    #          "LabelCategoryInclusionFilters": [ "string" ],
    #          "LabelExclusionFilters": [ "string" ],
    #          "LabelInclusionFilters": [ "string" ]
    #       },
        "ImageProperties": { 
            "MaxDominantColors": 5
        }
    }
    )
    objects_category_color = {}
    objects_category_color_masked = {}
    
    def add_span(x,type):
        if(type == 'obj'):
            return "<span style='color:#e28743;font-weight:bold'>"+x+"</span>"
        if(type == 'cat'):
            return "<span style='color:green;font-style:italic'>"+x+"</span>"
        if(type == 'color'):
            return "<span style='color:"+x+";font-weight:bold'>"+x+"</span>"
        
    
    for label in res['Labels']:
        objects_category_color_masked[add_span(label['Name'],'obj')]={'categories':[],'color':""}
        objects_category_color[label['Name']] = {'categories':[],'color':""}
        if(len(label['Categories'])!=0):
            for category in label['Categories']:
                objects_category_color_masked[add_span(label['Name'],'obj')]['categories'].append(add_span(category['Name'].lower(),'cat'))
                objects_category_color[label['Name']]['categories'].append(category['Name'].lower())


        if(len(label['Instances'])!=0):
            for instance in label['Instances']:
                if(len(instance['DominantColors'])!=0):
                    objects_category_color[label['Name']]['color'] = instance['DominantColors'][0]['SimplifiedColor']
                    objects_category_color_masked["<span style='color:#e28743;font-weight:bold'>"+label['Name']+"</span>"]['color'] = add_span(instance['DominantColors'][0]['SimplifiedColor'],'color')
                
        st.session_state.input_rekog_directoutput = objects_category_color_masked
    objects = []
    categories = []
    colors = []
    for key in objects_category_color.keys():
        if(key.lower() not in objects):
            objects.append(key.lower())
        categories.append(" ".join(set(objects_category_color[key]['categories'])))
        if(objects_category_color[key]['color']!=''):
            colors.append(objects_category_color[key]['color'].lower())
            
    objects = " ".join(set(objects))
    categories = " ".join(set(categories))
    colors = " ".join(set(colors))
    
    print("^^^^^^^^^^^^^^^^^^")
    print(colors+ " " + objects + " " + categories)
    
    return colors+ " " + objects + " " + categories

def call(a,b):
    print("'''''''''''''''''''''''")
    print(b)
    
    if(st.session_state.input_is_rewrite_query == 'enabled' and st.session_state.input_rewritten_query!=""):
        
        
        #st.session_state.input_rewritten_query['query']['bool']['should'].pop()
        st.session_state.input_rewritten_query['query']['bool']['should'].append( {
                    "simple_query_string": {
                    
                        "query": a + " " + b,
                        "fields":['description','rekog_all^3']
                    
                    }
                })
        rekog_query = st.session_state.input_rewritten_query
        
    else:
        rekog_query = { "query":{
                "simple_query_string": {
                  
                    "query": a + " " + b,
                    "fields":['description','rekog_all^3']
                  
                }
              }
            }
        st.session_state.input_rewritten_query = rekog_query
        
    # response = aos_client.search(
    #     body = rekog_query,
    #     index = 'demo-retail-rekognition'
    #     #pipeline = 'RAG-Search-Pipeline'
    # )
    
    
    # hits = response['hits']['hits']
    # print("rewrite-------------------------")
    # arr = []
    # for doc in hits:
    #     # if('b5/b5319e00' in doc['_source']['image_s3_url'] ):
    #     #     filter_out +=1
    #     #     continue
        
    #     res_ = {"desc":doc['_source']['text'].replace(doc['_source']['metadata']['rekog_all']," ^^^ " +doc['_source']['metadata']['rekog_all']),
    #             "image_url":doc['_source']['metadata']['image_s3_url']}
    #     if('highlight' in doc):
    #         res_['highlight'] = doc['highlight']['text']
    #     # if('caption_embedding' in doc['_source']):
    #     #     res_['sparse'] = doc['_source']['caption_embedding']
    #     # if('query_sparse' in response_ and len(arr) ==0 ):
    #     #     res_['query_sparse'] = response_["query_sparse"]
    #     res_['id'] = doc['_id']
    #     res_['score'] = doc['_score']
    #     res_['title'] = doc['_source']['text']
    #     res_['rekog'] = {'color':doc['_source']['metadata']['rekog_color'],'category': doc['_source']['metadata']['rekog_categories'],'objects':doc['_source']['metadata']['rekog_objects']}
           
    #     arr.append(res_)
            

    
    # return arr