Spaces:
Sleeping
Sleeping
File size: 3,497 Bytes
f8bf7d4 |
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 |
import streamlit as st
from PIL import Image
import datetime
import re
#import os
import json
import hashlib
allowed_image_types = ['webp']
#allowed_image_types = ['jpg', 'jpeg', 'png', 'webp']
# Function to validate email address
def is_valid_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
# Function to extract date and time from image metadata
def get_image_datetime(image_file):
try:
from PIL import ExifTags
image = Image.open(image_file)
exif_data = image._getexif()
if exif_data is not None:
for tag, value in exif_data.items():
if ExifTags.TAGS.get(tag) == 'DateTimeOriginal':
return value
except Exception as e:
st.warning("Could not extract date from image metadata.")
return None
# Streamlit app
st.sidebar.title("Input Form")
# 1. Image Selector
uploaded_filename = st.sidebar.file_uploader("Upload an image", type=allowed_image_types)
image_datetime = None # For storing date-time from image
if uploaded_filename is not None:
# Display the uploaded image
image = Image.open(uploaded_filename)
st.sidebar.image(image, caption='Uploaded Image.', use_column_width=True)
# Extract and display image date-time
image_datetime = get_image_datetime(uploaded_filename)
print(f"[D] image date extracted as {image_datetime}")
metadata = {
"latitude": 23.5,
"longitude": 44,
"author_email": "[email protected]",
"date": None,
"time": None,
}
# 2. Latitude Entry Box
latitude = st.sidebar.text_input("Latitude", metadata.get('latitude', ""))
# 3. Longitude Entry Box
longitude = st.sidebar.text_input("Longitude", metadata.get('longitude', ""))
# 4. Author Box with Email Address Validator
author_email = st.sidebar.text_input("Author Email", metadata.get('author_email', ""))
if author_email and not is_valid_email(author_email):
st.sidebar.error("Please enter a valid email address.")
# 5. date/time
## first from image metadata
if image_datetime is not None:
time_value = datetime.datetime.strptime(image_datetime, '%Y:%m:%d %H:%M:%S').time()
date_value = datetime.datetime.strptime(image_datetime, '%Y:%m:%d %H:%M:%S').date()
else:
time_value = datetime.datetime.now().time() # Default to current time
date_value = datetime.datetime.now().date()
## if not, give user the option to enter manually
date_option = st.sidebar.date_input("Date", value=date_value)
time_option = st.sidebar.time_input("Time", time_value)
# Display submitted data
if st.sidebar.button("Upload"):
# create a dictionary with the submitted data
submitted_data = {
"latitude": latitude,
"longitude": longitude,
"author_email": author_email,
"date": str(date_option),
"time": str(time_option),
"predicted_class": None,
"image_filename": uploaded_filename.name if uploaded_filename else None,
"image_md5": hashlib.md5(uploaded_filename.read()).hexdigest() if uploaded_filename else None,
}
st.write("Submitted Data:")
st.write(f"Latitude: {submitted_data['latitude']}")
st.write(f"Longitude: {submitted_data['longitude']}")
st.write(f"Author Email: {submitted_data['author_email']}")
st.write(f"Date: {submitted_data['date']}")
st.write(f"Time: {submitted_data['time']}")
st.write(f"full dict of data: {json.dumps(submitted_data)}") |