-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
157 lines (128 loc) · 3.98 KB
/
main.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
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
import cv2
import os
import time
from PIL import Image
from multiprocessing import Process, Queue
def dhash(image, hash_size = 8):
# Grayscale and shrink the image in one step.
image = image.convert('L').resize(
(hash_size + 1, hash_size),
Image.ANTIALIAS,
)
pixels = list(image.getdata())
# Compare adjacent pixels.
difference = []
for row in xrange(hash_size):
for col in xrange(hash_size):
pixel_left = image.getpixel((col, row))
pixel_right = image.getpixel((col + 1, row))
difference.append(pixel_left > pixel_right)
# Convert the binary array to a hexadecimal string.
decimal_value = 0
hex_string = []
for index, value in enumerate(difference):
if value:
decimal_value += 2**(index % 8)
if (index % 8) == 7:
hex_string.append(hex(decimal_value)[2:].rjust(2, '0'))
decimal_value = 0
return ''.join(hex_string)
def mp4tojpg(filename):
if os.path.isfile(("/home/hubeen/Prjct/CoMpOvie/mp4/" + str(filename) + ".mp4")):
vid = cv2.VideoCapture('/home/hubeen/Prjct/CoMpOvie/mp4/' + str(filename) + ".mp4")
suc, img = vid.read()
cnt = 0
suc = True
try:
os.chdir("/home/hubeen/Prjct/CoMpOvie/jpg")
os.mkdir(filename)
while suc:
suc, img = vid.read()
if img is not None:
cv2.imwrite(os.path.join(filename, "frame%d.jpg" % cnt), img)
cnt += 1
except:
print "wtf"
return False
print filename + " End"
else:
print "0x90"
def compare(oripath, chagpath):
ori = Image.open(oripath)
chag = Image.open(chagpath)
return (dhash(ori) == dhash(chag))
def ls(path='./'):
stk = []
if os.path.isdir(path):
rpath = os.path.join(os.path.abspath(path))
for files in os.listdir(rpath):
stk.append(os.path.join(rpath,files))
return stk
def the_world(orist, chagst, oristt, oried, chagstt, chaged, ret):
cnt = 0
a = orist
b = chagst
for x in range(oristt, oried):
for y in range(chagstt, chaged):
if compare(a[x],b[y]):
cnt += 1
ret.put(cnt)
return
def Analyze(orist,chagst,ret):
a = orist
b = chagst
arsz = len(a)
brsz = len(b)
if arsz+brsz >= 2000:
asz = arsz/1000
bsz = brsz/1000
for x in range(0,1000):
exec("pr" + str(x) + " = Process(target=the_world, args=(a, b, asz*" + str(x)+", asz*"+ str(x+1) + ", bsz*" +str(x)+", bsz*"+str(x+1)+", ret))")
# start
for y in range(0,1000):
exec("pr"+ str(y) +".start()")
for z in range(0, 1000):
exec("pr" + str(z) + ".join()")
ret.put('stop')
else:
asz = arsz/100
bsz = brsz/100
for x in range(0,100):
exec("pr" + str(x) + " = Process(target=the_world, args=(a, b, asz*" + str(x)+", asz*"+ str(x+1) + ", bsz*" +str(x)+", bsz*"+str(x+1)+", ret))")
# start
for y in range(0,100):
exec("pr"+ str(y) +".start()")
for z in range(0, 100):
exec("pr" + str(z) + ".join()")
ret.put('stop')
per = 0
while True:
tmp = ret.get()
if tmp == 'stop':
break
else:
per += tmp
if per >= 10:
return True
else:
return False
ret = Queue()
print "Run ..."
print ""
print "Step 1. jpg file extract in mp4 file"
mp4tojpg("ani_ori")
mp4tojpg("ani_chag")
print "extract done!"
a = sorted(ls('ani_ori'))
b = sorted(ls('ani_chag'))
print "ani_ori.mp4 is frame(" + str(len(a)) + ")"
print "ani_chag.mp4 is frame(" + str(len(b)) + ")"
print ""
print "Step 2. Image Compare"
print "Analyze(ani_ori.mp4, ani_chag.mp4)"
s_time = time.time()
if Analyze(a,b,ret):
print "Matches."
else:
print "It does not match."
print 'time = ', time.time()-s_time