Spaces:
Running
Running
initial_commit
Browse files- app.py +54 -0
- requirements.txt +12 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
5 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
6 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
8 |
+
#pickle.load(open('energy_model.pkl', 'rb'))
|
9 |
+
#vocab = np.load('w2i.p', allow_pickle=True)
|
10 |
+
print("="*150)
|
11 |
+
print("MODEL LOADED")
|
12 |
+
st.title("img_captioning_app")
|
13 |
+
#st.text("Build with Streamlit and OpenCV")
|
14 |
+
if "photo" not in st.session_state:
|
15 |
+
st.session_state["photo"]="not done"
|
16 |
+
|
17 |
+
c2, c3 = st.columns([2,1])
|
18 |
+
def change_photo_state():
|
19 |
+
st.session_state["photo"]="done"
|
20 |
+
print("="*150)
|
21 |
+
print("RESNET MODEL LOADED")
|
22 |
+
|
23 |
+
@st.cache
|
24 |
+
def load_image(img):
|
25 |
+
im = Image.open(img)
|
26 |
+
return im
|
27 |
+
activities = ["Detection","About"]
|
28 |
+
choice = st.sidebar.selectbox("Select Activty",activities)
|
29 |
+
uploaded_photo = c2.file_uploader("Upload Image",type=['jpg','png','jpeg'], on_change=change_photo_state)
|
30 |
+
camera_photo = c2.camera_input("Take a photo", on_change=change_photo_state)
|
31 |
+
if choice == 'Detection':
|
32 |
+
st.subheader("Face Detection")
|
33 |
+
if st.session_state["photo"]=="done":
|
34 |
+
if uploaded_photo:
|
35 |
+
our_image= load_image(uploaded_photo)
|
36 |
+
elif camera_photo:
|
37 |
+
our_image= load_image(camera_photo)
|
38 |
+
elif uploaded_photo==None and camera_photo==None:
|
39 |
+
our_image= load_image('image.jpg')
|
40 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
41 |
+
model.to(device)
|
42 |
+
max_length = 16
|
43 |
+
num_beams = 4
|
44 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
45 |
+
def predict_step(our_image):
|
46 |
+
if our_image.mode != "RGB":
|
47 |
+
our_image = our_image.convert(mode="RGB")
|
48 |
+
pixel_values = feature_extractor(images=our_image, return_tensors="pt").pixel_values
|
49 |
+
pixel_values = pixel_values.to(device)
|
50 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
51 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
52 |
+
preds = [pred.strip() for pred in preds]
|
53 |
+
return preds
|
54 |
+
predict_step(our_image)
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -f https://download.pytorch.org/whl/torch_stable.html
|
2 |
+
# torchvision==0.7.0+cpu
|
3 |
+
# torch==1.6.0+cpu
|
4 |
+
torch
|
5 |
+
torchvision
|
6 |
+
pandas==1.0.3
|
7 |
+
wget==3.2
|
8 |
+
streamlit==0.71.0
|
9 |
+
spacy==2.3.2
|
10 |
+
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.2.0/en_core_web_sm-2.2.0.tar.gz
|
11 |
+
requests==2.22.0
|
12 |
+
Pillow>=8.1.1
|