int main(int, char**) { //create Background Subtractor objects pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach // Load the cascades if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; }; int res = process_video(); return 0; }
// Video thread void Scheduler::video_thread_fn() { auto vid = GL::shared(file->video().width, file->video().height, file->video().aspect_ratio, file->video().ctx->pix_fmt); video = vid; auto event = GLEvent::shared(); if (file->sub().active) sub_renderer = ASSRenderer::shared(file->sub().fonts, file->sub().ass_data, file->video().width, file->video().height); AVFrame *frame = avcodec_alloc_frame(); // Add event handler for GL. add_event_handler(event); while (video_thread_active && vid_pkt_queue.alive()) { event->poll(); avlock.lock(); if (vid_pkt_queue.size() > 0 && !is_paused) { auto pkt = vid_pkt_queue.pull(); avlock.unlock(); process_video(pkt.get(), vid, frame); if (file->sub().active) process_subtitle(vid); // We have to calculate how long we should wait before swapping frame to screen. // We sync everything to audio clock. double delta = get_time(); avlock.lock(); delta -= audio_pts_ts; double sleep_time = video_pts - (audio_pts + delta); avlock.unlock(); // Yes, it can happen! :( if (delta < 0.0) delta = 0.0; //std::cout << "Delta: " << delta << std::endl; if (video_pts > (audio_pts + delta) && audio_thread_active) { double last_frame_delta = get_time(); last_frame_delta -= video_pts_ts; // :( if (last_frame_delta < 0.0) last_frame_delta = 0.0; // We try to keep the sleep time to a somewhat small value to avoid choppy video in some cases. // Max sleep time should be a bit over 1 frame time to allow audio to catch up. double max_sleep = 1.2 * frame_time() - last_frame_delta; if (max_sleep < 0.0) max_sleep = 0.0; if (sleep_time > max_sleep) { sleep_time = max_sleep; } //std::cout << "Sleep for " << sleep_time << std::endl; sync_sleep(sleep_time); } video_pts_ts = get_time(); vid->flip(); } else { avlock.unlock(); if (is_paused) sync_sleep(0.01); else { // Having some race conditions... quickfix it for now. vid_pkt_queue.wait(); //sync_sleep(0.01); //vid_pkt_queue.signal(); } } } video_thread_active = false; av_free(frame); }
static void mainloop (void) { int i,j; unsigned char processedPixels[FRAME_HEIGHT][FRAME_WIDTH]; for (;;) { fd_set fds; struct timeval tv; int r; FD_ZERO (&fds); FD_SET (fd, &fds); /* Timeout. */ tv.tv_sec = 2; tv.tv_usec = 0; r = select (fd + 1, &fds, NULL, NULL, &tv); if (-1 == r) { if (EINTR == errno) continue; errno_exit ("select"); } if (0 == r) { fprintf (stderr, "select timeout\n"); exit (EXIT_FAILURE); } if (read_frame ()) { // process the video process_video(pixels, processedPixels); // convert grayscale processed pixels to an RGB format convert_grayscale_to_rgb(pixels, processedPixels,FRAME_WIDTH, FRAME_HEIGHT); // process blobs int labels[FRAME_HEIGHT][FRAME_WIDTH]; for(i = 0; i < FRAME_HEIGHT; i++) { for(j = 0; j < FRAME_WIDTH; j++) { labels[i][j] = -1; } } int numBlobs = two_pass(processedPixels, labels, FRAME_WIDTH, FRAME_HEIGHT); blob blobs[MAX_BLOBS]; extract_blobs(blobs, numBlobs, labels, FRAME_WIDTH, FRAME_HEIGHT); // remove too small/big blobs numBlobs = apply_blob_size_heuristic(blobs, numBlobs); // get all center points for blobs coord centerCoords[MAX_BLOBS];// = (coord*)malloc(sizeof(coord) * numBlobs); get_blob_centers(blobs, numBlobs, centerCoords); collinear points[MAX_BLOBS]; int shortSegments = get_more_straight_sides(centerCoords, numBlobs, points); coord shapeCoords[5]; int shapeFound = 0; for(i = 0; i < shortSegments; i++) { coord p1 = points[i].point1; coord p2 = points[i].point2; coord p3 = points[i].point3; double m1 = fabs(distance(p1, p2)); double m2 = fabs(distance(p2, p3)); double m3 = fabs(distance(p1, p3)); if(is_long_side(points[i])) { collinear shortSide; int shortSideFound = get_short_side(centerCoords, numBlobs, points[i], &shortSide); if(shortSideFound) { draw_collinear(pixels, shortSide); draw_collinear(pixels, points[i]); /*print_point(p1); print_point(p2); print_point(p3); */ //printf(" -------------------- SHORT SIDE \n"); /* print_point(shortSide.point1); print_point(shortSide.point2); print_point(shortSide.point3);*/ printf("\n"); shapeCoords[0] = p1; shapeCoords[1] = p2; shapeCoords[2] = p3; shapeCoords[3] = shortSide.point1; shapeCoords[4] = shortSide.point3; shapeFound = 1; break; } } else if(is_short_side(points[i])) { collinear longSide; int longSideFound = get_long_side(centerCoords, numBlobs, points[i], &longSide); if(longSideFound) { draw_collinear(pixels, longSide); draw_collinear(pixels, points[i]); /* print_point(p1); print_point(p2); print_point(p3); */ //printf(" ======================== LONG SIDE\n"); /* print_point(longSide.point1); print_point(longSide.point2); print_point(longSide.point3); */ printf("\n"); shapeCoords[0] = p1; shapeCoords[1] = p2; shapeCoords[2] = p3; shapeCoords[3] = longSide.point1; shapeCoords[4] = longSide.point3; shapeFound = 1; break; } } } if(shapeFound) { int imageCenterX = FRAME_WIDTH / 2; int imageCenterY = FRAME_HEIGHT / 2; coord center; center.x = imageCenterX; center.y = imageCenterY; double distance = distance_from_center(shapeCoords[1]); double angle = quadrant_angle(shapeCoords[1]); if(distance > 100) { printf("MOVE TOWARDS CENTER :: ANGLE : %f :: DISTANCE : %f(pixels)\n", angle, distance); //print_direction(angle); } else { printf("MOVE DOWN HEIGHT : ?\n"); } print_image(centerCoords, numBlobs, shapeCoords, 5); } free_blobs(blobs, numBlobs); frame = create_cam_img(pixels, FRAME_WIDTH, FRAME_HEIGHT); apply_surface(0, 0, frame, screen); SDL_Flip(screen); } } }