louiecerv commited on
Commit
838a5f1
·
1 Parent(s): 1885c19

sync with remote

Browse files
Files changed (2) hide show
  1. app.py +80 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import exifread
4
+ from geopy.geocoders import Nominatim
5
+ import io
6
+
7
+ def get_exif_data(image_bytes): # Takes image bytes directly
8
+ try:
9
+ tags = exifread.process_file(image_bytes)
10
+
11
+ gps_latitude = tags.get('GPS GPSLatitude')
12
+ gps_latitude_ref = tags.get('GPS GPSLatitudeRef')
13
+ gps_longitude = tags.get('GPS GPSLongitude')
14
+ gps_longitude_ref = tags.get('GPS GPSLongitudeRef')
15
+ timestamp = tags.get('EXIF DateTimeOriginal')
16
+
17
+ if gps_latitude and gps_longitude and timestamp:
18
+ lat = convert_to_degrees(gps_latitude, gps_latitude_ref)
19
+ lon = convert_to_degrees(gps_longitude, gps_longitude_ref)
20
+ return lat, lon, str(timestamp)
21
+ else:
22
+ return None, None, None
23
+ except Exception as e:
24
+ st.error(f"Error processing image: {e}")
25
+ return None, None, None
26
+
27
+ def convert_to_degrees(value, ref):
28
+ try:
29
+ d = float(value.values[0].num) / float(value.values[0].den)
30
+ m = float(value.values[1].num) / float(value.values[1].den)
31
+ s = float(value.values[2].num) / float(value.values[2].den)
32
+
33
+ degrees = d + (m / 60.0) + (s / 3600.0)
34
+ if ref in ['S', 'W']:
35
+ degrees = -degrees
36
+ return degrees
37
+ except Exception as e:
38
+ st.error(f"Error converting GPS data: {e}")
39
+ return None
40
+
41
+ def get_location(lat, lon):
42
+ try:
43
+ geolocator = Nominatim(user_agent="photo_app")
44
+ location = geolocator.reverse((lat, lon))
45
+ return location.address
46
+ except Exception as e:
47
+ st.error(f"Error getting location: {e}")
48
+ return "Location not found"
49
+
50
+ st.title("Photo Inspection App")
51
+
52
+ uploaded_file = st.camera_input("Take a photo")
53
+
54
+ if uploaded_file is not None:
55
+ try:
56
+ image = Image.open(uploaded_file)
57
+ st.image(image, caption='Captured Image', use_column_width=True)
58
+
59
+ img_bytes = io.BytesIO()
60
+ image.save(img_bytes, format='JPEG')
61
+ img_bytes.seek(0)
62
+
63
+ lat, lon, timestamp = get_exif_data(img_bytes) # Pass bytes to exif function
64
+
65
+ if lat and lon and timestamp:
66
+ st.write(f"**Latitude:** {lat}")
67
+ st.write(f"**Longitude:** {lon}")
68
+ st.write(f"**Timestamp:** {timestamp}")
69
+
70
+ address = get_location(lat, lon)
71
+ st.write(f"**Location:** {address}")
72
+
73
+ google_maps_url = f"https://www.google.com/maps/search/?api=1&query={lat},{lon}"
74
+ st.markdown(f"[View on Google Maps]({google_maps_url})")
75
+
76
+ else:
77
+ st.write("No GPS data found in the image or an error occurred.")
78
+
79
+ except Exception as e:
80
+ st.error(f"An error occurred: {e}")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ Pillow
3
+ exifread
4
+ geopy