script.cpp 40.8 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
		if (positions2.size() == 1) {
			found = true;
			break;
		}

		std::cout << "Proceeding with negative angles." << endl;
		alg -> setMinAngle(350);
		alg -> setMaxAngle(360);
		alg -> detect(tapeProcessingAreaGrayscale, positions2);

		if (positions2.size() == 1) {
			found = true;
			break;
		}
		
		std::cout << "Increasing position value." << endl;
		oldPosThresh += 10;
		alg -> setPosThresh(oldPosThresh);
	}
	tm.stop();

    std::cout << "Tape detection time : " << tm.getTimeMilli() << " ms" << endl;

	for (int i = 0; i < positions2.size(); i++) {
		Point2f pos2(positions2[i][0], positions2[i][1]);
		scale = positions2[i][2];
		angle = positions2[i][3];

		rect.center = pos2;
		rect.size = Size2f(templateShape.cols * scale, templateShape.rows * scale);
		rect.angle = angle;

		rect.points(pts);

		// Update points with tape processing area coordinates
		pts[0] = Point2f(pts[0].x+tapeProcessingAreaX, pts[0].y+tapeProcessingAreaY);
		pts[1] = Point2f(pts[1].x+tapeProcessingAreaX, pts[1].y+tapeProcessingAreaY);
		pts[2] = Point2f(pts[2].x+tapeProcessingAreaX, pts[2].y+tapeProcessingAreaY);
		pts[3] = Point2f(pts[3].x+tapeProcessingAreaX, pts[3].y+tapeProcessingAreaY);
		// Update rect
		rect = RotatedRect(pts[0], pts[1], pts[2]);


		line(myFrame, pts[0], pts[1], Scalar(0, 255, 0), 2);
		line(myFrame, pts[1], pts[2], Scalar(0, 255, 0), 2);
		line(myFrame, pts[2], pts[3], Scalar(0, 255, 0), 2);
		line(myFrame, pts[3], pts[0], Scalar(0, 255, 0), 2);
	}

	imshow("Tape area(s)", myFrame);
	waitKey();

	return found;
}


int main(int argc, char** argv) {

	/**************************************** CONFIGURATION FILE ****************************************/

	// Read configuration file
	std::ifstream iConfig("../config/config.json");
	iConfig >> configurationFile;
	// Initialise parameters
	try {
		brands = configurationFile["Brands"];
		irregularityFileInputPath = configurationFile["IrregularityFileInput"];
		outputPath = configurationFile["OutputPath"];
		videoPath = configurationFile["PreservationAudioVisualFile"];
		speed = configurationFile["Speed"];
		thresholdPercentual = configurationFile["ThresholdPercentual"];
		thresholdPercentualPinchRoller = configurationFile["ThresholdPercentualPinchRoller"];
	} catch (nlohmann::detail::type_error e) {
		std::cerr << "\033[1;31mconfig.json error!\033[0;31m\n" << e.what() << std::endl;
		return -1;
	}
	// Input JSON check
	std::ifstream iJSON(irregularityFileInputPath);
	if (iJSON.fail()) {
		std::cerr << "\033[1;31mconfig.json error!\033[0;31m\nIrregularityFileInput.json cannot be found or opened."  << std::endl;
		return -1;
	}
	std::string fileName, extension;
    if (findFileName(videoPath, fileName, extension) == -1) {
        std::cerr << "\033[1;31mconfig.json error!\033[0;31m\nThe PreservationAudioVisualFile cannot be found or opened." << std::endl;
        return -1;
    }
	if (speed != 7.5 && speed != 15) {
		std::cerr << "\033[1;31mconfig.json error!\033[0;31m\nSpeed parameter must be 7.5 or 15 ips."  << std::endl;
		return -1;
	}
	if (thresholdPercentual < 0 || thresholdPercentual > 100) {
		std::cerr << "\033[1;31mconfig.json error!\033[0;31m\nThresholdPercentual parameter must be a percentage value."  << std::endl;
		return -1;
	}
	if (thresholdPercentualPinchRoller < 0 || thresholdPercentualPinchRoller > 100) {
		std::cerr << "\033[1;31mconfig.json error!\033[0;31m\nThresholdPercentual parameter must be a percentage value."  << std::endl;
		return -1;
	}

	if (speed == 15)
		thresholdPercentual += 4;

    std::cout << "\nParameters from config.json file:" << std::endl;
	std::cout << "  Brands: " << brands << std::endl;
	std::cout << "  Speed: " << speed << std::endl;
    std::cout << "  ThresholdPercentual: " << thresholdPercentual << std::endl;
	std::cout << "  ThresholdPercentualPinchRoller: " << thresholdPercentualPinchRoller << std::endl;

	// Read input JSON
	iJSON >> irregularityFileInput;

	/**************************************** DEFINE THE PROCESSING AREA ****************************************/

	// bool processingAreaDefined = false;
	// while (!processingAreaDefined) {
	// 	// Reset variables
	// 	savingPinchRoller = false;
	// 	pinchRollerRect = false;
	// 	processingSelection = true;
	// 	rotated = false;
	// 	int defineProcArea = defineProcessingArea();
	// 	if (defineProcArea == -1) {
	// 		std::cout << "Exit." << std::endl;
	// 		return -1;
	// 	} else if (defineProcArea == 1) {
	// 		std::cout << "Modifying the area again." << std::endl;
	// 	} else {
	// 		processingAreaDefined = true;
	// 	}
	// }

	/******************************************* TAPE AREA DETECTION *******************************************/

	cv::VideoCapture videoCapture(videoPath);
    if (!videoCapture.isOpened()) {
        std::cerr << "\033[31m" << "Video unreadable." << std::endl;
        return -1;
    }

	// Get total number of frames
	int totalFrames = videoCapture.get(CAP_PROP_FRAME_COUNT);
	// Set frame position to half video length
	videoCapture.set(CAP_PROP_POS_FRAMES, totalFrames/2);
	// Get frame and show it
	videoCapture >> myFrame;
	
	// Find the processing area corresponding to the tape area over the reading head
	bool found = findProcessingArea(configurationFile);

	// Reset frame position
	videoCapture.set(CAP_PROP_POS_FRAMES, 0);

	/**************************** WRITE USEFUL INFORMATION TO LOG FILE ***************************/

	// Get now time
	std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    std::string ts = std::ctime(&t);
	ofstream myFile;
	myFile.open("log.txt", ios::app);
	myFile << endl << fileName << endl;
	myFile << "tsh: " << thresholdPercentual << "   tshp: " << thresholdPercentualPinchRoller << std::endl;
	myFile << ts; // No endline character for avoiding middle blank line.

	if (found) {
		cout << "Processing area found!" << endl;
		myFile << "Processing area found!" << endl;
		myFile.close();
	} else {
		cout << "Processing area not found. Try changing JSON parameters." << endl;
		myFile << "Processing area not found." << endl;
		myFile.close();
		return 1; // Program terminated early
	}

	/********************************* MAKE REQUIRED DIRECTORIES *********************************/
	
	makeDirectories(fileName, outputPath, brands);

	/**************************************** PROCESSING *****************************************/

	std::cout << "\n\033[32mStarting processing...\033[0m\n" << std::endl;

	// Processing timer
	time_t startTimer, endTimer;
	startTimer = time(NULL);

	processing(videoCapture, fileName);

	endTimer = time(NULL);
	float min = (endTimer - startTimer) / 60;
	float sec = (endTimer - startTimer) % 60;

	std::string result("Processing elapsed time: " + std::to_string((int)min) + ":" + std::to_string((int)sec));
	cout << endl << result << endl;

	myFile.open("log.txt", ios::app);
	myFile << result << std::endl << std::endl;
	myFile.close();

	/**************************************** IRREGULARITY FILES ****************************************/

	std::ofstream outputFile1;
	std::string outputFile1Name = outputPath + "IrregularityFileOutput1.json";
	outputFile1.open(outputFile1Name);
	outputFile1 << irregularityFileOutput1 << std::endl;

	// Irregularities to extract for the AudioAnalyser and to the TapeIrregularityClassifier
	extractIrregularityImagesForAudio(outputPath, videoPath, irregularityFileInput, irregularityFileOutput2);

	std::ofstream outputFile2;
	std::string outputFile2Name = outputPath + "IrregularityFileOutput2.json";
	outputFile2.open(outputFile2Name);
	outputFile2 << irregularityFileOutput2 << std::endl;
	
    return 0;

}
For faster browsing, not all history is shown. View entire blame