mergeyolo.cpp 3.75 KB
#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;
}