-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04f8e60
commit 6c84150
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} ) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |