-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
325 lines (276 loc) · 8.58 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <opencv2/opencv.hpp>
#include <iostream>
#include "opencv2/core/utils/logger.hpp"
#include <stdint.h>
#include <bits/stdc++.h>
#include <Windows.h>
#include <fstream>
#include <WinUser.h>
#include <filesystem>
#include <stdio.h>
using namespace cv;
using namespace std;
void ClearScreen()
{
// Get the Win32 handle representing standard output.
// This generally only has to be done once, so we make it static.
static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD topLeft = { 0, 0 };
// std::cout uses a buffer to batch writes to the underlying console.
// We need to flush that to the console because we're circumventing
// std::cout entirely; after we clear the console, we don't want
// stale buffered text to randomly be written out.
std::cout.flush();
// Figure out the current width and height of the console window
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
// TODO: Handle failure!
abort();
}
DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
DWORD written;
// Flood-fill the console with spaces to clear it
FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
// Reset the attributes of every character to the default.
// This clears all background colour formatting, if any.
FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
// Move the cursor back to the top left for the next sequence of writes
SetConsoleCursorPosition(hOut, topLeft);
}
void MaximizeWindow()
{
HWND hWnd;
SetConsoleTitle(L"ASCII Video Animation");
hWnd = FindWindow(NULL, L"ASCII Video Animation");
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD NewSBSize = GetLargestConsoleWindowSize(hOut);
SMALL_RECT DisplayArea = { 0, 0, 0, 0 };
SetConsoleScreenBufferSize(hOut, NewSBSize);
DisplayArea.Right = NewSBSize.X - 1;
DisplayArea.Bottom = NewSBSize.Y - 1;
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
ShowWindow(hWnd, SW_MAXIMIZE);
}
void ChangeConsoleSize(int size)
{
CONSOLE_FONT_INFOEX cfi{};
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = size;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
wcscpy_s(cfi.FaceName, L"Consolas");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
}
//Read the given dark values for the ascii characters
void LoadGreyValues(vector<int>& GreyValues) {
ifstream ifile;
ifile.open("ASCII VALUES/Values.txt");
for (int i = 0; i < (126 - 33 + 1); i++) {
int x;
ifile >> x >> x;
GreyValues[i] = x;
}
ifile.close();
}
//Takes in an image and converts it into a string
string ConvertToText(Mat& frame,vector<int>& GreyValues) {
string Converted = "";
for (int r = 0; r < frame.rows; r++)
{
for (int c = 0; c < frame.cols; c++)
{
int color = frame.at<uint8_t>(r, c);
if (color == 0) {
Converted += " ";
continue;
}
int ci = 0;
for (int a = 34; a <= 126; a++) {
if (abs(GreyValues[ci] - color) > abs(GreyValues[a - 33] - color))
ci = a - 33;
}
Converted += (char)(ci + 33);
}
Converted += '\n';
}
Converted.resize(Converted.size() - 1);
return Converted;
}
//Get text from ConvertToText then write it to a txt file
void ProcessFrame(Mat& frame,int frameNumber,vector<int>& GreyValues,int w_width,int w_height) {
double height_ratio, width_ratio;
height_ratio = (double)w_height / frame.size().height;
width_ratio = (double)w_width / frame.size().width;
Mat frameResized, frameBW, frameC;
resize(frame, frameResized, Size(), width_ratio, height_ratio);
cvtColor(frameResized, frameBW, COLOR_BGR2GRAY);
frameBW.convertTo(frameC, -1, 1, 0);
string Converted = ConvertToText(frameC,GreyValues);
ofstream ofile;
ofile.open("DisplayFiles/" + to_string(frameNumber) + ".txt");
ofile << Converted;
ofile.close();
}
void DisplayVideo(int numberofFrames,int frameRate,int size) {
ChangeConsoleSize(size);
MaximizeWindow();
ClearScreen();
int FPS = frameRate;
auto time_between_frames = chrono::microseconds(chrono::seconds(1)) / FPS;
auto target_tp = chrono::steady_clock::now();
for (int i = 1; i <= numberofFrames; i++)
{
ifstream ifile("DisplayFiles/" + to_string(i) + ".txt");
stringstream stream;
stream << ifile.rdbuf();
ifile.close();
if (size == 6)
Sleep(10);
else if (size == 8)
Sleep(2);
printf("\r");
printf("%s", stream.str().c_str());
printf("\n");
target_tp += time_between_frames;
this_thread::sleep_until(target_tp);
}
}
// Call
// ProcessFrame for each frame in the video
// DisplayVideo at the end of processing
void ProcessVideo(string videoName,int size) {
ChangeConsoleSize(size);
//ClearScreen();
MaximizeWindow();
vector<int> GreyValues(94, 0); //94 different characters
LoadGreyValues(GreyValues); //gets values from text file
VideoCapture cap(videoName);
int frameRate = cap.get(CAP_PROP_FPS);
int numberofFrames = cap.get(CAP_PROP_FRAME_COUNT);
Mat frame;
string progressbar = "";
string emptybar = "----------";
cout << "Do you want to process the video from the beginning ? (Y/N): ";
string ans;
cin >> ans;
if (ans != "Y" && std::filesystem::is_empty("DisplayFiles/")) {
cout << "Empty Directory Detected: Start processing video again...\n";
ans = "Y";
}
if (ans == "Y") {
std::filesystem::remove_all("DisplayFiles/");
Sleep(1000);
std::filesystem::create_directory("DisplayFiles/");
cout << "Enter size (6 best quality,8,10,12,16 worst quality): ";
cin >> size;
ofstream ofile("DisplayFiles/info.txt");
ofile << numberofFrames<<" "<<frameRate<<" " << size;
ofile.close();
ChangeConsoleSize(size);
MaximizeWindow();
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
int w_width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
int w_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
ChangeConsoleSize(16);
for (int i = 1; i <= numberofFrames; i++)
{
cap.read(frame);
ProcessFrame(frame, i, GreyValues,w_width,w_height);
if (i % (numberofFrames / 10) == 0) {
progressbar += "#";
if (!emptybar.empty())
emptybar.resize(emptybar.size() - 1);
}
ClearScreen();
cout << "Processing frame number " << i << " of " << numberofFrames << " :";
cout << "[" << progressbar << emptybar << "]\n";
}
}
else {
ifstream ifile("DisplayFiles/info.txt");
ifile >> numberofFrames>>frameRate>>size;
ifile.close();
}
cout << "Video ready.\n"
<< "Play? (Enter): ";
cin.ignore();
cin.ignore();
DisplayVideo(numberofFrames,frameRate,size);
}
void main() {
cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT);
int size = 16;
system("Color F0");
ChangeConsoleSize(size);
ClearScreen();
MaximizeWindow();
string ans="D";
string videoName;
int numberofFrames,frameRate;
if (std::filesystem::is_directory("DisplayFiles/") && !std::filesystem::is_empty("DisplayFiles/")) {
cout << "Found previously processed video.\n" <<
"Do you want to play processed video? (Y/N):";
cin >> ans;
if (ans == "Y") {
ifstream ifile("DisplayFiles/info.txt");
if (ifile.is_open()) {
ifile >> numberofFrames >> frameRate >> size;
ifile.close();
if (std::filesystem::is_directory("DisplayFiles/" + to_string(numberofFrames) + ".txt")) {
do {
DisplayVideo(numberofFrames, frameRate, size);
ChangeConsoleSize(16);
ClearScreen();
MaximizeWindow();
cout << "Play video again or Delete files (and exit) or Exit only ? (P/D/E): ";
cin >> ans;
} while (ans == "P");
if (ans == "D") {
std::filesystem::remove_all("DisplayFiles/");
std::filesystem::create_directory("DisplayFiles/");
}
else
ans = "Y";
}
else {
cout << "Error: DisplayFiles not complete.\n";
ans = "N";
}
}
else {
cout << "Error: DisplayFiles not complete.\n";
ans = "N";
}
}
}
if(ans!="Y") {
std::filesystem::remove_all("DisplayFiles/");
std::filesystem::create_directory("DisplayFiles/");
cout << "Enter video file name : ";
cin >> videoName;
if (!std::filesystem::is_empty("DisplayFiles/")) {
cout << "Clear DisplayFiles directory? (Y/N): ";
cin >> ans;
if (ans == "Y") {
std::filesystem::remove_all("DisplayFiles/");
std::filesystem::create_directory("DisplayFiles/");
}
}
else
std::filesystem::create_directory("DisplayFiles/");
do {
ProcessVideo(videoName, size);
ChangeConsoleSize(16);
ClearScreen();
cout << "Play video again or Delete files (and exit) or Exit only ? (P/D/E): ";
cin >> ans;
} while (ans == "P");
if (ans == "D") {
std::filesystem::remove_all("DisplayFiles/");
std::filesystem::create_directory("DisplayFiles/");
}
}
}