-
Notifications
You must be signed in to change notification settings - Fork 50
/
ShotSegmenter.java
207 lines (166 loc) · 5.53 KB
/
ShotSegmenter.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package org.vitrivr.cineast.core.segmenter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.vitrivr.cineast.core.data.Frame;
import org.vitrivr.cineast.core.data.Histogram;
import org.vitrivr.cineast.core.data.Shot;
import org.vitrivr.cineast.core.data.providers.ShotProvider;
import org.vitrivr.cineast.core.db.PersistencyWriter;
import org.vitrivr.cineast.core.db.PersistentTuple;
import org.vitrivr.cineast.core.db.SgmentLookup.SegmentDescriptor;
import org.vitrivr.cineast.core.decode.subtitle.SubTitle;
import org.vitrivr.cineast.core.decode.subtitle.SubtitleItem;
import org.vitrivr.cineast.core.decode.video.VideoDecoder;
public class ShotSegmenter implements ShotProvider{
private static final double THRESHOLD = 0.05;
private static final int PRE_QUEUE_LEN = 10;
private static final int MAX_SHOT_LENGTH = 720;
private VideoDecoder vdecoder;
private final String movieId;
private LinkedList<Frame> frameQueue = new LinkedList<>();
private LinkedList<DoublePair<Frame>> preShotQueue = new LinkedList<>();
private ArrayList<SubTitle> subtitles = new ArrayList<SubTitle>();
@SuppressWarnings("rawtypes")
private PersistencyWriter pwriter;
private List<SegmentDescriptor> knownShotBoundaries;
public ShotSegmenter(VideoDecoder vdecoder, String movieId, @SuppressWarnings("rawtypes") PersistencyWriter pwriter, List<SegmentDescriptor> knownShotBoundaries){
this.vdecoder = vdecoder;
this.movieId = movieId;
this.pwriter = pwriter;
this.pwriter.setFieldNames("id", "multimediaobject", "sequencenumber", "segmentstart", "segmentend");
this.pwriter.open("cineast_segment");
this.knownShotBoundaries = ((knownShotBoundaries == null) ? new LinkedList<SegmentDescriptor>() : knownShotBoundaries);
}
public void addSubTitle(SubTitle st) {
this.subtitles.add(st);
}
private boolean queueFrames(){
return queueFrames(20);
}
private boolean queueFrames(int number){
Frame f;
for(int i = 0; i < number; ++i){
f = this.vdecoder.getFrame();
if(f == null){ //no more frames
return false;
}else{
this.frameQueue.offer(f);
}
}
return true;
}
public Shot getNextShot(){
if(this.frameQueue.isEmpty()){
queueFrames();
}
Shot _return = null;
if (!preShotQueue.isEmpty()){
_return = new Shot(this.movieId, this.vdecoder.getTotalFrameCount());
while (!preShotQueue.isEmpty()) {
_return.addFrame(preShotQueue.removeFirst().first);
}
}
if(this.frameQueue.isEmpty()){
return finishShot(_return); //no more shots to segment
}
if(_return == null){
_return = new Shot(this.movieId, this.vdecoder.getTotalFrameCount());
}
Frame frame = this.frameQueue.poll();
SegmentDescriptor bounds = this.knownShotBoundaries.size() > 0 ? this.knownShotBoundaries.remove(0) : null;
if (bounds != null && frame.getId() >= bounds.getStartFrame() && frame.getId() <= bounds.getEndFrame()){
_return.addFrame(frame);
queueFrames(bounds.getEndFrame() - bounds.getStartFrame());
do{
frame = this.frameQueue.poll();
if(frame != null){
_return.addFrame(frame);
}else{
break;
}
}while(frame.getId() < bounds.getEndFrame());
_return.setShotId(bounds.getShotId());
addSubtitleItems(_return);
idCounter.incrementAndGet();
return _return;
}else{
Histogram hPrev, h = getHistogram(frame);
_return.addFrame(frame);
while (true) {
if ((frame = this.frameQueue.poll()) == null) {
queueFrames();
if ((frame = this.frameQueue.poll()) == null) {
return finishShot(_return);
}
}
hPrev = h;
h = getHistogram(frame);
double distance = hPrev.getDistance(h);
preShotQueue.offer(new DoublePair<Frame>(frame, distance));
if (preShotQueue.size() > PRE_QUEUE_LEN) {
double max = 0;
int index = -1, i = 0;
for (DoublePair<Frame> pair : preShotQueue) {
if (pair.second > max) {
index = i;
max = pair.second;
}
i++;
}
if (max <= THRESHOLD && _return.getNumberOfFrames() < MAX_SHOT_LENGTH) { //no cut
for (DoublePair<Frame> pair : preShotQueue) {
_return.addFrame(pair.first);
}
preShotQueue.clear();
} else {
for (i = 0; i < index; ++i) {
_return.addFrame(preShotQueue.removeFirst().first);
}
break;
}
}
}
return finishShot(_return);
}
}
private static Histogram getHistogram(Frame f){
return FuzzyColorHistogramCalculator.getSubdividedHistogramNormalized(f.getImage().getThumbnailImage(), 3);
}
private AtomicInteger idCounter = new AtomicInteger(0);
@SuppressWarnings({ "unchecked", "rawtypes" })
private Shot finishShot(Shot shot){
if(shot == null){
return null;
}
int shotNumber = idCounter.incrementAndGet();
String shotId = Shot.generateShotID(movieId, shotNumber);
shot.setShotId(shotId);
addSubtitleItems(shot);
PersistentTuple tuple = this.pwriter.generateTuple(shotId, movieId, shot.getStart(), shot.getEnd());
this.pwriter.persist(tuple);
return shot;
}
private void addSubtitleItems(Shot shot){
int start = shot.getStart();
int end = shot.getEnd();
for(SubTitle st : this.subtitles){
for(int i = 1; i <= st.getNumerOfItems(); ++i){
SubtitleItem si = st.getItem(i);
if(si == null || start > si.getEndFrame() || end < si.getStartFrame()){
continue;
}
shot.addSubtitleItem(si);
}
}
}
}
class DoublePair<K>{
K first;
double second;
DoublePair(K first, double second){
this.first = first;
this.second = second;
}
}