Skip to content

Commit

Permalink
adds video-to-ascii-cpp #65
Browse files Browse the repository at this point in the history
  • Loading branch information
mxochicale committed Mar 31, 2024
1 parent 04f8e60 commit 6c84150
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
18 changes: 18 additions & 0 deletions opencv/examples/cpp/video-to-ascii-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.1)
project(main)

#Set Source Code Path
set(SRC_PATH src)
set(${PROJECT_NAME}_SRC
${SRC_PATH}/main.cpp
)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

#Include opencv
find_package(OpenCV 4 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

#Executable
add_executable( ${PROJECT_NAME} ${${PROJECT_NAME}_SRC} )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

14 changes: 14 additions & 0 deletions opencv/examples/cpp/video-to-ascii-cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Video to ASCII Converter (C++)

## Launching app
use Ctrl+- to decrease font size and fit ascii video conversion


## References
* GitHub
* https://github.com/servetgulnaroglu/video-to-ascii-cpp/tree/main
* Varios videos in youtube
* https://www.youtube.com/watch?v=_JIctc1wmCw
* https://www.youtube.com/watch?v=3N7fRURLz4A&t=94s
* https://www.youtube.com/watch?v=zHxgUFYwoRQ

72 changes: 72 additions & 0 deletions opencv/examples/cpp/video-to-ascii-cpp/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <opencv2/opencv.hpp>

#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
using namespace cv;

#define VID_WIDTH 640
#define VID_HEIGHT 480
#define VIDEO_IN "/dev/video0"


string pixelToASCII(int pixel_intensity) {
//const string ASCII_CHARS = "@$%#&!*+=-_. ";
//const string ASCII_CHARS = "@#&!*+=-_. ";
const string ASCII_CHARS = " ._-=+*!&#%$@";
string s =
string(1, ASCII_CHARS[pixel_intensity * ASCII_CHARS.length() / 256]);
return s;
}

int main() {
VideoCapture cap(VIDEO_IN);

double fps = cap.get(CAP_PROP_FPS);

cout << fps << endl;

//int frame_duration_ms = 10000 / fps;
//int frame_duration_ms = 1000 / fps; //DEFAULT
int frame_duration_ms = 100 / fps;
//int frame_duration_ms = 10 / fps;
//int frame_duration_ms = 1 / fps;

int width = 250;
int height = 50;

int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
cout << frame_width << " " << frame_height << endl;
// 4096 2160
// 751 × 944

height = (width * frame_height / frame_width) * 0.4194;

Mat frame, gray_frame, resized_frame;

while (true) {
cap >> frame;
if (frame.empty())
break;

cv::cvtColor(frame, gray_frame, cv::COLOR_BGR2GRAY);

resize(gray_frame, resized_frame, Size(width, height), 0, 0, INTER_LINEAR);

string ascii_frame;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
ascii_frame += pixelToASCII(resized_frame.at<uchar>(i, j));
}
ascii_frame += "\n";
}

system("clear"); // to clear the console
cout << ascii_frame;
std::this_thread::sleep_for(std::chrono::milliseconds(frame_duration_ms));
}

return 0;
}

0 comments on commit 6c84150

Please sign in to comment.