Can't figure out if its just bad code or if its the IDE but it doesn't even work on replit

70 Views Asked by At

All im trying to do is get the program to call on a .mp4 file and parse through each frame before translating the frames into ascii art and then repeating the output in the terminal until some key is pressed, spent a month trying to get it to work on my own and after a week of trying to get chatgpt or copilot to fix the bugs I'm all out of ideas. its possible its my system but I've tried the global python on my system, I've tried anaconda and andaconda ran through vs code. it might just be bad code I'm all self taught so...yeah here's the code

import cv2
from ascii_graph import Pyasciigraph

def convert_frame_to_ascii(frame):
    height, width = frame.shape[:2]
    aspect_ratio = width / height
    new_width = 120
    new_height = int(new_width / aspect_ratio / 2)
    resized_frame = cv2.resize(frame, (new_width, new_height))
    ascii_frame = ''
    ascii_graph = Pyasciigraph()

    for row in resized_frame:
        for pixel in row:
            ascii_frame += ascii_graph.graph([pixel[0], pixel[1], pixel[2]])[0][0]
        ascii_frame += '\n'

    return ascii_frame

def play_video(file_path):
    cap = cv2.VideoCapture(file_path)

    while True:
        ret, frame = cap.read()

        if not ret:
            break

        ascii_frame = convert_frame_to_ascii(frame)
        print(ascii_frame)

        if cv2.waitKey(1) == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    video_file = 'path/to/video.mp4'
    play_video(video_file)

and the traceback looks like this or some variation of this

    Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import cv2
  File "/home/runner/vidtoascii/venv/lib/python3.10/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/runner/vidtoascii/venv/lib/python3.10/site-packages/cv2/__init__.py", line 111, in bootstrap
    load_first_config(['config.py'], True)
  File "/home/runner/vidtoascii/venv/lib/python3.10/site-packages/cv2/__init__.py", line 109, in load_first_config
    raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.

I tried swapping different modules and libraries and packages as well as changing interpreters and IDEs, I've tried running it on the cloud, for some reason the issue is getting whatever environment I'm using to download and import the prerequisites.

edit:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import cv2
  File "/home/runner/vidtoascii/venv/lib/python3.10/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/runner/vidtoascii/venv/lib/python3.10/site-packages/cv2/__init__.py", line 111, in bootstrap
    load_first_config(['config.py'], True)
  File "/home/runner/vidtoascii/venv/lib/python3.10/site-packages/cv2/__init__.py", line 109, in load_first_config
    raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
1

There are 1 best solutions below

1
Tim Roberts On

Here is code that works, based on the link I included in the comments above.

import cv2
ASCII_CHARS = ["@", "#", "$", "%", "?", "*", "+", ";", ":", ",", "."]

def resize(image, new_width = 100):
    height, width = image.shape[:2]
    new_height = new_width * height // width
    return cv2.resize(image, (new_width, new_height))

def to_grayscale(image):
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

def pixel_to_ascii(image):
    aimage = []
    for row in image:
        aimage.append( ''.join(ASCII_CHARS[col//25] for col in row) )
    return '\n'.join(aimage)

def play_video(file_path):
    cap = cv2.VideoCapture(file_path)

    while True:
        ret, frame = cap.read()

        if not ret:
            break

        frame = resize(frame)
        frame = to_grayscale(frame)
        ascii = pixel_to_ascii(frame)
        print(ascii)
        print('\n')

        if cv2.waitKey(1) == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    video_file = '/home/timr/Music/Scores/PuttinOnTheRitz.mp4'
    play_video(video_file)