0

When I use cx_Frezze to create an exe for the below OpenCV code it works as expected:

import cv2
import numpy as np

# mouse callback function
def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),100,(255,50,70),-1)

# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

But, when I try to do the same thing with the simple code below, the video is not displayed. Basically, nothing happens. No error message, no crash... nothing. But, no video.

import numpy as np
import cv2
try:
    cap = cv2.VideoCapture('some_video.wmv')

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

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cv2.imshow('frame', gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
except:
    pass

The setup.py code is the same for both, except for the file name.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48

1 Answers1

0

Add print statements to your code for debugging:

import numpy as np
import cv2
try:
    cap = cv2.VideoCapture('some_video.wmv')
    if not cap.isOpened():
        print('VideoCapture not opened')
        exit(1)

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

        if not ret:
            print('frame is empty')
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cv2.imshow('frame', gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
except:
    pass

If VideoCapture is not opened or frame is empty then check this answer.

zindarod
  • 6,328
  • 3
  • 30
  • 58
  • Thanks for your answer, I appreciate it. I checked, as you suggested; it seems VideoCapture not opened. I check your other answer, and FFMPEG is Yes. My concern is that when I run the python file this code works as expected. I only have the problem after I create the exe file with cx_Freeze. Usually, when cx_Freeze is missing some files, it will let me know, so if FFMPEG was missing , I would get an error. But, I am not getting any error. – Joe T. Boka Jun 05 '18 at 13:28
  • @JoeT.Boka Use [dependency walker](https://stackoverflow.com/a/1993677/2286337) to find out to which libraries the executable is linking. – zindarod Jun 05 '18 at 13:49