vistopics 0.1.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vistopics-0.1.7/LICENSE +22 -0
- vistopics-0.1.7/MANIFEST.in +2 -0
- vistopics-0.1.7/PKG-INFO +273 -0
- vistopics-0.1.7/README.md +232 -0
- vistopics-0.1.7/pyproject.toml +3 -0
- vistopics-0.1.7/setup.cfg +4 -0
- vistopics-0.1.7/setup.py +36 -0
- vistopics-0.1.7/vistopics/__init__.py +6 -0
- vistopics-0.1.7/vistopics/captioning.py +124 -0
- vistopics-0.1.7/vistopics/extract_frames.py +140 -0
- vistopics-0.1.7/vistopics/image_download.py +129 -0
- vistopics-0.1.7/vistopics/reduce_frame.py +93 -0
- vistopics-0.1.7/vistopics/video_scrape.py +67 -0
- vistopics-0.1.7/vistopics.egg-info/PKG-INFO +273 -0
- vistopics-0.1.7/vistopics.egg-info/SOURCES.txt +16 -0
- vistopics-0.1.7/vistopics.egg-info/dependency_links.txt +1 -0
- vistopics-0.1.7/vistopics.egg-info/requires.txt +15 -0
- vistopics-0.1.7/vistopics.egg-info/top_level.txt +1 -0
vistopics-0.1.7/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ayse Deniz Lokmanoglu and Dror Walter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
vistopics-0.1.7/PKG-INFO
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vistopics
|
|
3
|
+
Version: 0.1.7
|
|
4
|
+
Summary: A package for video and image processing with captioning capabilities
|
|
5
|
+
Home-page: https://github.com/aysedeniz09/VisTopics
|
|
6
|
+
Author: Ayse D. Lokmanoglu & Dror Walter
|
|
7
|
+
Author-email: alokman@bu.edu
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: openai>=1.50.0
|
|
16
|
+
Requires-Dist: opencv-python<4.10,>=4.9.0
|
|
17
|
+
Requires-Dist: opencv-python-headless<4.10,>=4.9.0
|
|
18
|
+
Requires-Dist: pandas<2.2,>=2.0.3
|
|
19
|
+
Requires-Dist: requests>=2.28.0
|
|
20
|
+
Requires-Dist: yt-dlp>=2024.12.6
|
|
21
|
+
Requires-Dist: gradio>=3.36.0
|
|
22
|
+
Requires-Dist: aiofiles>=23.0
|
|
23
|
+
Requires-Dist: pydantic>=2.8
|
|
24
|
+
Requires-Dist: urllib3>=1.26
|
|
25
|
+
Requires-Dist: beautifulsoup4>=4.12
|
|
26
|
+
Provides-Extra: fastdup
|
|
27
|
+
Requires-Dist: fastdup>=2.15; extra == "fastdup"
|
|
28
|
+
Requires-Dist: numpy~=1.23.0; extra == "fastdup"
|
|
29
|
+
Dynamic: author
|
|
30
|
+
Dynamic: author-email
|
|
31
|
+
Dynamic: classifier
|
|
32
|
+
Dynamic: description
|
|
33
|
+
Dynamic: description-content-type
|
|
34
|
+
Dynamic: home-page
|
|
35
|
+
Dynamic: license
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
Dynamic: provides-extra
|
|
38
|
+
Dynamic: requires-dist
|
|
39
|
+
Dynamic: requires-python
|
|
40
|
+
Dynamic: summary
|
|
41
|
+
|
|
42
|
+
# vistopics
|
|
43
|
+
|
|
44
|
+
`vistopics` (Topic Visualization for Visuals) is a Python package for video and image processing, offering features such as:
|
|
45
|
+
|
|
46
|
+
- Frame extraction from videos.
|
|
47
|
+
- Caption generation for images using OpenAI.
|
|
48
|
+
- Video scraping and organization.
|
|
49
|
+
- Duplicate frame reduction with efficient algorithms.
|
|
50
|
+
- Image scraping and captioning from article URLs.
|
|
51
|
+
|
|
52
|
+
This package is designed for developers, researchers, and data scientists working on media processing, visualization, or clustering tasks.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### Installation
|
|
57
|
+
|
|
58
|
+
Install `vistopics` from PyPI using:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install vistopics
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If you plan to use **FastDup for duplicate frame detection**, install with:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install vistopics[fastdup]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
Alternatively, install it directly from the source:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
git clone https://github.com/aysedeniz09/VisTopics
|
|
75
|
+
cd VisTopics
|
|
76
|
+
pip install .
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
*Note: The repository name on GitHub is VisTopics (capitalized), but the package name and Python import name are lowercase vistopics.*
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### Features
|
|
84
|
+
|
|
85
|
+
#### Option A: Video-Based Pipeline
|
|
86
|
+
1. Scrape and download videos from URLs
|
|
87
|
+
2. Extract frames from the downloaded videos
|
|
88
|
+
3. Reduce duplicate frames with FastDup
|
|
89
|
+
4. Generate captions for the cleaned set of frames
|
|
90
|
+
|
|
91
|
+
#### Option B: Image URL-Based Pipeline
|
|
92
|
+
1. Download images from a CSV containing article or image URLs
|
|
93
|
+
2. Generate captions for the images
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### Requirements
|
|
98
|
+
|
|
99
|
+
The following Python libraries are required:
|
|
100
|
+
|
|
101
|
+
- openai>=1.50.0
|
|
102
|
+
- opencv-python>=4.9.0,<4.10
|
|
103
|
+
- opencv-python-headless>=4.9.0,<4.10
|
|
104
|
+
- pandas>=2.0.3,<2.2
|
|
105
|
+
- requests>=2.28.0
|
|
106
|
+
- yt-dlp>=2024.12.6
|
|
107
|
+
- gradio>=3.36.0
|
|
108
|
+
- aiofiles>=23.0
|
|
109
|
+
- pydantic>=2.8
|
|
110
|
+
- urllib3>=1.26
|
|
111
|
+
- beautifulsoup4>=4.12
|
|
112
|
+
- tqdm>=4.66
|
|
113
|
+
|
|
114
|
+
**Note:** To use FastDup-based functionality (`limiting_frames`), you must additionally install:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
pip install vistopics[fastdup]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Install base dependencies with:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pip install -r requirements.txt
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
### Usage
|
|
130
|
+
|
|
131
|
+
#### Option A: Video-Based Pipeline
|
|
132
|
+
|
|
133
|
+
**1. Video Scraping**
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from vistopics import video_download
|
|
137
|
+
|
|
138
|
+
video_download(
|
|
139
|
+
input_df_path="test_data.csv",
|
|
140
|
+
output_df_path="cleaned_videos.csv",
|
|
141
|
+
output_dir="downloaded_videos",
|
|
142
|
+
link_column="Link",
|
|
143
|
+
title_column="Page Name"
|
|
144
|
+
)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**2. Frame Extraction**
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from vistopics import extract_frames
|
|
151
|
+
|
|
152
|
+
extract_frames(
|
|
153
|
+
videofolder="downloaded_videos",
|
|
154
|
+
images_folder="images",
|
|
155
|
+
frame_rate=1
|
|
156
|
+
)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**3. Duplicate Frame Reduction**
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from vistopics import limiting_frames
|
|
163
|
+
|
|
164
|
+
limiting_frames(
|
|
165
|
+
path="images",
|
|
166
|
+
output_file="reduced_frame_list.csv",
|
|
167
|
+
ccthreshold=0.8
|
|
168
|
+
)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This step requires the optional `fastdup` dependency:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
pip install vistopics[fastdup]
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**4. Caption Generation**
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from vistopics import get_caption
|
|
181
|
+
|
|
182
|
+
get_caption(
|
|
183
|
+
mykey="your-open-ai-api-key",
|
|
184
|
+
path_in="images",
|
|
185
|
+
captions_file="captions_file.csv",
|
|
186
|
+
model="gpt-4o-mini"
|
|
187
|
+
)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
#### Option B: Image URL-Based Pipeline
|
|
193
|
+
|
|
194
|
+
**1. Download Images from URLs**
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
from vistopics import download_images_from_url
|
|
198
|
+
|
|
199
|
+
download_images_from_url(
|
|
200
|
+
input_csv="urls.csv", # must have a 'url' column
|
|
201
|
+
output_csv="captions.csv",
|
|
202
|
+
image_dir="images"
|
|
203
|
+
)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**2. Caption Generation**
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
from vistopics import get_caption
|
|
210
|
+
|
|
211
|
+
get_caption(
|
|
212
|
+
mykey="your-open-ai-api-key",
|
|
213
|
+
path_in="images",
|
|
214
|
+
captions_file="captions_file.csv",
|
|
215
|
+
model="gpt-4o-mini"
|
|
216
|
+
)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
### License
|
|
222
|
+
|
|
223
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
### Contributing
|
|
228
|
+
|
|
229
|
+
We welcome contributions! If you'd like to contribute:
|
|
230
|
+
|
|
231
|
+
1. Fork the repository
|
|
232
|
+
2. Create a feature branch:
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
git checkout -b feature-name
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
3. Commit your changes:
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
git commit -m "Add new feature"
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
4. Push to the branch:
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
git push origin feature-name
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
5. Open a pull request
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
For a full research example using `vistopics`, see:
|
|
255
|
+
Lokmanoglu, A. D., & Walter, D. (2025, accepted). *Topic Modeling of Video and Image Data: A Visual Semantic Unsupervised Approach*. Communication Methods and Measures. [Replication Code on GitHub](https://github.com/aysedeniz09/VisTopics/tree/main/paper)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
### Contact
|
|
261
|
+
|
|
262
|
+
If you have any questions or feedback, feel free to contact:
|
|
263
|
+
|
|
264
|
+
**Ayse Lokmanoglu** & **Dror Walter**
|
|
265
|
+
GitHub: https://github.com/aysedeniz09/VisTopics
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
### Acknowledgments
|
|
270
|
+
|
|
271
|
+
- OpenAI for providing APIs used in the captioning feature, and for Researcher Access Program
|
|
272
|
+
- FastDup for efficient duplicate detection
|
|
273
|
+
- OpenCV for video and image processing utilities
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# vistopics
|
|
2
|
+
|
|
3
|
+
`vistopics` (Topic Visualization for Visuals) is a Python package for video and image processing, offering features such as:
|
|
4
|
+
|
|
5
|
+
- Frame extraction from videos.
|
|
6
|
+
- Caption generation for images using OpenAI.
|
|
7
|
+
- Video scraping and organization.
|
|
8
|
+
- Duplicate frame reduction with efficient algorithms.
|
|
9
|
+
- Image scraping and captioning from article URLs.
|
|
10
|
+
|
|
11
|
+
This package is designed for developers, researchers, and data scientists working on media processing, visualization, or clustering tasks.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
### Installation
|
|
16
|
+
|
|
17
|
+
Install `vistopics` from PyPI using:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install vistopics
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
If you plan to use **FastDup for duplicate frame detection**, install with:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install vistopics[fastdup]
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
Alternatively, install it directly from the source:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
git clone https://github.com/aysedeniz09/VisTopics
|
|
34
|
+
cd VisTopics
|
|
35
|
+
pip install .
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
*Note: The repository name on GitHub is VisTopics (capitalized), but the package name and Python import name are lowercase vistopics.*
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
### Features
|
|
43
|
+
|
|
44
|
+
#### Option A: Video-Based Pipeline
|
|
45
|
+
1. Scrape and download videos from URLs
|
|
46
|
+
2. Extract frames from the downloaded videos
|
|
47
|
+
3. Reduce duplicate frames with FastDup
|
|
48
|
+
4. Generate captions for the cleaned set of frames
|
|
49
|
+
|
|
50
|
+
#### Option B: Image URL-Based Pipeline
|
|
51
|
+
1. Download images from a CSV containing article or image URLs
|
|
52
|
+
2. Generate captions for the images
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### Requirements
|
|
57
|
+
|
|
58
|
+
The following Python libraries are required:
|
|
59
|
+
|
|
60
|
+
- openai>=1.50.0
|
|
61
|
+
- opencv-python>=4.9.0,<4.10
|
|
62
|
+
- opencv-python-headless>=4.9.0,<4.10
|
|
63
|
+
- pandas>=2.0.3,<2.2
|
|
64
|
+
- requests>=2.28.0
|
|
65
|
+
- yt-dlp>=2024.12.6
|
|
66
|
+
- gradio>=3.36.0
|
|
67
|
+
- aiofiles>=23.0
|
|
68
|
+
- pydantic>=2.8
|
|
69
|
+
- urllib3>=1.26
|
|
70
|
+
- beautifulsoup4>=4.12
|
|
71
|
+
- tqdm>=4.66
|
|
72
|
+
|
|
73
|
+
**Note:** To use FastDup-based functionality (`limiting_frames`), you must additionally install:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install vistopics[fastdup]
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Install base dependencies with:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
pip install -r requirements.txt
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
### Usage
|
|
89
|
+
|
|
90
|
+
#### Option A: Video-Based Pipeline
|
|
91
|
+
|
|
92
|
+
**1. Video Scraping**
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from vistopics import video_download
|
|
96
|
+
|
|
97
|
+
video_download(
|
|
98
|
+
input_df_path="test_data.csv",
|
|
99
|
+
output_df_path="cleaned_videos.csv",
|
|
100
|
+
output_dir="downloaded_videos",
|
|
101
|
+
link_column="Link",
|
|
102
|
+
title_column="Page Name"
|
|
103
|
+
)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**2. Frame Extraction**
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from vistopics import extract_frames
|
|
110
|
+
|
|
111
|
+
extract_frames(
|
|
112
|
+
videofolder="downloaded_videos",
|
|
113
|
+
images_folder="images",
|
|
114
|
+
frame_rate=1
|
|
115
|
+
)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**3. Duplicate Frame Reduction**
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from vistopics import limiting_frames
|
|
122
|
+
|
|
123
|
+
limiting_frames(
|
|
124
|
+
path="images",
|
|
125
|
+
output_file="reduced_frame_list.csv",
|
|
126
|
+
ccthreshold=0.8
|
|
127
|
+
)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
This step requires the optional `fastdup` dependency:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pip install vistopics[fastdup]
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**4. Caption Generation**
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from vistopics import get_caption
|
|
140
|
+
|
|
141
|
+
get_caption(
|
|
142
|
+
mykey="your-open-ai-api-key",
|
|
143
|
+
path_in="images",
|
|
144
|
+
captions_file="captions_file.csv",
|
|
145
|
+
model="gpt-4o-mini"
|
|
146
|
+
)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
#### Option B: Image URL-Based Pipeline
|
|
152
|
+
|
|
153
|
+
**1. Download Images from URLs**
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from vistopics import download_images_from_url
|
|
157
|
+
|
|
158
|
+
download_images_from_url(
|
|
159
|
+
input_csv="urls.csv", # must have a 'url' column
|
|
160
|
+
output_csv="captions.csv",
|
|
161
|
+
image_dir="images"
|
|
162
|
+
)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**2. Caption Generation**
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
from vistopics import get_caption
|
|
169
|
+
|
|
170
|
+
get_caption(
|
|
171
|
+
mykey="your-open-ai-api-key",
|
|
172
|
+
path_in="images",
|
|
173
|
+
captions_file="captions_file.csv",
|
|
174
|
+
model="gpt-4o-mini"
|
|
175
|
+
)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
### License
|
|
181
|
+
|
|
182
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
### Contributing
|
|
187
|
+
|
|
188
|
+
We welcome contributions! If you'd like to contribute:
|
|
189
|
+
|
|
190
|
+
1. Fork the repository
|
|
191
|
+
2. Create a feature branch:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
git checkout -b feature-name
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
3. Commit your changes:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
git commit -m "Add new feature"
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
4. Push to the branch:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
git push origin feature-name
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
5. Open a pull request
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
For a full research example using `vistopics`, see:
|
|
214
|
+
Lokmanoglu, A. D., & Walter, D. (2025, accepted). *Topic Modeling of Video and Image Data: A Visual Semantic Unsupervised Approach*. Communication Methods and Measures. [Replication Code on GitHub](https://github.com/aysedeniz09/VisTopics/tree/main/paper)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
### Contact
|
|
220
|
+
|
|
221
|
+
If you have any questions or feedback, feel free to contact:
|
|
222
|
+
|
|
223
|
+
**Ayse Lokmanoglu** & **Dror Walter**
|
|
224
|
+
GitHub: https://github.com/aysedeniz09/VisTopics
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
### Acknowledgments
|
|
229
|
+
|
|
230
|
+
- OpenAI for providing APIs used in the captioning feature, and for Researcher Access Program
|
|
231
|
+
- FastDup for efficient duplicate detection
|
|
232
|
+
- OpenCV for video and image processing utilities
|
vistopics-0.1.7/setup.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="vistopics",
|
|
5
|
+
version="0.1.7",
|
|
6
|
+
description="A package for video and image processing with captioning capabilities",
|
|
7
|
+
long_description=open("README.md").read(),
|
|
8
|
+
long_description_content_type="text/markdown",
|
|
9
|
+
author="Ayse D. Lokmanoglu & Dror Walter",
|
|
10
|
+
author_email="alokman@bu.edu",
|
|
11
|
+
url="https://github.com/aysedeniz09/VisTopics",
|
|
12
|
+
license="MIT",
|
|
13
|
+
packages=find_packages(),
|
|
14
|
+
install_requires=[
|
|
15
|
+
"openai>=1.50.0",
|
|
16
|
+
"opencv-python>=4.9.0,<4.10",
|
|
17
|
+
"opencv-python-headless>=4.9.0,<4.10",
|
|
18
|
+
"pandas>=2.0.3,<2.2",
|
|
19
|
+
"requests>=2.28.0",
|
|
20
|
+
"yt-dlp>=2024.12.6",
|
|
21
|
+
"gradio>=3.36.0",
|
|
22
|
+
"aiofiles>=23.0",
|
|
23
|
+
"pydantic>=2.8",
|
|
24
|
+
"urllib3>=1.26",
|
|
25
|
+
"beautifulsoup4>=4.12"
|
|
26
|
+
],
|
|
27
|
+
extras_require={
|
|
28
|
+
"fastdup": ["fastdup>=2.15", "numpy~=1.23.0"]
|
|
29
|
+
},
|
|
30
|
+
classifiers=[
|
|
31
|
+
"Programming Language :: Python :: 3",
|
|
32
|
+
"License :: OSI Approved :: MIT License",
|
|
33
|
+
"Operating System :: OS Independent",
|
|
34
|
+
],
|
|
35
|
+
python_requires=">=3.9",
|
|
36
|
+
)
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
def encode_image(image_path):
|
|
2
|
+
import base64
|
|
3
|
+
with open(image_path, "rb") as image_file:
|
|
4
|
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_caption_main_func(base64_image, api_key, model):
|
|
8
|
+
"""
|
|
9
|
+
Generates a caption for a single image using the specified model and API key.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
base64_image (str): Base64-encoded string of the image.
|
|
13
|
+
api_key (str): API key for OpenAI.
|
|
14
|
+
model (str): Model to use for caption generation (e.g., 'gpt-4', 'gpt-3.5-turbo').
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
str: Generated caption or an error message.
|
|
18
|
+
"""
|
|
19
|
+
import requests
|
|
20
|
+
|
|
21
|
+
custom_prompt = (
|
|
22
|
+
"Directly describe with brevity and as brief as possible the scene or characters without any introductory "
|
|
23
|
+
"phrase like 'This image shows', 'In the scene', 'This image depicts' or similar phrases. If there is a text in "
|
|
24
|
+
"the image mention there is a text but do not caption the text, just start describing the scene please. If you "
|
|
25
|
+
"recognize historical figures and current celebrities and politicians in the picture give their full name, but "
|
|
26
|
+
"don't give the whole background about who they are"
|
|
27
|
+
)
|
|
28
|
+
headers = {
|
|
29
|
+
"Content-Type": "application/json",
|
|
30
|
+
"Authorization": f"Bearer {api_key}"
|
|
31
|
+
}
|
|
32
|
+
payload = {
|
|
33
|
+
"model": model, # Dynamic model parameter
|
|
34
|
+
"messages": [
|
|
35
|
+
{
|
|
36
|
+
"role": "user",
|
|
37
|
+
"content": [
|
|
38
|
+
{"type": "text", "text": custom_prompt},
|
|
39
|
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"max_tokens": 300
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try:
|
|
47
|
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
|
48
|
+
response.raise_for_status()
|
|
49
|
+
response_json = response.json()
|
|
50
|
+
|
|
51
|
+
if 'choices' in response_json and response_json['choices'] and 'message' in response_json['choices'][0]:
|
|
52
|
+
caption = response_json['choices'][0]['message'].get('content', 'Caption not found').strip()
|
|
53
|
+
return caption
|
|
54
|
+
except requests.RequestException as e:
|
|
55
|
+
print(f"API request failed: {e}")
|
|
56
|
+
return "Failed to get caption"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def list_files(folder):
|
|
60
|
+
"""
|
|
61
|
+
Lists all files in the specified folder and subdirectories.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
folder (str): Path to the folder.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
list: List of file paths.
|
|
68
|
+
"""
|
|
69
|
+
import os
|
|
70
|
+
file_list = []
|
|
71
|
+
for root, dirs, files in os.walk(folder):
|
|
72
|
+
for file in files:
|
|
73
|
+
file_list.append(os.path.join(root, file))
|
|
74
|
+
return file_list
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def get_caption(mykey, path_in, captions_file, model):
|
|
78
|
+
"""
|
|
79
|
+
Generates captions for images and saves them to a CSV file.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
mykey (str): API key for the captioning service.
|
|
83
|
+
path_in (str): Directory containing images to process.
|
|
84
|
+
captions_file (str): Path to the CSV file to save captions.
|
|
85
|
+
model (str): Model to use for caption generation (e.g., 'gpt-4', 'gpt-3.5-turbo').
|
|
86
|
+
"""
|
|
87
|
+
import csv
|
|
88
|
+
import os
|
|
89
|
+
import pandas as pd
|
|
90
|
+
import random
|
|
91
|
+
import time
|
|
92
|
+
|
|
93
|
+
# List all images in the input directory
|
|
94
|
+
all_images = list_files(path_in)
|
|
95
|
+
|
|
96
|
+
# Load existing captions if the file exists
|
|
97
|
+
try:
|
|
98
|
+
df = pd.read_csv(captions_file)
|
|
99
|
+
existing_images = df['image_path'].tolist()
|
|
100
|
+
except FileNotFoundError:
|
|
101
|
+
existing_images = []
|
|
102
|
+
|
|
103
|
+
# Filter out images that already have captions
|
|
104
|
+
new_images = [img for img in all_images if img not in existing_images]
|
|
105
|
+
|
|
106
|
+
# Open the captions file for appending
|
|
107
|
+
with open(captions_file, mode='a', newline='') as file:
|
|
108
|
+
writer = csv.writer(file)
|
|
109
|
+
|
|
110
|
+
# Write header if the file is new
|
|
111
|
+
if not existing_images:
|
|
112
|
+
writer.writerow(['image_path', 'caption'])
|
|
113
|
+
|
|
114
|
+
# Process each new image
|
|
115
|
+
for i, img_path in enumerate(new_images):
|
|
116
|
+
print(f"Processing image: {img_path}")
|
|
117
|
+
time.sleep(random.uniform(1, 5)) # Random delay to avoid API rate limits
|
|
118
|
+
base64_image = encode_image(img_path)
|
|
119
|
+
caption = get_caption_main_func(base64_image, mykey, model)
|
|
120
|
+
|
|
121
|
+
# Write the current row to the CSV file
|
|
122
|
+
writer.writerow([img_path, caption])
|
|
123
|
+
file.flush() # Ensure data is written to disk
|
|
124
|
+
print(f"Caption done for image {i + 1}/{len(new_images)}")
|