Skip to content

Commit c2f6d66

Browse files
committed
improve VideoToImage export
Export less images for faster translation
1 parent fad0b15 commit c2f6d66

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

README.md

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,9 @@ This project utilizes optical character recognition (OCR) and translation to tra
1414
2. Run the script `main.py`.
1515
3. Translated images will be saved in the `output` folder.
1616

17-
## New Features
18-
19-
### SyncVideoWithAudio.py
20-
This script syncs audio to a video file using advanced checks and features. It performs the following steps:
21-
- **Duration Check:** Ensures that the video and audio durations are within a specified tolerance.
22-
- **Audio Extraction:** Extracts audio from the video if available, or generates silent audio if not.
23-
- **Audio Alignment:** Aligns the audio using cross-correlation to calculate the offset.
24-
- **Synchronization:** Syncs the audio to the video using FFmpeg and saves the output.
25-
26-
### TranslateMultipleImage.py
27-
This script processes multiple images by performing OCR to extract text, translating the text, and replacing the original text in the images with the translated text. It includes:
28-
- **Batch Processing:** Allows processing images one by one or multiple images simultaneously using multithreading.
29-
- **Error Handling:** Handles translation errors and missing translations gracefully.
30-
- **Customization:** Supports custom source and target languages for OCR and translation.
31-
32-
### videoToImage.py
33-
This script extracts frames from a video file and saves them as individual images. It features:
34-
- **Frame Extraction:** Reads and saves each frame of the video as a separate image.
35-
- **Output Management:** Ensures the output folder exists and manages the file naming for the frames.
36-
37-
### imageToVideo.py
38-
This script converts a series of images into a video file. It includes:
39-
- **Image-to-Video Conversion:** Reads images from a folder and combines them into a video file.
40-
- **Frame Rate Customization:** Allows setting the frame rate for the output video.
41-
- **Error Handling:** Uses a placeholder frame for any invalid or unreadable images.
42-
43-
## The goal of this update is to be able to translate video to video with the combination of [OpenTranslator](https://github.com/overcrash66/OpenTranslator).
17+
## The goal of this update / tools, is to be able to translate from a video to video with the combination of [OpenTranslator](https://github.com/overcrash66/OpenTranslator).
18+
19+
[![Demo - Translation Example](https://img.youtube.com/vi/ebviBPenkfI/0.jpg)](https://www.youtube.com/watch?v=ebviBPenkfI)
4420

4521
# Setup
4622

@@ -69,13 +45,15 @@ pip install torch==2.5.1+cu118 torchaudio==2.5.1+cu118 torchvision==0.20.1+cu118
6945

7046
## Notes
7147

72-
- Supported languages for OCR can be seen [here](https://www.jaided.ai/easyocr/)
73-
- Supported languages for Google Translate can be obtained using the following code:
48+
- Supported languages for OCR can be seen [here](https://www.jaided.ai/easyocr/)
49+
- Supported languages for Google Translate can be obtained using the following code:
50+
7451
```python
7552
from deep_translator.constants import GOOGLE_LANGUAGES_TO_CODES
7653
print(GOOGLE_LANGUAGES_TO_CODES)
7754
```
78-
- Adjustments to text languages, recognition thresholds, translation services, or image processing parameters can be made within the script.
55+
56+
- Adjustments to text languages, recognition thresholds, translation services, or image processing parameters can be made within the script.
7957

8058
## Examples
8159

@@ -84,6 +62,6 @@ pip install torch==2.5.1+cu118 torchaudio==2.5.1+cu118 torchvision==0.20.1+cu118
8462

8563
## Acknowledgments
8664

87-
- [EasyOCR](https://github.com/JaidedAI/EasyOCR) - For OCR processing.
88-
- [Google Translator](https://pypi.org/project/deep-translator/) - For text translation.
89-
- [Pillow (PIL Fork)](https://python-pillow.org/) - For image manipulation.
65+
- [EasyOCR](https://github.com/JaidedAI/EasyOCR) - For OCR processing.
66+
- [Google Translator](https://pypi.org/project/deep-translator/) - For text translation.
67+
- [Pillow (PIL Fork)](https://python-pillow.org/) - For image manipulation.

videoToImage.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'''
22
This script convert an input video to frames / a list of images
33
'''
4-
54
import os
65
import cv2
6+
import numpy as np
77

88
def video_to_images(video_path, output_folder):
99
# Check if the video file exists
@@ -24,6 +24,8 @@ def video_to_images(video_path, output_folder):
2424
return
2525

2626
frame_count = 0
27+
saved_count = 0
28+
prev_frame = None # To store the previous frame for comparison
2729

2830
while True:
2931
# Read a frame from the video
@@ -33,25 +35,33 @@ def video_to_images(video_path, output_folder):
3335
if not ret:
3436
break
3537

38+
# Check if the current frame is identical to the previous frame
39+
if prev_frame is not None and np.array_equal(prev_frame, frame):
40+
frame_count += 1
41+
continue # Skip saving this frame
42+
3643
# Generate the file name for the frame image
3744
frame_filename = os.path.join(output_folder, f"frame_{frame_count:04d}.png")
3845

3946
# Save the frame as an image
4047
cv2.imwrite(frame_filename, frame)
48+
saved_count += 1
4149

50+
# Update the previous frame
51+
prev_frame = frame
4252
frame_count += 1
4353

4454
# Release the video capture object
4555
video.release()
4656

47-
print(f"Exported {frame_count} frames to '{output_folder}'.")
57+
print(f"Exported {saved_count} unique frames to '{output_folder}'.")
4858

4959
if __name__ == "__main__":
5060
# Input MP4 video file
51-
video_path = input("Enter the path to the MP4 video file: ")
52-
61+
#video_path = input("Enter the path to the MP4 video file: ")
62+
video_path = 'canada.mp4'
5363
# Output folder
54-
output_folder = "ExportedImages"
64+
output_folder = "test-ExportedImages"
5565

5666
# Extract frames
5767
video_to_images(video_path, output_folder)

0 commit comments

Comments
 (0)