Skip to content

Commit

Permalink
video colourization
Browse files Browse the repository at this point in the history
  • Loading branch information
imvickykumar999 committed Nov 23, 2023
1 parent 0bc757e commit 2d3652b
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[InternetShortcut]
URL=https://www.youtube.com/playlist?list=PLyeWzbpbicN032fYAmjBoxPQn9AlB1uwl
104 changes: 104 additions & 0 deletions Video Colourization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

# `Video_colorization`.[py](https://github.com/imvickykumar999/Video-Colorization/blob/main/video_colorization.py)

pip install -r requirements.txt

-----------------

## `Uploaded` [`PlayList`](https://www.youtube.com/playlist?list=PLyeWzbpbicN032fYAmjBoxPQn9AlB1uwl)

- [Beqaraar Karake Hamen Yun Na Jaaiye](https://youtu.be/r4cJrr0WczE) **(32.4k+ views)**
- [Mera Joota Hai Japani](https://www.youtube.com/shorts/FlktPZTxCq4)
- [Haal Kaisa Hai Janaab Ka](https://youtu.be/dRTmhoUrr_0) **(7.4k+ views)**
- [Itna Na Mujhse Tu Pyaar Badha](https://www.youtube.com/watch?v=AFy573PU8Og)
- [Aa chal ke tujhe main leke chaloon](https://www.youtube.com/watch?v=gqDkgqpBrwo)

--------------------------------

## `Transformed Video`

https://user-images.githubusercontent.com/50515418/208450925-81a28039-1ee6-48e6-afc4-e41ebb9ab95c.mp4

### `It took 20 minutes to transform only 10 seconds video.`

## `Original Video`

https://user-images.githubusercontent.com/50515418/208451041-b5ab039c-36d4-432f-9047-f15d3da0bea9.mp4

----------------------

**Clone the repository; install dependencies**

```
gh repo clone imvickykumar999/Video-Colorization
pip install requirements.txt
```

**Colorize!** This script will colorize an image. The results should match the images in the `imgs_out` folder.

```
python demo_release.py -i imgs/output.jpg
```

**Model loading in Python** The following loads pretrained colorizers. See [demo_release.py](https://github.com/imvickykumar999/Video-Colorization/blob/main/extras/demo_release.py) for some details on how to run the model. There are some pre and post-processing steps: convert to Lab space, resize to 256x256, colorize, and concatenate to the original full resolution, and convert to RGB.

```python
import colorizers
colorizer_eccv16 = colorizers.eccv16().eval()
colorizer_siggraph17 = colorizers.siggraph17().eval()
```

--------------------

### Original implementation (Caffe branch)

https://pub.towardsai.net/colorizing-images-with-deep-learning-a34d11587643

The original implementation contained train and testing, our network and AlexNet (for representation learning tests), as well as representation learning tests. It is in Caffe and is no longer supported. Please see the [caffe](https://github.com/richzhang/colorization/tree/caffe) branch for it.

### Citation ###

If you find these models useful for your resesarch, please cite with these bibtexs.

```
@inproceedings{zhang2016colorful,
title={Colorful Image Colorization},
author={Zhang, Richard and Isola, Phillip and Efros, Alexei A},
booktitle={ECCV},
year={2016}
}
@article{zhang2017real,
title={Real-Time User-Guided Image Colorization with Learned Deep Priors},
author={Zhang, Richard and Zhu, Jun-Yan and Isola, Phillip and Geng, Xinyang and Lin, Angela S and Yu, Tianhe and Efros, Alexei A},
journal={ACM Transactions on Graphics (TOG)},
volume={9},
number={4},
year={2017},
publisher={ACM}
}
```

----------------------------------

## `Input command` in [Curl Commands Online](https://reqbin.com/curl)

curl \
-F 'image=https://keyassets.timeincuk.net/inspirewp/live/wp-content/uploads/sites/12/2019/01/BW-using-curves.jpg' \
-H 'api-key:c34c7088-11a1-456b-9a77-1384500eb46f' \
https://api.deepai.org/api/colorizer

-----------------------

## `Output Response` : [deepai.org](https://deepai.org/machine-learning-model/colorizer) need payment of 5 doller per month.
`I avoid such websites which need payments, this repository can do same for free.`

{
"status": "Out of API credits - please enter payment info in your dashboard: https://deepai.org/dashboard"
}

---------------------------

## `Output Video` on : [YouTube](https://youtube.com/shorts/FlktPZTxCq4?feature=share)

![image](https://user-images.githubusercontent.com/50515418/208730165-93c8807d-8cf7-4626-827d-eaf191b71e06.png)
7 changes: 7 additions & 0 deletions Video Colourization/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
torch
scikit-image
numpy>=1.16.5,<1.23.0
matplotlib
argparse
Pillow
torchvision
139 changes: 139 additions & 0 deletions Video Colourization/video_colorization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@

from moviepy.editor import VideoFileClip, AudioFileClip
import moviepy.video.fx.all as vfx
import argparse, os, cv2, shutil
import matplotlib.pyplot as plt
from colorizers import *
from PIL import Image

def step1():
try:
shutil.rmtree('vid_out')
except:
pass

try:
os.mkdir('vid_out')
except:
pass

try:
os.mkdir('vid')
except:
pass

file_name = 'vid/oldsong.mp4'
vidcap = cv2.VideoCapture(file_name)
success,image = vidcap.read()
count = 0

while success:
cv2.imwrite(f"vid_out/{str(count).zfill(6)}.jpg", image) # save frame as JPEG file
success,image = vidcap.read()
print(f'Read {count+1} frame: ', success)
count += 1

def step2():
try:
shutil.rmtree('bw_vid_out')
except:
pass

try:
os.mkdir('bw_vid_out')
except:
pass

def magic(input_path, output_path):
parser = argparse.ArgumentParser()
parser.add_argument('--use_gpu', action='store_true', help='whether to use GPU')
opt = parser.parse_args()

colorizer_eccv16 = eccv16(pretrained=True).eval()
colorizer_siggraph17 = siggraph17(pretrained=True).eval()
if(opt.use_gpu):
colorizer_eccv16.cuda()
colorizer_siggraph17.cuda()

img = load_img(input_path)
(tens_l_orig, tens_l_rs) = preprocess_img(img, HW=(256,256))
if(opt.use_gpu):
tens_l_rs = tens_l_rs.cuda()

img_bw = postprocess_tens(tens_l_orig, torch.cat((0*tens_l_orig,0*tens_l_orig), dim=1))
out_img_eccv16 = postprocess_tens(tens_l_orig, colorizer_eccv16(tens_l_rs).cpu())
plt.imsave(output_path, out_img_eccv16)

images = [img for img in os.listdir('vid_out')
if img.endswith(".jpg") or
img.endswith(".jpeg") or
img.endswith("png")]

for i in images:
print('Processing for frame : ', i)
magic(f'vid_out/{i}', f'bw_vid_out/{i}')

def step3():
mean_height = 0
mean_width = 0

path = 'bw_vid_out'
num_of_images = len(os.listdir('bw_vid_out'))
print(f'num_of_images = {num_of_images}')

for file in os.listdir('bw_vid_out'):
im = Image.open(os.path.join(path, file))
width, height = im.size
mean_width += width
mean_height += height

mean_width = int(mean_width / num_of_images)
mean_height = int(mean_height / num_of_images)

for file in os.listdir('bw_vid_out'):
if file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith("png"):
im = Image.open(os.path.join(path, file))

width, height = im.size
imResize = im.resize((mean_width, mean_height), Image.Resampling.LANCZOS)
imResize.save(os.path.join(path, file), 'JPEG', quality = 95)
print(im.filename.split('\\')[-1], " is resized")

def generate_video():
image_folder = 'bw_vid_out'
video_name = 'mygeneratedvideo.avi'

images = [img for img in os.listdir(image_folder)
if img.endswith(".jpg") or
img.endswith(".jpeg") or
img.endswith("png")]

frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
video = cv2.VideoWriter(os.path.join('vid', video_name), 0, 1, (width, height))

for image in images:
print('Writing for ', image)
video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

generate_video()

def step4():
in_loc = 'vid/mygeneratedvideo.avi'
out_loc = 'vid/coloured.mp4'

clip = VideoFileClip(in_loc)
clip = clip.set_fps(clip.fps * 30)
final = clip.fx(vfx.speedx, 30)

audioclip = AudioFileClip("vid/oldsong.mp4")
videoclip = final.set_audio(audioclip)
videoclip.write_videofile(out_loc)

step1()
step2()
step3()
step4()

0 comments on commit 2d3652b

Please sign in to comment.