mergeyolo.cpp
3.75 KB
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
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#define Port 23
#define BUF_SIZE 1024
using namespace std;
using namespace cv;
int newSockfd, Sockfd;
void Receivedone(int signo)
{
close(Sockfd);
close(newSockfd);
printf("\nconnect done\n");
exit(0);
}
void send_result(int newsfd, int ind) {
FILE* image;
int read_size;
char sync = '\0', msg_buf[BUF_SIZE], file_name[256];
unsigned int fsize;
sprintf(file_name, "img_%03d_result.jpg", ind);
printf("%s\n", file_name);
if ((image = fopen(file_name,"r")) == NULL) {
printf("image load error");
close(Sockfd);
close(newSockfd);
exit(1);
}
///// file size
fseek(image, 0, SEEK_END);
fsize = ftell(image);
fseek(image, 0, SEEK_SET);
sprintf(msg_buf,"%d", fsize);
if(write(newsfd, (char*)&msg_buf, sizeof(msg_buf)-1) < 0) {
perror("write");
exit(1);
}
printf("\nSent picture : %ssize\n", file_name);
// send image file
while(!feof(image)) {
memset(msg_buf, 0, BUF_SIZE);
read_size = fread(msg_buf, 1, sizeof(msg_buf), image); // get image file
if(write(newsfd, (char*)&msg_buf, read_size) < 0) { // send image file
perror("write");
exit(1);
}
read(newsfd, (char*)&sync, sizeof(sync)); // for sync
}
if(read(newsfd, (char*)&msg_buf, sizeof(msg_buf)) < 0) {
perror("read");
exit(1);
} // 종료 확인
bzero(msg_buf, sizeof(msg_buf));
fclose(image);
}
int main(int argc, char* argv[]) {
char buf[256], cwd[256], res_cwd[256], dest_cwd[256], file_ind[10], command[256];
int hostAddrLen, n, option, index = 0;
struct sockaddr_in hostAddr, boardAddr;
getcwd(cwd, 256);
signal(SIGINT, Receivedone); // 종료 signal
// camera setting
unsigned int CAM_ID = cv::CAP_V4L;
Mat frame;
VideoCapture cam(0+CAM_ID);
if(!cam.isOpened()) {
printf("Can't open the CAM(%d)\n", CAM_ID);
return -1;
}
cam.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cam.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
// socket setting
if((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
option = 1; // SO_REUSEADDR 의 옵션 값을 TRUE 로
setsockopt(Sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); // set socket reusable
bzero((char*)&boardAddr, sizeof(boardAddr));
boardAddr.sin_family = PF_INET;
boardAddr.sin_addr.s_addr = htonl(INADDR_ANY);
boardAddr.sin_port = htons(Port);
if(bind(Sockfd, (struct sockaddr*)&boardAddr, sizeof(boardAddr)) < 0) {
perror("bind");
exit(1);
}
listen(Sockfd, 5);
printf("\nWait connecting\n\n");
hostAddrLen = sizeof(hostAddr);
newSockfd = accept(Sockfd, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrLen);
if(newSockfd < 0) {
perror("accept");
exit(1);
} // === 연결 완료
if((n = read(newSockfd, (char*)&dest_cwd, sizeof(dest_cwd))) < 0) { //
perror("read");
exit(1);
} // host cwd 받아오기
while(1) {
// ---------- 이미지 읽어오기
for(int i = 0; i <5; i++) // buffer claer
cam.grab();
cam.read(frame);
sprintf(file_ind, "/img_%03d.jpg", index);
strcpy(res_cwd, cwd);
strcat(res_cwd, file_ind); // cwd와 파일 이름 설정
printf("%s\n",res_cwd);
imwrite(res_cwd, frame); // 이미지로 저장
// ---------- yolo 실행
strcpy(command, "~/Vitis-AI/vitis_ai_library/samples/yolov3/test_jpeg_yolov3 yolov3_bdd ");
strcat(command, res_cwd);
printf("start : %s\n", command);
if(system(command)) { // yolo 실행
perror("system");
exit(1);
}
// --------- send result
send_result(newSockfd, index);
printf("\nSuccess\n=========================================================\n\n");
index++;
if(index == 999) index = 0;
}
return 0;
}