派生自 development/c++

pansen
2019-03-07 979bc003bce710bf300bc2bd87a8278585678763
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
#include "VideoMoveCaptureElement.h"
//#include <basic/pipe_element/ffmpeg/cap_ffmpeg_impl.hpp>
//#todo
#include <basic/pipe_element/ffmpeg/FfmpegElement.cpp>
#include <QtCore/QStringList>
#include <QtCore/QDateTime>
#include <unistd.h>
#include <basic/pipe_element/ffmpeg/ffmpegRecoder/FileRecorder.h>
 
VideoMoveCaptureElement::VideoMoveCaptureElement(const std::string &path, const std::string &outPath,
                                                 int fps, int reOpenTime, int gpuIndex) :
    basic::PipeElement(false), path(path), m_pathOut(outPath), gpuIndex(gpuIndex),
    reopenTime(reOpenTime), outPutInterval(25), outPutIndex(0), bYoloDetect(false) //TODO25
{
 
}
 
void VideoMoveCaptureElement::threadFunc() {
//    _timer.reset();
    usleep(1);
    u_char *data;
    int width = 0, height = 0, step = 0, cn = 0;
    bool ret = capture->grabFrame();
    if (!ret) {
        if (reopenTime < 0) {
            stop();
            INFO("grabFrame faild, element stopping" << path);
            //INFO(_timerTotal.tell_ms());
            return;
        } else {
            usleep(reopenTime * 1000);
            INFO("grabFrame faild, try reopen video: " << path);
            openVideo();
            ret = capture->grabFrame();
            if (!ret)return;
        }
    }
 
//    DBG(_timer.tell_ns());
    if (outPutInterval > 0 && (outPutIndex++ % outPutInterval != 0)) {
        return;
    }
 
    capture->retrieveFrame(0, &data, &step, &width, &height, &cn);
    try {
        cv::Mat img(height, width, CV_8UC3, data, step);
        cv::resize(img, image, cv::Size(192, 108), 0, 0, cv::INTER_LINEAR);
        nChangeLevel = videoChangeScore.EvalChange(image);
    } catch (std::exception ex) {
        ERR("ex is " << ex.what());
    }
 
 
 
//    DBG(nChangeLevel);
 
    fireConnectors();
 
}
 
int VideoMoveCaptureElement::getOutPutInterval() const {
    return outPutInterval;
}
 
void VideoMoveCaptureElement::setOutPutInterval(int value) {
    outPutInterval = value;
}
 
cv::Mat VideoMoveCaptureElement::getImage() const {
    return image;
}
 
void VideoMoveCaptureElement::openVideo() {
    if (gpuIndex >= 0) {
        setenv("CUDA_VISIBLE_DEVICES", std::to_string(gpuIndex).c_str(), 0);
    }
    capture->open(path.c_str(), gpuIndex >= 0);
}
 
int VideoMoveCaptureElement::getOutPutIndex() const {
    return outPutIndex;
}
 
void VideoMoveCaptureElement::startRecord(int startFrame) {
    m_nFrame = startFrame;
}
 
void VideoMoveCaptureElement::endRecord(int endFrame) {
    int startSec = m_nFrame / outPutInterval;
    int endSec = endFrame / outPutInterval;
 
    std::string start = std::to_string(startSec);
    std::string end = std::to_string(endSec);
 
    QString strPath = QString::fromStdString(path);
    QStringList list = strPath.split("/");
    QString fileName;
    QString time;
    QString strNewTime;
    if (list.size() != 0) {
        fileName = list.last();
        time = fileName.split(".").first();
        //#todo
//        time = fileName.split("_").first();
    } else {
        ERR("list.size() == 0)");
    }
 
    if (!time.isEmpty()) {
        QDateTime dt = QDateTime::fromString(time, "yyyy-MM-dd hh:mm:ss");
        int second = m_nFrame / outPutInterval;
        qint64 newTime = dt.toMSecsSinceEpoch() + second * 1000;
        QDateTime newDt = QDateTime::fromMSecsSinceEpoch(newTime);
        strNewTime = newDt.toString("yyyy-MM-dd hh:mm:ss");
    }
 
    //#todo m_pathOut + /
    QString strNewPath = QString(m_pathOut.c_str()) + "/" + strNewTime + QString(".mp4");
 
    fileRecord(path.c_str(), start.c_str(), endSec - startSec, strNewPath.toStdString().c_str());
}
 
bool VideoMoveCaptureElement::getBYoloDetect() const {
    return bYoloDetect;
}
 
int VideoMoveCaptureElement::getChangeLevel() const {
    return nChangeLevel;
}
 
void VideoMoveCaptureElement::threadInitial() {
    capture = new CvCapture_FFMPEG;
    openVideo();
    //_timerTotal.reset();
}
 
void VideoMoveCaptureElement::threadClosing() {
    capture->close();
    delete capture;
    capture = nullptr;
}
 
int VideoMoveCaptureElement::getM_nFrame() const {
    return m_nFrame;
}