File size: 9,077 Bytes
d01f802
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454f6b6
d01f802
 
 
 
ed96d64
d01f802
 
 
ed96d64
d01f802
 
c8161d6
d01f802
 
 
3db78b4
856113e
 
 
91bda11
d01f802
 
3db78b4
91bda11
 
 
 
d01f802
 
 
 
 
 
 
 
 
 
 
 
 
72431ed
 
d01f802
 
72431ed
d01f802
 
 
 
 
72431ed
d01f802
 
 
 
 
72431ed
d01f802
 
 
72431ed
d01f802
 
 
 
 
 
 
72431ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01f802
72431ed
 
 
 
 
d01f802
 
 
 
 
72431ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01f802
 
 
 
 
 
 
91bda11
 
 
 
 
 
d01f802
 
 
 
 
 
ed96d64
 
91bda11
 
 
 
 
 
d01f802
 
 
 
 
 
 
ed96d64
 
72431ed
91bda11
 
 
 
 
72431ed
 
 
 
 
 
 
 
 
 
91bda11
 
d01f802
 
 
 
 
91bda11
 
 
 
 
 
 
 
 
d01f802
91bda11
 
 
d01f802
 
91bda11
 
d01f802
91bda11
 
 
d01f802
 
 
 
 
 
 
 
72431ed
d01f802
 
 
 
 
 
72431ed
d01f802
 
91bda11
d01f802
 
 
 
91bda11
ed96d64
d01f802
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
TOC:
0) IMPORTS 
1) METADATA
2) UPLOAD
3) ANNOTATIONS
-1) MAIN
"""

# gradio run.py --demo-name=my_demo

##################################################
# 0) IMPORTS
##################################################

# baselayer
import os
from io import BytesIO
import argparse

# web
import gradio as gr

# image processing
from tkinter import Tk, filedialog
from pathlib import Path
from PIL import Image, ExifTags
from PIL.ExifTags import TAGS

# data science
import numpy as np
import pandas as pd

# export
import csv


# from transformers import AutoImageProcessor, AutoModelForImageClassification
# import torch
# Load model
# processor = AutoImageProcessor.from_pretrained("victor/animals-classifier")
# model = AutoModelForImageClassification.from_pretrained("victor/animals-classifier")
# model.eval()

##################################################
# 1) METADATA
##################################################


# this one works with PIL but we don't get all the metadata
def decode_utf16_little_endian(binary_data):
    try:
        # Decode the binary data as UTF-16 Little Endian
        # print(f"Test:{binary_data.decode('utf-16-le')}")
        # print(f"Type:{type(binary_data)}")
        decoded_text = binary_data.decode("utf-16-le").rstrip("\x00")
    except Exception as e:
        decoded_text = "Encoded"
    return decoded_text


'''
def get_exif(list_file_paths):
    metadata_all_file = {}
    df = pd.DataFrame()
    for file_path in list_file_paths:
        metadata = {}
        metadata["name"] = file_path.split("/")[-1]
        print(file_path)
        try:
            image = Image.open(file_path)
            exifdata = image._getexif()
            if exifdata is not None:
                print(len(exifdata.items()))
                for tagid, value in exifdata.items():
                    # print(tagid, value)
                    # print(f"Value:{value}")
                    tagname = str(TAGS.get(tagid, tagid))
                    # value = exifdata.get(tagid)
                    # Handle binary data
                    if isinstance(value, bytes):
                        # print(f"Value bytes {value}")
                        # print(f"Value bytes {type(value)}")
                        # print(f"Value str {decode_utf16_little_endian(value)}")
                        value = decode_utf16_little_endian(value)
                    print(tagname)
                    print(type(tagname))
                    print(value)
                    if type(tagname) is not str:
                        print(">>>>>>>>>>>> here " + type(tagname))
                        try:
                            metadata[str(tagname)] = value
                        except:
                            try:
                                metadata[repr(tagname)] = value
                            except:
                                pass
                    else:
                        metadata[tagname] = value
                    """
                    for key in metadata.keys():
                        if type(key) is not str:
                            try:
                                metadata[str(key)] = metadata[key]
                            except:
                                try:
                                    metadata[repr(key)] = metadata[key]
                                except:
                                    pass
                            del metadata[key]
                    """
                    # print(f"\t{metadata}")
                print(metadata)
                print(pd.DataFrame([metadata]))
                df = pd.concat([df, pd.DataFrame([metadata])], ignore_index=True)
                # new_row = {"name": file_path, **metadata}
                # df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
                # metadata_all_file[file_path] = metadata
            else:
                return "No EXIF metadata found."
        except Exception as e:
            return f"Error : {e}"
        print(pd.concat([df, pd.DataFrame([metadata])], ignore_index=True))
    print(f"FINAL DF \n \n \n {df}")
    return df
'''
import pandas as pd
from PIL import Image
from PIL.ExifTags import TAGS


def decode_utf16_little_endian(value):
    try:
        return value.decode("utf-16le").strip()
    except:
        return value  # Fallback to the original value if decoding fails


def extract_particular_value_from_exif_file(metadata, tagname, value):
    pass


def get_exif(list_file_paths):
    df = pd.DataFrame()

    for file_path in list_file_paths:
        metadata = {"name": file_path.split("/")[-1]}
        print(file_path)

        try:
            image = Image.open(file_path)
            exifdata = image._getexif()

            if exifdata is not None:
                for tagid, value in exifdata.items():
                    tagname = TAGS.get(tagid, str(tagid))  # Ensure tagname is a string
                    print(type(tagname))
                    if isinstance(value, bytes):
                        value = decode_utf16_little_endian(value)
                    if isinstance(value, dict):
                        # for subkey, subvalue in value.items():
                        #     metadata[f"{tagname}_{subkey}"] = subvalue
                        # else:
                        #     metadata[tagname] = value
                        value = str(value)
                    print(value)
                    print(type(value))
                    metadata[tagname] = value  # All keys are now strings
                    print(metadata)
                if all(isinstance(k, str) for k in metadata.keys()):
                    df = pd.concat([df, pd.DataFrame([metadata])], ignore_index=True)
                else:
                    print("Skipping metadata with non-string keys.")
            else:
                print(f"No EXIF metadata found for {file_path}")

        except Exception as e:
            print(f"Error processing {file_path}: {e}")

    print(f"FINAL DF:\n{df}")
    return df


##################################################
# 2) UPLOAD
##################################################


def get_file_names(files_):
    """
    Get a list of the name of files splitted to get only the proper name
    Input: Uploaded files
    Output: ['name of file 1', 'name of file 2']"""
    return [file.name for file in files_]


##################################################
# 3) ANNOTATIONS
##################################################


def get_annotation(files_):
    """
    Get the label and accuracy from pretrained (or futur custom model)
    Input: Uploaded files
    Output: Df that contains: file_name | label | accuracy
    """
    # df = pd.DataFrame(columns=["file_name", "label", "accuracy"])
    df_exif = get_exif(get_file_names(files_))
    return df_exif


def update_dataframe(df):
    return df  # Simply return the modified dataframe


def df_to_csv(df_, encodings=None):
    """
    Get the df and convert it as an gradio file output ready for download
    Input: DF created
    Output: gr.File()
    """
    if encodings is None:
        encodings = ["utf-8", "utf-8-sig", "latin1", "iso-8859-1", "cp1252"]

    for encoding in encodings:
        try:
            df_.to_csv("output.csv", encoding=encoding, index=False)
            # print(f"File saved successfully with encoding: {encoding}")
            return gr.File(value="output.csv", visible=True)
        except Exception as e:
            print(f"Failed with encoding {encoding}: {e}")


##################################################
# -1) MAIN
##################################################


def process_files(files_):
    """
    Main function
    - Get uploaded files
    - Get annotations # TODO
    - Get the corresponding df
    - Get the csv output
    """
    df = get_annotation(files_)
    return df


with gr.Blocks() as interface:
    gr.Markdown("# Wildlife.ai Annotation tools")
    # Upload data
    with gr.Row():
        upload_btn = gr.UploadButton(
            "Upload raw data",
            file_types=["image", "video"],
            file_count="multiple",
        )
        update_btn = gr.Button("Modify raw data")
        download_raw_btn = gr.Button("Generate raw data as csv")
        download_modified_btn = gr.Button("Generate new data as a csv")
        # Get results
    gr.Markdown("## Results")
    df = gr.DataFrame(interactive=False)
    download_raw_btn.click(
        fn=df_to_csv,
        inputs=[df],
        outputs=gr.File(visible=False),
    )
    gr.Markdown("## Modified results")
    df_modified = gr.DataFrame(interactive=True)
    download_modified_btn.click(
        fn=df_to_csv,
        inputs=[df_modified],
        outputs=gr.File(visible=False),
        show_progress=False,
    )
    # gr.Markdown("## Extract as CSV")
    # Buttons
    upload_btn.upload(fn=process_files, inputs=upload_btn, outputs=df)
    update_btn.click(fn=update_dataframe, inputs=df, outputs=df_modified)


if __name__ == "__main__":
    interface.launch(debug=True)