Created
November 11, 2015 08:15
-
-
Save northeastsquare/a4b9d03d9a7712cd9346 to your computer and use it in GitHub Desktop.
clientdarknet cpp file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include<errno.h> | |
#include<sys/types.h> | |
#include<sys/socket.h> | |
#include<netinet/in.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#include <libavcodec/avcodec.h> | |
#include <libavformat/avformat.h> | |
#include <libavformat/avio.h> | |
#include <libswscale/swscale.h> | |
typedef struct { | |
int h; | |
int w; | |
int c; | |
char *data; | |
} image; | |
struct nodeBC | |
{ | |
int c; | |
int left, top, right, bot; | |
struct nodeBC* next; | |
float prob; | |
}; | |
char *class_names[] = {"aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"}; | |
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) | |
{ | |
FILE *pFile; | |
char szFilename[32]; | |
int y; | |
// Open file | |
sprintf(szFilename, "frame%d.ppm", iFrame); | |
pFile=fopen(szFilename, "wb"); | |
if(pFile==NULL) | |
return; | |
// Write header | |
fprintf(pFile, "P6\n%d %d\n255\n", width, height); | |
// Write pixel data | |
for(y=0; y<height; y++) | |
fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int sockfd, n; | |
char recvline[4096], sendline[4096] = "sdf"; | |
struct sockaddr_in servaddr; | |
char *g_bufDetectionRes; | |
int g_bytesBufDetectionRes = 0; | |
int MAX_G_AVFRAME_SIZE = 1920*1080*3*sizeof(char)+20*(5*sizeof(int) + sizeof(struct nodeBC *)) + 1920*1080; | |
int bufSize = MAX_G_AVFRAME_SIZE; | |
int isFirstImage = 1; | |
image im ; | |
IplImage *disp; | |
g_bufDetectionRes = (char *)malloc(MAX_G_AVFRAME_SIZE); | |
memset(g_bufDetectionRes, 0, MAX_G_AVFRAME_SIZE); | |
if( (sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ | |
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno); | |
exit(0); | |
} | |
memset(&servaddr, 0, sizeof(servaddr)); | |
servaddr.sin_family = AF_INET; | |
servaddr.sin_port = htons(8800); | |
if( inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr) <= 0){ | |
printf("inet_pton error for"); | |
exit(0); | |
} | |
if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){ | |
printf("connect error: %s(errno: %d)\n",strerror(errno),errno); | |
exit(0); | |
} | |
printf("send msg to server: \n"); | |
//fgets(sendline, 4096, stdin); | |
//sendline = "ABCD"; | |
if( send(sockfd, sendline, strlen(sendline), 0) < 0) | |
{ | |
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); | |
exit(0); | |
} | |
while(1) | |
{ | |
int sum =0; | |
int isFirstBx = 1; | |
memset(g_bufDetectionRes, 0, MAX_G_AVFRAME_SIZE); | |
/*if (bytesLeft > 0) | |
{ | |
memcpy(g_bufDetectionRes, ) | |
}*/ | |
n = recv(sockfd, g_bufDetectionRes, 1024, 0); | |
if(n == -1) | |
{ | |
perror("sock err -1"); | |
return 0; | |
} | |
sum += n; | |
int pos =0; | |
int nbox; | |
struct nodeBC *pbc = NULL, *head = NULL, *pre = NULL, *next = NULL; | |
memcpy(&nbox, g_bufDetectionRes + pos,sizeof(nbox)); | |
pos += sizeof(nbox); | |
memcpy(&im.c,g_bufDetectionRes + pos, sizeof(im.c)); | |
pos += sizeof(im.c); | |
memcpy(&im.h, g_bufDetectionRes+pos, sizeof(im.h)); | |
pos += sizeof(im.h); | |
memcpy(&im.w, g_bufDetectionRes+pos,sizeof(im.w)); | |
pos += sizeof(im.w); | |
if (isFirstImage == 1) | |
{ | |
im.data = (char *)malloc(im.c * im.h * im.w *sizeof(*im.data)); | |
} | |
for(; nbox > 0; nbox --) | |
{ | |
pbc = (struct nodeBC *)calloc(1, sizeof(struct nodeBC)); | |
memcpy(&pbc->c, g_bufDetectionRes+pos,sizeof(int)); | |
pos += sizeof(pbc->c); | |
memcpy(&pbc->prob, g_bufDetectionRes+pos,sizeof(pbc->prob)); | |
pos += sizeof(pbc->prob); | |
memcpy(&pbc->left, g_bufDetectionRes+pos,sizeof(int)); | |
pos += sizeof(pbc->left); | |
memcpy(&pbc->top, g_bufDetectionRes+pos,sizeof(int)); | |
pos += sizeof(pbc->top); | |
memcpy(&pbc->right, g_bufDetectionRes+pos,sizeof(int)); | |
pos += sizeof(pbc->right); | |
memcpy(&pbc->bot, g_bufDetectionRes+pos,sizeof(int)); | |
pos += sizeof(pbc->bot); | |
if(isFirstBx == 1) | |
{ | |
pre = head = pbc; | |
isFirstBx = 0; | |
} | |
else | |
{ | |
pre->next = pbc; | |
pre = pbc; | |
} | |
} | |
int bytesCopy = n - pos; | |
int headerLength = pos; | |
int sizeImg = im.c*im.h*im.w*sizeof(char); | |
memcpy(im.data + pos - headerLength, g_bufDetectionRes+pos, bytesCopy); | |
pos += bytesCopy; | |
//sum: bytes receviedfrom network | |
while(sum - headerLength < sizeImg) | |
{ | |
n = recv(sockfd, g_bufDetectionRes+pos, sizeImg-(sum-headerLength), 0);//MAX_G_AVFRAME_SIZE - pos, 0); | |
if(n == -1) | |
{ | |
perror("socket error"); | |
return 0; | |
} | |
else if(n == 0) | |
{ | |
usleep(100); | |
} | |
/*if(sum - headerLength + n > sizeImg ) | |
{ | |
bytesCopy = sizeImg - sum; | |
bytesLeft = sum + n - sizeImg; | |
}*/ | |
sum += n; | |
bytesCopy = n; | |
memcpy(im.data + pos - headerLength, g_bufDetectionRes+pos, bytesCopy); | |
pos += bytesCopy; | |
} | |
if(isFirstImage == 1) | |
{ | |
disp= cvCreateImage(cvSize(im.w,im.h), IPL_DEPTH_8U, im.c); | |
isFirstImage = 0; | |
} | |
int i; | |
for(i = 0; i < im.w*im.h; ++i){ | |
char swap = im.data[i]; | |
im.data[i] = im.data[i+im.w*im.h*2]; | |
im.data[i+im.w*im.h*2] = swap; | |
} | |
int step = disp->widthStep; | |
int x, y , k; | |
for(y = 0; y < im.h; ++y){ | |
for(x = 0; x < im.w; ++x){ | |
for(k= 0; k < im.c; ++k){ | |
disp->imageData[y*step + x*im.c + k] = (im.data[k *im.w* im.h + y * im.w + x]);// y * im.w *im.c + x * im.c + k]); | |
} | |
} | |
} | |
for(pbc = head; pbc != NULL; ) | |
{ | |
CvPoint pt1{pbc->left, pbc->top}; | |
CvPoint pt2{pbc->right, pbc->bot}; | |
CvScalar cl; | |
if(pbc->c == 6) | |
{ | |
cl.val[0]=0; | |
cl.val[1]=0; | |
cl.val[2]=0; | |
} | |
else | |
{ | |
cl.val[0]=255; | |
cl.val[1]=255; | |
cl.val[2]=255; | |
} | |
char sprob[10]; | |
sprintf(sprob, "%f", pbc->prob); | |
CvFont font; | |
double hscale = .5; | |
double vscale = .5; | |
int linewidth = 2; | |
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,hscale,vscale,0,linewidth); | |
CvScalar textColor =cvScalar(0,0,0); | |
CvSize text_size; | |
int baseline; | |
cvPutText(disp, class_names[pbc->c], CvPoint{pbc->left + 2, pbc->top - 2}, &font,textColor); | |
cvGetTextSize(class_names[pbc->c], &font, &text_size, &baseline); | |
cvPutText(disp, sprob, CvPoint{pbc->left + 2, pbc->top + text_size.height + 2}, &font,textColor); | |
cvRectangle(disp, pt1, pt2, cl, 2); | |
free(pbc); | |
pbc = pbc->next; | |
} | |
// cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 ) | |
cvShowImage("Result", disp); | |
cvWaitKey(10); | |
} | |
free(im.data); | |
cvReleaseImage(&disp); | |
close(sockfd); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment