-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_searcher.py
109 lines (81 loc) · 3.2 KB
/
image_searcher.py
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
import numpy as np
import csv
import argparse
import cv2
import imutils
import os
import moviepy
from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
from get_csv import *
def image_stats(image):
(l,a,b) = cv2.split(image)
(lMean, lStd) = (l.mean(), l.std())
(aMean, aStd) = (a.mean(), a.std())
(bMean, bStd) = (b.mean(), b.std())
return lMean, lStd, aMean, aStd, bMean, bStd
def color_transfer(source, input_image):
source = cv2.cvtColor(source, cv2.COLOR_BGR2LAB).astype("float32")
(lMeanSrc, lStdSrc, aMeanSrc, aStdSrc, bMeanSrc, bStdSrc) = image_stats(source)
target = cv2.cvtColor(input_image, cv2.COLOR_BGR2LAB).astype("float32")
(lMeanTar, lStdTar, aMeanTar, aStdTar, bMeanTar, bStdTar) = image_stats(target)
(l, a, b) = cv2.split(target)
l -= lMeanTar
a -= aMeanTar
b -= bMeanTar
l = (lStdTar / lStdSrc) * l
a = (aStdTar / aStdSrc) * a
b = (bStdTar / bStdSrc) * b
l += lMeanSrc
a += aMeanSrc
b += bMeanSrc
transfer = cv2.merge([l, a, b])
transfer = cv2.cvtColor(transfer.astype("uint8"), cv2.COLOR_LAB2BGR)
return transfer
def load_frames(video):
vidObj = cv2.VideoCapture(video)
count=0
flag=1
images = []
while flag:
flag, image = vidObj.read()
images.append(image)
if flag:
count += 1
return images
def create_timelapse_video(input_image, image_id,path):
print("Matched with one of the frame of video with id: "+ image_id)
images = load_frames(path+image_id+".mp4")
total_frame = len(images)
image_files = []
count=0
printProgressBar(0, total_frame, prefix = 'Progress:', suffix = 'Complete', length = 50)
for image in images:
count +=1
printProgressBar(count + 1, total_frame, prefix = 'Progress:', suffix = 'Complete', length = 50)
if not image is None:
styled_image = color_transfer(image,input_image)
name = "styled_image"+str(count) +".jpg"
cv2.imwrite(name, styled_image)
read_frame = cv2.imread(name)
image_files.append(name)
clip = ImageSequenceClip(image_files, fps=10)
clip.write_videofile('output.mp4')
cv2.destroyAllWindows()
for k in image_files:
os.system("rm "+k)
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
if iteration == total:
print()
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("-q", "--query", required = True, help = "Path to the query image")
ap.add_argument("-r", "--result-path", required = True, help = "Path to the result path")
args = vars(ap.parse_args())
query = cv2.imread(args["query"])
query1 = cv2.resize(query,(150,150))
results = get_target_image(query1,'./video_frames')
create_timelapse_video(query, results,args["result_path"])