cxFreeze与OpenCV有关 - 视频无法播放

时间:2018-06-05 12:26:03

标签: python opencv cx-freeze cv2

当我使用cx_Frezze为下面的OpenCV代码创建exe时,它按预期工作:

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()

但是,当我尝试用下面的简单代码做同样的事情时,视频不会显示。基本上没有任何反应。没有错误信息,没有崩溃......没有。但是,没有视频。

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

setup.py代码对于两者都是相同的,除了文件名。

1 个答案:

答案 0 :(得分:0)

print语句添加到代码中以进行调试:

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

如果未打开VideoCapture或框架为空,请检查this answer