Spaces:
Runtime error
Runtime error
Download utils.py from ThanhNguyen714/TrashDetection: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/ThanhNguyen714/TrashDetection/resolve/main/utils.py
- Command line
-
hf download hf://spaces/ThanhNguyen714/TrashDetection/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/ThanhNguyen714/TrashDetection/resolve/main/utils.py
2.78 kB
| # Hàm lấy tọa độ từ EXIF | |
| from PIL import Image, ExifTags, ImageDraw | |
| import piexif | |
| import gradio as gr | |
| import cv2 | |
| import tempfile | |
| import io | |
| import base64 | |
| import requests | |
| import datetime | |
| codec = 'ISO-8859-1' # or latin-1 | |
| labels = ['Giấy', 'Khác', 'Kim loại', 'Nhựa - nilon'] | |
| colorss = ["#FF3838","#FF9D97","#FF701F","#FFB21D"] | |
| def exif_to_tag(exif_dict): | |
| exif_tag_dict = {} | |
| thumbnail = exif_dict.pop('thumbnail') | |
| exif_tag_dict['thumbnail'] = thumbnail.decode(codec) | |
| for ifd in exif_dict: | |
| exif_tag_dict[ifd] = {} | |
| for tag in exif_dict[ifd]: | |
| try: | |
| element = exif_dict[ifd][tag].decode(codec) | |
| except AttributeError: | |
| element = exif_dict[ifd][tag] | |
| exif_tag_dict[ifd][piexif.TAGS[ifd][tag]["name"]] = element | |
| return exif_tag_dict | |
| def get_coordinates_from_exif(img): | |
| try: | |
| exif = img.info.get('exif') | |
| img_exif = piexif.load(exif) | |
| img_exif = exif_to_tag(img_exif) | |
| # print(img_exif) | |
| if img_exif.get('GPS'): | |
| gps_data = img_exif['GPS'] | |
| print(gps_data) | |
| lat = gps_data['GPSLatitude'] | |
| lon = gps_data['GPSLongitude'] | |
| lat = float(lat[0][0]/lat[0][1]) + float(lat[1][0] / lat[1][1] / 60) + float(lat[2][0]/lat[2][1] / 3600) | |
| lon = float(lon[0][0]/lon[0][1]) + float(lon[1][0] / lon[1][1] / 60) + float(lon[2][0]/lon[2][1] / 3600) | |
| if gps_data['GPSLatitudeRef'] == 'S': # South | |
| lat = -lat | |
| if gps_data['GPSLongitudeRef'] == 'W': # West | |
| lon = -lon | |
| date_create = datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S') | |
| if img_exif['Exif']: | |
| if img_exif['Exif']['DateTimeOriginal']: | |
| date_create = datetime.datetime.strptime(img_exif['Exif']['DateTimeOriginal'], '%Y:%m:%d %H:%M:%S').strftime('%d/%m/%Y %H:%M:%S') | |
| return lat, lon,date_create | |
| return None, None,datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S') | |
| except Exception as e: | |
| print(e) | |
| return None, None,datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S') | |
| def covert_image_tobase64(img): | |
| im_rgb = Image.fromarray(img[..., ::-1]) | |
| im_rgb.thumbnail((640, 640)) | |
| buffered = io.BytesIO() | |
| im_rgb.save(buffered, format="JPEG") | |
| img_str = base64.b64encode(buffered.getvalue()).decode() | |
| return img_str,im_rgb | |
| def get_adress(latitude, longitude): | |
| url = f'https://rsapi.goong.io/geocode?latlng={latitude},{longitude}&api_key=AuV0pJBL6oERS3csazO1SuGkbF9IYHdRg7B6iVGE' | |
| resp = requests.get(url=url) | |
| data = resp.json() # Check the JSON Response Content documentation below | |
| return data['results'][0]['formatted_address'] | |