Python视频处理
我已经编写了一个代码,使用两个线程在两个不同的帧中播放视频。代码看起来不错,但是cv2.imshow方法不显示正在播放的视频。视频播放器会立即打开和关闭。 解决此问题时需要帮助Python视频处理,python,multithreading,video-processing,Python,Multithreading,Video Processing,我已经编写了一个代码,使用两个线程在两个不同的帧中播放视频。代码看起来不错,但是cv2.imshow方法不显示正在播放的视频。视频播放器会立即打开和关闭。 解决此问题时需要帮助 import threading import cv2 def print_cube(num): """ function to print cube of given num """ print("start") print(num) cv2.imshow("
import threading
import cv2
def print_cube(num):
"""
function to print cube of given num
"""
print("start")
print(num)
cv2.imshow("video1",num)
print("end")
cv2.destroyAllWindows()
def print_square(num):
"""
function to print square of given num
"""
print("start1")
print(num)
cv2.imshow("video2",num)
print("end2")
if __name__ == "__main__":
# creating thread
cap = cv2.VideoCapture("C:\\Users\shakarna\Desktop\video.mp4")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
t1 = threading.Thread(target=print_square, args=(frame,))
t2 = threading.Thread(target=print_cube, args=(gray,))
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# wait until thread 1 is completely executed
t1.join()
# wait until thread 2 is completely executed
t2.join()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
问题可能是您的代码找不到视频。要查看这是否是问题所在,请将mp4文件与代码放在同一文件夹中,然后 cap=cv2.VideoCapture(“C:\Users\shakarna\Desktop\video.mp4”) 应该是
cap=cv2.VideoCapture(“video.mp4”)我按你说的那样试过了,但不起作用。当我在控制台中打印图像时,我可以看到视频矩阵预期的结果是什么?要播放视频但将其显示为灰度?要同时播放两个视频,我尚未测试您的代码,但问题可能是
cv2.destroyAllWindows()
,或者您正在使用While循环。尝试删除所有cv2。首先销毁…。