File size: 1,650 Bytes
c225a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import csv
import io


class FL_CaptionToCSV:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "image_directory": ("STRING", {"default": ""}),
            },
        }

    RETURN_TYPES = ("CSV",)
    FUNCTION = "create_csv"
    CATEGORY = "🏵️Fill Nodes/Captioning"
    OUTPUT_NODE = True

    def create_csv(self, image_directory):
        # Get all image files and their corresponding caption files
        image_files = [f for f in os.listdir(image_directory) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
        image_files.sort()  # Sort files to ensure consistent order

        # Prepare CSV data
        csv_data = []
        for image_file in image_files:
            caption_file = os.path.splitext(image_file)[0] + '.txt'
            caption_path = os.path.join(image_directory, caption_file)
            try:
                with open(caption_path, 'r', encoding='utf-8') as f:
                    caption = f.read().strip()
            except FileNotFoundError:
                caption = "No caption found"

            csv_data.append([image_file, caption])

        # Create CSV in memory
        output = io.StringIO()
        writer = csv.writer(output)
        writer.writerow(['image_file', 'caption'])  # Header
        writer.writerows(csv_data)

        # Get the CSV content as a string
        csv_content = output.getvalue()

        # Convert to bytes for compatibility with other nodes
        csv_bytes = csv_content.encode('utf-8')

        return (csv_bytes,)

    @classmethod
    def IS_CHANGED(cls, image_directory):
        return float("NaN")