Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #754

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AI/Task Submission/Shreeti Mohapatra.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/shivah12/AI-ML.github.io
Binary file added AI/Task Submission/requirements.txt
Binary file not shown.
71 changes: 71 additions & 0 deletions AI/Task Submission/webapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import streamlit as st
import numpy as np
from PIL import Image
import cv2
import tempfile
import os

# Define the app layout
st.title("Image and Video Captioning App")
menu = ["Image", "Video"]
choice = st.sidebar.selectbox("Select an option", menu)

# Define the capture mode
mode = st.sidebar.radio("Select capture mode", ["Local file", "Camera"])

tfile = None # initialize tfile variable to None

# Define the file upload option
if mode == "Local file":
file = st.file_uploader("Upload file", type=[choice.lower(), "mp4", "avi"])
if file is not None:
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(file.read())

# Define the camera capture option
else:
cap = cv2.VideoCapture(0)
if st.button("Capture"):
# Capture the frame
ret, img = cap.read()
# Save the frame to a temporary file
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
cv2.imwrite(tfile.name, cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
cap.release()

# Define the caption input
caption = st.text_input("Enter a caption")

# Display the image/video with caption
if tfile is not None:
st.write(f"**{caption}**")
if choice == "Image":
img = Image.open(tfile.name)
st.image(img)
else:
st.video(tfile.name)


st.title("Image Upload")

# Display file upload widget
uploaded_file = st.file_uploader("Choose an image...", type="jpg")

# Display caption input box
caption = st.text_input('Enter a caption for the image (optional):')

# If a file was uploaded
if uploaded_file is not None:
# Read the file contents
image = Image.open(uploaded_file)
st.image(image, caption=caption, use_column_width=True)
img_array = np.array(image)

# Write the file to a temporary file
with tempfile.NamedTemporaryFile(delete=False) as tfile:
tfile.write(img_array)
st.write('Image saved to:', tfile.name)

# Close the file and delete
os.unlink(tfile.name)