victorjoh / hard-subs-to-srt

Creates an SRT file from a video file that has hardcoded subtitles
MIT License
23 stars 2 forks source link

cv2.error: OpenCV #1

Open Milor123 opened 1 year ago

Milor123 commented 1 year ago

I tried use it in MKV, AVI, MP4, but the results is the same. I am working in windows 10 over Python 3.11 and have installed the https://github.com/UB-Mannheim/tesseract/

image

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

What should i do? Thank u very much !!

victorjoh commented 1 year ago

Wow! an issue, this is a first for me :)

From the error message and reading the related code, it seems that cropped_frame is empty when sent into:

img = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2GRAY)

My guess for why this happens for you is that hard-subs-to-srt looks for subtitles outside of the video frame resulting in an empty array. You could try to change the subtitle bounds in the script:

SUBTITLE_BOUNDS_LEFT = 820
SUBTITLE_BOUNDS_RIGHT = 3020
SUBTITLE_BOUNDS_TOP = 1600
SUBTITLE_BOUNDS_BOTTOM = 1863

When I change the bounds to be way off like so:

SUBTITLE_BOUNDS_LEFT = 8200
SUBTITLE_BOUNDS_RIGHT = 30200
SUBTITLE_BOUNDS_TOP = 16000
SUBTITLE_BOUNDS_BOTTOM = 18630

I get the same error:

PS C:\Users\Victor\Programming\hard-subs-to-srt> pipenv run python .\minning-town.py
Traceback (most recent call last):
  File "C:\Users\Victor\Programming\hard-subs-to-srt\minning-town.py", line 4, in <module>
    hard_subs_to_srt.extract_srt(Video_FILE, 'Minning Town S01E01 chn.srt')
  File "C:\Users\Victor\Programming\hard-subs-to-srt\hard_subs_to_srt.py", line 89, in extract_srt
    convert_frames_to_srt(video, FIRST_FRAME)
  File "C:\Users\Victor\Programming\hard-subs-to-srt\hard_subs_to_srt.py", line 129, in convert_frames_to_srt
    monochrome_frame = to_monochrome_subtitle_frame(cropped_frame)
  File "C:\Users\Victor\Programming\hard-subs-to-srt\hard_subs_to_srt.py", line 248, in to_monochrome_subtitle_frame
    img = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Please let me know if changing the bounds works for you.

yuvgin commented 1 year ago

I'm getting the same error. Was not able to find bounds that make the error not appear.

victorjoh commented 1 year ago

@yuvgin, it should work if the subtitle bounds are within your video resolution. So if your video has the resolution 1920 x 800 pixels, then for example the bounds:

SUBTITLE_BOUNDS_LEFT = 400
SUBTITLE_BOUNDS_RIGHT = 1500
SUBTITLE_BOUNDS_TOP = 500
SUBTITLE_BOUNDS_BOTTOM = 700

should work.

What is the resolution of you video and what bounds did you try?

Levine91 commented 1 year ago

I would like to add that I too am having the exact same error as the other users. MP4 video file is 640x480. All dependencies are completely up to date. Video file is in the same directory too for what it's worth.

I tried changing PREVIEW_MAX_SIZE = (1280, 720) over to PREVIEW_MAX_SIZE = (640, 480)

Before that I was trying various different subtitle bounds as I'm confused by what these numbers actually are representing the subtitle box/how they are being measured:

SUBTITLE_BOUNDS_LEFT = 300
SUBTITLE_BOUNDS_RIGHT = 300
SUBTITLE_BOUNDS_TOP = 300
SUBTITLE_BOUNDS_BOTTOM = 300
SUBTITLE_BOUNDS_LEFT = 400
SUBTITLE_BOUNDS_RIGHT = 400
SUBTITLE_BOUNDS_TOP = 400
SUBTITLE_BOUNDS_BOTTOM = 400
SUBTITLE_BOUNDS_LEFT = 400
SUBTITLE_BOUNDS_RIGHT = 500
SUBTITLE_BOUNDS_TOP = 500
SUBTITLE_BOUNDS_BOTTOM = 400
SUBTITLE_BOUNDS_LEFT = 820
SUBTITLE_BOUNDS_RIGHT = 3020
SUBTITLE_BOUNDS_TOP = 1600
SUBTITLE_BOUNDS_BOTTOM = 1863

image

cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

victorjoh commented 1 year ago

The subtitles bounds are the coordinates used to crop the frame before sending it to Tesseract. We want the frame sent to Tesseract to contain only the subtitles, the rest of the frame is just noise.

Coordinates are measured from the top left corner of the frame. To get a sense of what the numbers mean you can try the values below. They will probably not be correct but you should be able to see the frames.

SUBTITLE_BOUNDS_LEFT = 100
SUBTITLE_BOUNDS_RIGHT = 300
SUBTITLE_BOUNDS_TOP = 50
SUBTITLE_BOUNDS_BOTTOM = 200

Since your video is 640x480, left and right must be between 0 and 640, and left must be less than right. Top and bottom must be between 0 and 480, and top must be less than bottom.

lovinuedm commented 1 year ago

I have the same error but it seems different ??? Please help error