-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
73 lines (61 loc) · 2.1 KB
/
data_utils.py
File metadata and controls
73 lines (61 loc) · 2.1 KB
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
import os
import cv2
import os
def list_files_in_directory(directory):
"""
Returns a list of all filenames in the specified directory.
Args:
directory (str): The path to the directory.
Returns:
list: A list of filenames in the directory.
"""
try:
filenames = os.listdir(directory)
return filenames
except FileNotFoundError:
print(f"The directory '{directory}' does not exist.")
return []
except PermissionError:
print(f"Permission denied to access the directory '{directory}'.")
return []
except Exception as e:
print(f"An error occurred: {e}")
return []
# Example usage:
# directory_path = '/path/to/directory'
# print(list_files_in_directory(directory_path))
def list_files(directory, extensions):
"""
List all files in a directory with the specified extensions.
Args:
- directory (str): The directory to search in.
- extensions (tuple): The file extensions to look for (e.g., ('.jpg', '.txt')).
Returns:
- list: A list of file paths that match the specified extensions.
"""
files_list = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(extensions):
files_list.append(os.path.join(root, file))
return files_list
def read_image_and_text(directory):
"""
Reads image files and their corresponding text files.
Args:
- directory (str): The directory containing the files.
Returns:
- list: A list of tuples, where each tuple contains an image (as a numpy array)
and its corresponding text content (as a string).
"""
image_text_pairs = []
image_files = list_files(directory, ('.jpg',))
for image_file in image_files:
base_name = os.path.splitext(image_file)[0]
text_file = base_name + '.txt'
if os.path.exists(text_file):
image = cv2.imread(image_file)
with open(text_file, 'r') as file:
text_content = file.read()
image_text_pairs.append((image, text_content))
return image_text_pairs