-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCV_Workshop_Github.py
51 lines (37 loc) · 1.18 KB
/
CV_Workshop_Github.py
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
import streamlit as st
import requests
visionKey = "KEY"
visionEndpoint = "ENDPOINT"
def get_image_features(img_url):
analyze_url = f"{visionEndpoint}/vision/v3.1/analyze"
headers = {
'Ocp-Apim-Subscription-Key': visionKey
}
params = {
'visualFeatures': 'Categories,Description,Color',
'language': 'en',
}
data = {'url': img_url}
response = requests.post(analyze_url, headers=headers, params=params, json=data)
if response.status_code != 200:
st.write("Error:", response.status_code)
st.write(response.text)
return None
else:
return response.json()
# Streamlit webapp
st.title('Computer Vision Workshop')
img_url = st.text_input('Enter Image URL')
if img_url:
st.header('Original Image')
st.image(img_url, use_column_width=True)
parsed = get_image_features(img_url)
if parsed:
st.header('JSON Response')
st.json(parsed)
st.header('Image Caption')
try:
caption = parsed['description']['captions'][0]['text']
st.write(caption)
except (KeyError, IndexError) as e:
st.write("Couldn't find a caption for this image.")