-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_unified_database.py
More file actions
187 lines (144 loc) · 5.65 KB
/
Copy pathcreate_unified_database.py
File metadata and controls
187 lines (144 loc) · 5.65 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
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
#!/usr/bin/env python3
"""
Script to create a unified database for both product images and videos.
"""
import csv
import json
import re
from pathlib import Path
from urllib.parse import quote
def clean_image_url(url):
"""
Clean malformed Amazon image URLs.
Removes the /W/IMAGERENDERING_XXXXXX-TX/images pattern from URLs.
Example:
https://m.media-amazon.com/images/W/IMAGERENDERING_521856-T2/images/I/71cv73eEBWL._AC_UL320_.jpg
-> https://m.media-amazon.com/images/I/71cv73eEBWL._AC_UL320_.jpg
Args:
url: Original image URL
Returns:
Cleaned URL
"""
if not url:
return url
# Pattern to match: /W/IMAGERENDERING_XXXXXX-TX/images
pattern = r'/W/IMAGERENDERING_[^/]+-[^/]+/images'
cleaned_url = re.sub(pattern, '', url)
return cleaned_url
def process_product_images(csv_dir):
"""
Process product image CSV files and convert to unified format.
Returns:
List of image entries in unified format
"""
images = []
csv_dir_path = Path(csv_dir)
csv_files = sorted(csv_dir_path.glob('*.csv'))
print(f"Processing {len(csv_files)} product image CSV files...")
for csv_file in csv_files:
print(f" Processing: {csv_file.name}")
try:
with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
name = row.get('name', '').strip()
main_category = row.get('main_category', '').strip()
sub_category = row.get('sub_category', '').strip()
image_url = row.get('image', '').strip()
if name and image_url: # Only include if we have name and image
# Clean the image URL to fix malformed Amazon URLs
cleaned_url = clean_image_url(image_url)
entry = {
'content_type': 'image',
'title': name,
'description': name,
'link': cleaned_url,
'meta': {
'category': main_category,
'sub_category': sub_category
}
}
images.append(entry)
except Exception as e:
print(f" Error processing {csv_file.name}: {e}")
continue
return images
def process_videos(metadata_file, base_url):
"""
Process video metadata and convert to unified format.
Args:
metadata_file: Path to video metadata JSON file
base_url: Base URL for video files (e.g., http://194.238.23.194/epicsum/)
Returns:
List of video entries in unified format
"""
videos = []
print(f"\nProcessing video metadata...")
try:
with open(metadata_file, 'r', encoding='utf-8') as f:
metadata = json.load(f)
for item in metadata:
text = item.get('text', '').strip()
handwritten_description = item.get('handwritten_description', '').strip()
file_name = item.get('file_name', '').strip()
if file_name and handwritten_description:
# Extract just the filename from the path (e.g., "videos/file.mp4" -> "file.mp4")
video_filename = file_name.split('/')[-1]
# URL encode the filename for the link
encoded_filename = quote(video_filename)
video_link = f"{base_url}{encoded_filename}"
entry = {
'content_type': 'video',
'title': text if text else handwritten_description,
'description': handwritten_description,
'link': video_link,
'meta': {}
}
videos.append(entry)
except Exception as e:
print(f" Error processing video metadata: {e}")
return videos
def create_unified_database(
csv_dir,
video_metadata_file,
video_base_url,
output_file
):
"""
Create unified database combining images and videos.
"""
print("=" * 60)
print("Creating Unified Media Database")
print("=" * 60)
# Process images
images = process_product_images(csv_dir)
print(f"✓ Processed {len(images)} product images")
# Process videos
videos = process_videos(video_metadata_file, video_base_url)
print(f"✓ Processed {len(videos)} videos")
# Combine all media
all_media = images + videos
print(f"\n{'=' * 60}")
print(f"Total media items: {len(all_media)}")
print(f" - Images: {len(images)}")
print(f" - Videos: {len(videos)}")
print(f"{'=' * 60}")
# Write to JSON file
print(f"\nWriting to {output_file}...")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(all_media, f, indent=2, ensure_ascii=False)
print(f"✓ Successfully created {output_file}")
return len(images), len(videos)
if __name__ == "__main__":
# Configuration
CSV_DIRECTORY = "product-images-dataset"
VIDEO_METADATA_FILE = "video-dataset/metadata.json"
VIDEO_BASE_URL = "http://194.238.23.194/epicsum/videos/"
OUTPUT_FILE = "unified_media_database.json"
# Create unified database
create_unified_database(
CSV_DIRECTORY,
VIDEO_METADATA_FILE,
VIDEO_BASE_URL,
OUTPUT_FILE
)