shvtr159

projrct file

1 +#include <opencv2/opencv.hpp>
2 +#include <stdio.h>
3 +#include <stdlib.h>
4 +#include <unistd.h>
5 +#include <sys/types.h>
6 +#include <sys/socket.h>
7 +#include <netinet/in.h>
8 +#include <arpa/inet.h>
9 +#include <string.h>
10 +#include <signal.h>
11 +
12 +#define Port 23
13 +#define BUF_SIZE 1024
14 +
15 +using namespace std;
16 +using namespace cv;
17 +
18 +int newSockfd, Sockfd;
19 +
20 +void Receivedone(int signo)
21 +{
22 + close(Sockfd);
23 + close(newSockfd);
24 + printf("\nconnect done\n");
25 +
26 + exit(0);
27 +}
28 +
29 +void send_result(int newsfd, int ind) {
30 + FILE* image;
31 + int read_size;
32 + char sync = '\0', msg_buf[BUF_SIZE], file_name[256];
33 + unsigned int fsize;
34 +
35 + sprintf(file_name, "img_%03d_result.jpg", ind);
36 + printf("%s\n", file_name);
37 + if ((image = fopen(file_name,"r")) == NULL) {
38 + printf("image load error");
39 + close(Sockfd);
40 + close(newSockfd);
41 + exit(1);
42 + }
43 +
44 + ///// file size
45 + fseek(image, 0, SEEK_END);
46 + fsize = ftell(image);
47 + fseek(image, 0, SEEK_SET);
48 + sprintf(msg_buf,"%d", fsize);
49 + if(write(newsfd, (char*)&msg_buf, sizeof(msg_buf)-1) < 0) {
50 + perror("write");
51 + exit(1);
52 + }
53 +
54 + printf("\nSent picture : %ssize\n", file_name);
55 + // send image file
56 +
57 + while(!feof(image)) {
58 + memset(msg_buf, 0, BUF_SIZE);
59 + read_size = fread(msg_buf, 1, sizeof(msg_buf), image); // get image file
60 + if(write(newsfd, (char*)&msg_buf, read_size) < 0) { // send image file
61 + perror("write");
62 + exit(1);
63 + }
64 + read(newsfd, (char*)&sync, sizeof(sync)); // for sync
65 + }
66 +
67 + if(read(newsfd, (char*)&msg_buf, sizeof(msg_buf)) < 0) {
68 + perror("read");
69 + exit(1);
70 + } // 종료 확인
71 +
72 + bzero(msg_buf, sizeof(msg_buf));
73 + fclose(image);
74 +}
75 +
76 +int main(int argc, char* argv[]) {
77 + char buf[256], cwd[256], res_cwd[256], dest_cwd[256], file_ind[10], command[256];
78 + int hostAddrLen, n, option, index = 0;
79 + struct sockaddr_in hostAddr, boardAddr;
80 + getcwd(cwd, 256);
81 +
82 + signal(SIGINT, Receivedone); // 종료 signal
83 +
84 + // camera setting
85 + unsigned int CAM_ID = cv::CAP_V4L;
86 + Mat frame;
87 + VideoCapture cam(0+CAM_ID);
88 + if(!cam.isOpened()) {
89 + printf("Can't open the CAM(%d)\n", CAM_ID);
90 + return -1;
91 + }
92 + cam.set(CV_CAP_PROP_FRAME_WIDTH, 640);
93 + cam.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
94 +
95 + // socket setting
96 + if((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
97 + perror("socket");
98 + exit(1);
99 + }
100 + option = 1; // SO_REUSEADDR 의 옵션 값을 TRUE 로
101 + setsockopt(Sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); // set socket reusable
102 +
103 + bzero((char*)&boardAddr, sizeof(boardAddr));
104 + boardAddr.sin_family = PF_INET;
105 + boardAddr.sin_addr.s_addr = htonl(INADDR_ANY);
106 + boardAddr.sin_port = htons(Port);
107 +
108 + if(bind(Sockfd, (struct sockaddr*)&boardAddr, sizeof(boardAddr)) < 0) {
109 + perror("bind");
110 + exit(1);
111 + }
112 +
113 + listen(Sockfd, 5);
114 +
115 + printf("\nWait connecting\n\n");
116 +
117 + hostAddrLen = sizeof(hostAddr);
118 +
119 + newSockfd = accept(Sockfd, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrLen);
120 + if(newSockfd < 0) {
121 + perror("accept");
122 + exit(1);
123 + } // === 연결 완료
124 +
125 + if((n = read(newSockfd, (char*)&dest_cwd, sizeof(dest_cwd))) < 0) { //
126 + perror("read");
127 + exit(1);
128 + } // host cwd 받아오기
129 +
130 + while(1) {
131 + // ---------- 이미지 읽어오기
132 + for(int i = 0; i <5; i++) // buffer claer
133 + cam.grab();
134 + cam.read(frame);
135 + sprintf(file_ind, "/img_%03d.jpg", index);
136 + strcpy(res_cwd, cwd);
137 + strcat(res_cwd, file_ind); // cwd와 파일 이름 설정
138 + printf("%s\n",res_cwd);
139 + imwrite(res_cwd, frame); // 이미지로 저장
140 +
141 + // ---------- yolo 실행
142 + strcpy(command, "~/Vitis-AI/vitis_ai_library/samples/yolov3/test_jpeg_yolov3 yolov3_bdd ");
143 + strcat(command, res_cwd);
144 +
145 + printf("start : %s\n", command);
146 + if(system(command)) { // yolo 실행
147 + perror("system");
148 + exit(1);
149 + }
150 + // --------- send result
151 + send_result(newSockfd, index);
152 + printf("\nSuccess\n=========================================================\n\n");
153 +
154 + index++;
155 + if(index == 999) index = 0;
156 + }
157 +
158 + return 0;
159 +}
1 +#include <stdio.h>
2 +#include <sys/types.h>
3 +#include <sys/socket.h>
4 +#include <netinet/in.h>
5 +#include <arpa/inet.h>
6 +#include <signal.h>
7 +#include <stdlib.h>
8 +#include <string.h>
9 +#include <unistd.h>
10 +
11 +#define Port 23
12 +#define BUF_SIZE 1024
13 +int newSockfd, Sockfd;
14 +
15 +void Receivedone(int signum)
16 +{
17 + close(Sockfd);
18 + close(newSockfd);
19 + printf("\nconnect done\n");
20 +
21 + exit(0);
22 +}
23 +
24 +void send_result(int newsfd) {
25 + FILE* image;
26 + int read_size;
27 + char sync = '\0', msg_buf[BUF_SIZE], file_name[256];
28 + unsigned int fsize;
29 + static int ind = 0;
30 +
31 + sprintf(file_name, "img_%3d_result.jpg", ind);
32 + while((image = fopen(file_name,"r")) == NULL) {sleep(1);}
33 +
34 + ///// file size
35 + fseek(image, 0, SEEK_END);
36 + fsize = ftell(image);
37 + fseek(image, 0, SEEK_SET);
38 + sprintf(msg_buf,"%d", fsize);
39 + if(write(newsfd, (char*)&msg_buf, sizeof(msg_buf)-1) < 0) {
40 + perror("write");
41 + exit(1);
42 + }
43 +
44 + printf("Sent picture : %dsize\n", fsize);
45 + // send image file
46 +
47 + while(!feof(image)) {
48 + memset(msg_buf, 0, BUF_SIZE);
49 + read_size = fread(msg_buf, 1, sizeof(msg_buf), image); // get image file
50 + if(write(newsfd, (char*)&msg_buf, read_size) < 0) { // send image file
51 + perror("write");
52 + exit(1);
53 + }
54 + read(newsfd, (char*)&sync, sizeof(sync)); // for sync
55 + }
56 +
57 + if(read(newsfd, (char*)&msg_buf, sizeof(msg_buf)) < 0) {
58 + perror("read");
59 + exit(1);
60 + } // 종료 확인
61 +
62 + bzero(msg_buf, sizeof(msg_buf));
63 + fclose(image);
64 + ind++;
65 + if(ind == 999) ind = 0;
66 +}
67 +
68 +int main(int argc, char* argv[])
69 +{
70 + int hostAddrLen, n, option;
71 + struct sockaddr_in hostAddr, boardAddr;
72 + char cwd[256];
73 + pid_t pid;
74 +
75 + if((pid = fork()) < 0) { // error
76 + perror("fork");
77 + exit(1);
78 + }
79 + else if (pid > 0) { // only do parent process
80 + signal(SIGINT, Receivedone); // 종료 signal
81 +
82 + if((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
83 + perror("socket");
84 + exit(1);
85 + }
86 + option = 1; // SO_REUSEADDR 의 옵션 값을 TRUE 로
87 + setsockopt(Sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); // set socket reusable
88 +
89 + bzero((char*)&boardAddr, sizeof(boardAddr));
90 + boardAddr.sin_family = PF_INET;
91 + boardAddr.sin_addr.s_addr = htonl(INADDR_ANY);
92 + boardAddr.sin_port = htons(Port);
93 +
94 + if(bind(Sockfd, (struct sockaddr*)&boardAddr, sizeof(boardAddr)) < 0) {
95 + perror("bind");
96 + exit(1);
97 + }
98 +
99 + listen(Sockfd, 5);
100 +
101 + printf("\nWait connecting\n\n");
102 +
103 + hostAddrLen = sizeof(hostAddr);
104 +
105 + newSockfd = accept(Sockfd, (struct sockaddr*)&hostAddr,(socklen_t*)&hostAddrLen);
106 + if(newSockfd < 0) {
107 + perror("accept");
108 + exit(1);
109 + } // === 연결 완료
110 +
111 + if((n = read(newSockfd, (char*)&cwd, sizeof(cwd))) < 0) { //
112 + perror("read");
113 + exit(1);
114 + } // host cwd 받아오기
115 +
116 + // 전송 시작
117 + }
118 + while(1) {
119 + if(pid == 0) { // do yolo
120 +
121 + }
122 + send_result(newSockfd);
123 + printf("\nSuccess\n=========================================================\n\n");
124 + }
125 + return 0;
126 +}
1 +mergeyolo.cpp -> board 프로그램
2 +wcap_host -> host 프로그램
3 +
4 +mergeyolo를 실행
5 +-> 소켓 대기하고 cam을 열어둠
6 +-> host에서 wcap_host가 실행
7 +-> board와 host 연결. usb 카메라 사진을 받아서 yolo처리
8 +-> host로 전송
9 +반복