Spaces:
Sleeping
Sleeping
File size: 3,380 Bytes
8055877 |
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 |
import pandas as pd
import streamlit as st
from PIL import Image
from PIL.ExifTags import TAGS
def classify_image_security(exifdata):
# Define criteria for classifying an image as secure or not secure
secure_criteria = {
"Make": "SecureMake",
"Model": "SecureModel",
# We can add more criteria as needed
}
for tag_id, criteria_value in secure_criteria.items():
tag = TAGS.get(tag_id, tag_id)
data = exifdata.get(tag_id, b"") # Ensure data is bytes
if isinstance(data, bytes):
data = data.decode("utf-8", "ignore") # Use "ignore" option to handle decoding errors
if data == criteria_value:
return "Secure"
return "Not Secure"
def extract_geolocation(exifdata):
gps_info = exifdata.get(34853) # EXIF tag for GPS information
if gps_info:
lat = gps_info[2][0] + gps_info[2][1] / 60 + gps_info[2][2] / 3600
lon = gps_info[4][0] + gps_info[4][1] / 60 + gps_info[4][2] / 3600
return lat, lon
return None
def display_map(location):
if location:
st.subheader('Image Location on Map')
map_data = pd.DataFrame({'lat': [location[0]], 'lon': [location[1]]})
st.map(map_data)
else:
st.subheader("No location details")
def load_image():
uploaded_file = st.sidebar.file_uploader(label='Pick an image to test')
if uploaded_file and st.sidebar.button('Load'):
image = Image.open(uploaded_file)
exifdata = image._getexif()
with st.expander('Selected Image', expanded=True):
st.image(image, use_column_width=True)
st.subheader('Image Exchange Information')
info_dict = {
"Filename": uploaded_file.name,
"Image Size": image.size,
"Image Width": image.width,
"Image Height": image.height,
"Image Format": image.format,
"Image Mode": image.mode,
"Image is Animated": getattr(image, "is_animated", False),
"Frames in Image": getattr(image, "n_frames", 1)
}
for label, value in info_dict.items():
st.markdown(f"{label:25}: {value}")
if exifdata is not None:
for tag_id in exifdata:
tag = TAGS.get(tag_id, tag_id)
data = exifdata.get(tag_id, b"") # Ensure data is bytes
if isinstance(data, bytes):
data = data.decode("utf-8", "ignore") # Use "ignore" option to handle decoding errors
st.markdown(f"{tag:25}: {data}")
# Classify image security based on metadata
image_security = classify_image_security(exifdata)
st.subheader('Image Security Classification')
st.write(f"This image is: {image_security}")
# Extract geolocation
location = extract_geolocation(exifdata)
# Display the location on a map
display_map(location)
else:
st.subheader('Image Security Classification')
st.write('This image does not have metadata or does not match secure criteria.')
def main():
st.title('Image EXIF Data Analysis With Location Tracing')
st.sidebar.title("Upload an Image")
st.sidebar.write("Click 'Load' to analyze metadata and location.")
load_image()
if __name__ == '__main__':
main()
|