<jupyter_start><jupyter_text>Intro  Create Image annotation CSV First we Load the images from the folders into a list. Then also create a list with tags belonging to the image. Here 1 for closed hand and 2 for open hand<jupyter_code>import os
import csv #provides functionality to both read from and write to CSV files. Designed to work out of the box with Excel-generated CSV files
import pandas as pd #pandas is a great data handling library

root_dir = "T:/werth/Messe/hände/small/"  # Path relative to current dir (os.getcwd())
folder1 = "Faust"
folder2 = "Offen"

#os.listdir(root_dir + folder)  # List all the items in root_dir
Faust=[item for item in os.listdir(root_dir+ folder1) if os.path.isfile(os.path.join(root_dir+ folder1, item))]  # Filter items and only keep files (strip out directories)
Offen=[item for item in os.listdir(root_dir+ folder2) if os.path.isfile(os.path.join(root_dir+ folder2, item))]  # Filter items and only keep files (strip out directories)

Faust=[root_dir + s for s in Faust] # Adding the root path to the image name to create a full path
Offen=[root_dir + s for s in Offen]

Annot1= [1] * len(Faust) # Same length list with annotation key
Annot2= [2] * len(Offen)<jupyter_output><empty_output><jupyter_text>Then we ceate a data frame with pandas in the way we want the image and tags merged. In this case we need the format: Imagepath,tag<jupyter_code>df1= pd.DataFrame(list(zip(*[Faust,Annot1]))).add_prefix('Col_')
df2= pd.DataFrame(list(zip(*[Offen,Annot2]))).add_prefix('Col_')

frames = [df1,df2]   # Merge list 
F_and_A = pd.concat(frames, sort=False) #Merge into the pandas dataframe

F_and_A.to_csv('Hand_Annotations.csv', index=False) # Write to CSV file

#print(frames)<jupyter_output><empty_output><jupyter_text>Alternaternatively we could also create an dictionary :<jupyter_code>d1 = dict(zip(Faust,Annot1)) # Create dict from data list and annotaions keys
d2 = dict(zip(Offen,Annot2))
d = {**d1, **d2} #Merge both dicts. Attention, if the keys have the same name, they will be overwritten by (here) d2<jupyter_output><empty_output><jupyter_text>But the transformation is not as straight forward as with pandas to_csv  Create ID mapping CSV Now we create another CSV file which maps the tag od ID to a annotation. First create a simple dict to wit the key and values as ID mapping and then transfer that again into CSV<jupyter_code>df3 = {'Faust': [1], 'Offen': [2]} #regular dict with the annotation mapping
df3 = pd.DataFrame.from_dict(data=df3,orient='index') #convert to a pandas dataframe for easy conversion into CSV
df3.to_csv('IDmapping.csv', index=True, header=False) # Write to CSV file (Can also just be added in the line before with: ...).to_csv(...))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09796067006554988, 'fraction_numerical': 0.011289147851420248, 'mean_word_length': 5.418224299065421, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '88753', 'license': 'MIT', 'n_tokens_mistral': 927, 'n_tokens_neox': 888, 'n_words': 345, 'path': '.ipynb_checkpoints/CSV tag creation -checkpoint.ipynb', 'repo_name': 'JanderHungrige/Workshop', 'size': '2746'}