-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_report.py
84 lines (67 loc) · 2.35 KB
/
html_report.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
from operator import itemgetter
import sys
import tables
import numpy as np
from jinja2 import Template, Environment, PackageLoader
import os, errno
from core.shots import Shot
from core.videoparser import VideoParser
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
import shutil, errno
def copyanything(src, dst):
try:
shutil.copytree(src, dst)
except OSError as exc: # python >2.5
if exc.errno == errno.ENOTDIR:
shutil.copy(src, dst)
elif exc.errno == errno.EEXIST:
pass
else: raise
filename = sys.argv[1]
hdf_filename = '%s.hdf' % os.path.basename(sys.argv[1])
f = tables.openFile(hdf_filename, 'r+')
clusterings = f.root.clusterings[:]
min_clustering = min(clusterings, key=itemgetter('squared_error'))
clustering_index = np.where(clusterings == min_clustering)
clustering_group = getattr(f.root, 'clustering_%d' % clustering_index, None)
min_size = len(f.root.shots)*0.06
clusters = []
for arr in clustering_group:
if arr.name.startswith('cluster_'):
shots = []
for id in arr[0]:
shot = Shot(f.root.shots[id][0], f.root.shots[id][1])
shot.is_result = id in clustering_group.centroids[0]
shots.append(shot)
clusters.append(shots)
shutil.rmtree('report/css', ignore_errors=True)
shutil.rmtree('report/images', ignore_errors=True)
shutil.rmtree('report/js', ignore_errors=True)
mkdir_p('report')
mkdir_p('report/img')
copyanything('core/assets/css','report/css')
copyanything('core/assets/images','report/images')
copyanything('core/assets/img','report/img')
copyanything('core/assets/js','report/js')
#generate thumbnails
parser = VideoParser(filename)
for i,cluster in enumerate(clusters):
print "%d of %d" % (i, len(clusters))
for shot in cluster:
parser.save_frame(shot.median,
file_name='report/img/%d.jpg' % shot.median)
parser.save_frame(shot.median,
file_name='report/img/%d_thumb.jpg' % shot.median,
width = 60)
#jinja
env = Environment(loader=PackageLoader('core', 'assets'))
template = env.get_template('index_template.html')
html = template.render(clusters = clusters, min_size = min_size)
f=open('report/index.html', 'wb')
f.write(html)