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
/*
Example code for displaying gstreamer video from the CSI port of the Nvidia Jetson in OpenCV.
Created by Peter Moran on 7/29/17.
 
Update by inseock, seongjun on 2019.07.12
 
*/
#include <opencv2/opencv.hpp>
#include <iostream>
#include <ctime>
#include <list>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
using namespace cv;
using namespace std;
 
std::string get_tegra_pipeline(int width, int height, int fps) {
    return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + std::to_string(width) + ", height=(int)" +
        std::to_string(height) + ", format=(string)NV12, framerate=(fraction)" + std::to_string(fps) +
        "/1 ! nvvidconv flip-method=0 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
}
 
int main() 
{
    // Options
    int WIDTH = 640;
    int HEIGHT = 480;
    int FPS = 10;
 
    // Define the gstream pipeline
    std::string pipeline = get_tegra_pipeline(WIDTH, HEIGHT, FPS);
    std::cout << "Using pipeline: \n\t" << pipeline << "\n";
 
    // Create OpenCV capture object, ensure it works.
    cv::VideoCapture cap(pipeline, cv::CAP_GSTREAMER);
    if (!cap.isOpened()) {
        std::cout << "Connection failed";
        return -1;
    }
 
    // View video
    cv::Mat frame;
 
    while (1)
    {
        time_t curr_time;
        struct tm *curr_tm;
        curr_time = time(NULL);
        curr_tm = localtime(&curr_time);
        int num;
        num = (curr_tm->tm_min + 1) % 60;
 
        cout << "======= num =========" << endl;
        cout << num << endl;
 
        VideoWriter video("save_avi/" +
            to_string(curr_tm->tm_year + 1900+ "_" +
            to_string(curr_tm->tm_mon + 1+ "_" +
            to_string(curr_tm->tm_mday) + "_" +
            to_string(curr_tm->tm_hour) + ":" +
            to_string(curr_tm->tm_min) + ":" +
            to_string(curr_tm->tm_sec) + ".avi", CV_FOURCC('M''J''P''G'), FPS, Size(WIDTH, HEIGHT), true);
        
        while (1) {
 
            // time 함수는 1970년 1월 1일 이후 몇초가 지났는지를 계산합니다. NULL을 인자로 사용합니다.
            curr_time = time(NULL);
 
            // 지역 시간을 기준으로 변환 및 출력 편의를 위하여 tm 구조체에 저장합니다.(포맷팅)
            curr_tm = localtime(&curr_time);
 
            cap >> frame; // Get a new frame from camera
                          // Display frame
 
            cv::putText(frame,
                to_string(curr_tm->tm_year + 1900+ "." +
                to_string(curr_tm->tm_mon + 1+ "." +
                to_string(curr_tm->tm_mday) + "." +
                to_string(curr_tm->tm_hour) + ":" +
                to_string(curr_tm->tm_min) + ":" +
                to_string(curr_tm->tm_sec),
 
                Point(30450), 11.2, Scalar::all(255));
 
            video.write(frame);
            imshow("Display window", frame);
            cv::waitKey(1); //needed to show frame
 
            if (curr_tm->tm_min == num)
                break;
        }
 
        FILE *fp;
        list<string> avi;
 
        char buf[255= "\0";
        fp = popen("ls save_avi/""r");
 
        if (fp == NULL)
        {
            perror("error");
            exit(0);
        }
 
        while (fgets(buf, 255, fp) != NULL)
        {
            avi.push_back(buf);
        }
 
        avi.sort();
 
        if (avi.size()<3)
        {
        }
        else
        {
            char buf2[255= "\0";    
            strcpy(buf, avi.front().c_str());
            int i = 0;
            char buff_null[255= "\0";
 
            chdir("/home/user/0712black/save_avi/");
 
            while (buf[i + 1!= '\0')
            {
                buff_null[i] = buf[i];
                i++;
            }
 
            if (remove(buff_null) == 0)
            {
                cout << "ok" << endl;
            }
            else 
            {
                cout << "error" << endl;
            }
            chdir("/home/user/0712black/");
        }
    }
    cout << "exit!!" << endl;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

개발환경

Jetson TX2

opencv 3.3.1

c++

+ Recent posts