-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
102 lines (85 loc) · 3.94 KB
/
app.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
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
import streamlit as st
from PIL import Image
import exifread
import tempfile
import os
from moviepy.editor import VideoFileClip
from hachoir.parser import createParser
from hachoir.metadata import extractMetadata
# Function to extract metadata from the image file
def get_metadata(image_file_path):
with open(image_file_path, "rb") as f:
tags = exifread.process_file(f)
taken_date = tags.get('EXIF DateTimeOriginal', None)
taken_location = tags.get('GPS GPSLatitude', None), tags.get('GPS GPSLongitude', None)
return taken_date, taken_location
# Function to extract GPS metadata from video file
def get_video_metadata(video_file_path):
parser = createParser(video_file_path)
metadata = extractMetadata(parser)
taken_date = metadata.get('creation_date', None)
taken_location = None, None
for line in metadata.exportPlaintext():
if 'GPS latitude' in line.lower():
taken_location = line.split(':')[-1].strip(), taken_location[1]
elif 'GPS longitude' in line.lower():
taken_location = taken_location[0], line.split(':')[-1].strip()
return taken_date, taken_location
# Function to convert .mov to .mp4 using moviepy
def convert_mov_to_mp4(mov_file_path):
mp4_path = os.path.splitext(mov_file_path)[0] + '.mp4'
video_clip = VideoFileClip(mov_file_path)
video_clip.write_videofile(mp4_path, codec="libx264")
video_clip.close()
return mp4_path
st.title("Photo Test Viewer")
#st.write("Upload your photos and videos and see their metadata.")
# File uploader
#uploaded_file = st.file_uploader("Choose a photo or video...", type=["jpg", "jpeg", "png", "mp4", "mov"])
#camera input - ska funka även med iPhone Kameran
uploaded_file = st.camera_input(label="ProbeTest", key="camera_input_file")
if uploaded_file is not None:
# To read image file buffer as bytes:
bytes_data = uploaded_file.getvalue()
# Check the type of bytes_data:
# Should output: <class 'bytes'>
st.write(type(bytes_data))
if uploaded_file is not None:
# Create a temporary file to save the uploaded content
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
tmp_filename = tmp_file.name
tmp_file.write(uploaded_file.read())
file_type = uploaded_file.type.split('/')[0]
if file_type == "image":
# Display uploaded image and metadata
image = Image.open(tmp_filename)
st.image(image, caption="Uploaded Image", use_column_width=True)
taken_date, taken_location = get_metadata(tmp_filename)
st.write("Metadata:")
st.write(f"Taken Date: {taken_date}")
if taken_location[0] is not None and taken_location[1] is not None:
st.write(f"Taken Location: Latitude {taken_location[0]}, Longitude {taken_location[1]}")
else:
st.write("Location information not available.")
elif file_type == "video":
# Handle .mov videos
if uploaded_file.type == "video/quicktime":
st.warning("Converting .mov video to .mp4. Please wait...")
tmp_filename = convert_mov_to_mp4(tmp_filename)
# Display uploaded video and metadata
st.video(tmp_filename, format='video/mp4')
taken_date, taken_location = get_video_metadata(tmp_filename)
duration = VideoFileClip(tmp_filename).duration
resolution = VideoFileClip(tmp_filename).size
st.write("Metadata:")
st.write(f"Taken Date: {taken_date}")
if taken_location[0] is not None and taken_location[1] is not None:
st.write(f"Taken Location: Latitude {taken_location[0]}, Longitude {taken_location[1]}")
else:
st.write("Location information not available.")
st.write(f"Duration: {duration:.2f} seconds")
st.write(f"Resolution: {resolution[0]}x{resolution[1]}")
else:
st.write("Unsupported file format. Please upload an image or video.")
# Remove the temporary file after displaying the content
os.remove(tmp_filename)