Untitled
raw download clone
TEXT
views 93
,
size 7845 b
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <fstream>
#include <ctime>
#include <queue>
#include <thread>
#include <mutex>
#include <exception>
#include "sys/sysinfo.h"

#include "VideoReader.h"
#include "PlateReader.h"
#include "PlateRecognizator.h"
#include "PlateRecognitionAction.h"
#include "Onvif_controller.h"
#include "Onvif.h"
#include "xmlParser.h"
#include "publicValue.h"

using namespace pr;
using namespace cv;
using namespace std;

vector<double> endgrid, divgrid, l1, v1, v2;
double scale;
extern int video_width, video_height;
// int MaxKindsPlate;
// int MaxPlateProcessCNN;

queue <FrameData> frameQueue;//Frames from Video Streaming
mutex frameQueueMutex;

//Mode: kinds plate
//0: While square
//1: While long
//2: BlueRed square
//3: BlueRed long
cv::Rect cropRect;

QueuePlates plateQueue;
queue<PlateData> plateQueue_showFrame;
mutex plateQueue_showFrameMutex;
deque <image_details> OnvifDetailsQueue;

pr::ObjectUtils objects;

void showFrame(string linkFileConfig,int cropWidth, int cropHeight)
{
	int FrameIgnored2=atoi(IOData::GetCongfigData(linkFileConfig,"FrameIgnored:").c_str());
	int fps = atoi(IOData::GetCongfigData(linkFileConfig,"fps_video_reading:").c_str())+1;
		
	int nframe = 0;
	auto start = CLOCK_NOW();
	int delay = 1000/fps*FrameIgnored2;
	double displayScale((float)cropWidth/(float)cropHeight);
	while (true)
	{
		this_thread::sleep_for(chrono::milliseconds(1));
		auto startime = CLOCK_NOW();
		if (plateQueue_showFrame.empty())
			continue;
		plateQueue_showFrameMutex.lock();
		PlateData data = plateQueue_showFrame.front();
		plateQueue_showFrame.pop();
		plateQueue_showFrameMutex.unlock();

		cv::Mat frame = data.frame.clone();
		cv::resize(frame, frame, cv::Size(), 0.4, 0.4);
		cv::imshow("Plate1", frame);
		cv::waitKey(1);
		nframe++;
		auto end = CLOCK_NOW();
		ElapsedTime elapsed = end - start;
		if (nframe % 100 == 0)
		{
			//cout << " display fps=" << nframe / elapsed.count() << endl;
		}
		while (1)
		{
			auto endtime = CLOCK_NOW();
			ElapsedTime elapsedtime = endtime - startime;
			if (elapsedtime.count()*1000 < delay)
				this_thread::sleep_for(chrono::milliseconds(1));
			else break;
		}
	}
}

void checkave(string linkFileConfig)
{
    long n=0,n_bu=0,the_same=0;
	int num_of_kill=0;
	objects.runave(linkFileConfig);
    while (1)
    {
        usleep(3000000);
        ifstream fi("logrun.txt");
        if (fi.is_open())
        {
            fi >> n;
            if (n!=n_bu)
                the_same=0;
            else the_same++;
            n_bu=n;
            fi.close();
        }
        if (the_same>=10)
        {
            the_same=0;
			num_of_kill++;
			if (num_of_kill==10)
			{
				system("sudo killall -s SIGINT loop_run");
				objects.update_rtsplink(linkFileConfig);
				system("sudo reboot");
			}
			else
			{	
				system("sudo killall -s SIGINT loop_run");
				objects.runave(linkFileConfig);
			}
        }
        cout << "is running..\t "<<n<<endl;
    }
}

void get_mem_total(string linkFileConfig) {
	string logfolder = IOData::GetCongfigData(linkFileConfig,"logfolder:").c_str();
	while(1)
	{
		this_thread::sleep_for(chrono::milliseconds(3000));
		string date=objects.getCurrentDateTime_pushSQL().substr(0,10);
		std::string token;
		std::ifstream file("/proc/meminfo");
		string s;
		while(file >> token) {
			if(token == "MemAvailable:") {
				unsigned long mem;
				if(file >> mem) {
					cout << "MemAvailable: "<<mem/1024<<" Mb"<<endl;
					//write log			
            		objects.wtfile_demo(logfolder+date+".txt",objects.getCurrentDateTime_pushSQL()+"\t"+ "MemAvailable: "+to_string(mem/1024)+" Mb");
					if (mem/1024<150)
						system("sudo reboot");
				}
			}
			// ignore rest of the line
			file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
		objects.wtfile_demo(logfolder+date+".txt",s);
	}
}

int main(int argc, char** argv)
// int main()
{
	// string linkFileConfig = argv[1];
	//string linkFileConfig1 = "dos2unix "+linkFileConfig;
	//const char *command1 = linkFileConfig1.c_str();
	//system(command1);
	string linkFileConfig = "../data/Config.txt";
	cout << linkFileConfig << endl;

	int cropX = atoi(IOData::GetCongfigData(linkFileConfig,"cropX:").c_str());
	int cropY = atoi(IOData::GetCongfigData(linkFileConfig,"cropY:").c_str());
	int cropWidth = atoi(IOData::GetCongfigData(linkFileConfig,"cropWidth:").c_str());
	int cropHeight = atoi(IOData::GetCongfigData(linkFileConfig,"cropHeight:").c_str());
	int showVideo = atoi(IOData::GetCongfigData(linkFileConfig,"showVideo:").c_str());
	int runonvif = atoi(IOData::GetCongfigData(linkFileConfig,"runonvif:").c_str());
	string kindurl = IOData::GetCongfigData(linkFileConfig,"urlkind:").c_str();

	cropRect = cv::Rect(cropX, cropY, cropWidth, cropHeight);

	//-------------------Huu Ton Test-----------------------------------------
	//string IPCameraAddress = IOData::GetCongfigData("IPCameraAddress:").c_str();
	//OnvifController onvifController(IPCameraAddress+":2000");
	// //OnvifController onvifController("10.12.11.149:8999"); // Cam a Tuc
	//int current_exposure=0;
	//int count=0;
	//onvifController.setExposureTime(1000,"VideoSource0");
	//------------------------------------------------------------------------
	VideoReader VideoReader(IOData::GetLinkURL(linkFileConfig),
							atoi(IOData::GetCongfigData(linkFileConfig,"video_width:").c_str()),
							atoi(IOData::GetCongfigData(linkFileConfig,"video_height:").c_str()),
							cropRect,
							frameQueue,
							linkFileConfig);

	PlateReader plateReader(
		cropRect,
		frameQueue,
		frameQueueMutex,
		plateQueue,
		OnvifDetailsQueue,
		plateQueue_showFrame,
		plateQueue_showFrameMutex,
		linkFileConfig,
		showVideo);
	
	PlateRecognitionAction PlateRecognitionAction(
			cropRect,
			plateQueue,
			linkFileConfig);
	
	Onvif_controller Onvif_controller(
			OnvifDetailsQueue,
			linkFileConfig);

	// Code de tam, se viet lai (cua em Bach) ===========================
	int base_video_width = 1280;
	int base_video_height = 960;

	int cur_video_width = atoi(IOData::GetCongfigData(linkFileConfig,"video_width:").c_str());
	int cur_video_height = atoi(IOData::GetCongfigData(linkFileConfig,"video_height:").c_str());

	cout << cur_video_width << " " << cur_video_height << endl;

	double scale_video_x = (float)cur_video_width/base_video_width;
	double scale_video_y = (float)cur_video_height/base_video_height;

	scale = atof(IOData::GetCongfigData(linkFileConfig,"scale_speed:").c_str());
	endgrid.push_back(0*scale_video_x);
	endgrid.push_back(960*scale_video_y);
	divgrid.push_back(1533*scale_video_x);
	divgrid.push_back(-264.147*scale_video_y);
	l1.push_back(960*scale_video_y);
	l1.push_back(-0.79853*scale_video_y/scale_video_x);
	v1.push_back(1633*scale_video_x);
	v1.push_back(-344*scale_video_y);
	v2.push_back(12099*scale_video_x);
	v2.push_back(300*scale_video_y);
	// ======================================================================

	std::vector<std::thread> ths;
	ths.push_back(std::thread(VideoReader));
	ths.push_back(std::thread(plateReader));
	if (runonvif)
		ths.push_back(std::thread(Onvif_controller));
	ths.push_back(std::thread(PlateRecognitionAction));
	if (showVideo)
		ths.push_back(std::thread(showFrame,linkFileConfig,atoi(IOData::GetCongfigData(linkFileConfig,"video_width:").c_str()),atoi(IOData::GetCongfigData(linkFileConfig,"video_height:").c_str())));
	ths.push_back(std::thread(get_mem_total,linkFileConfig));
	if (kindurl=="ave")
	{
		ths.push_back(std::thread(checkave,linkFileConfig));
	}
	for (auto &t : ths)
		t.join();
}
close fullscreen
Login or Register to edit or fork this paste. It's free.