线程未在 python 中打开目标函数
Thread is not opening the target function in python
我想通过调用 cv2.imshow() 方法打开一个在屏幕上显示正在加载视频的功能。
首先我想演示一下代码,然后再演示问题。
import cv2
import threading
def Load():
video = cv2.VideoCapture('Loading.mov')
if video.isOpened() == True:
cv2.namedWindow("The Video")
cv2.moveWindow("The Video", 500,200)
elif video.isOpened() == False:
print('No Data For Loading Video')
return 0
while video.isOpened():
_, frame = video.read()
if _ == True:
cv2.imshow("The Video",frame)
if cv2.waitKey(10) & 0xff == 27:
break
if _ == False :
break
cv2.destroyAllWindows()
video.release()
t = threading.Thread(target = Load)
t.start()
现在,问题:
当我第一次调用 t.start() 时,线程启动并正确显示视频。
循环中断后,如果我再次尝试创建一个新的 t
作为线程并再次 .start() 它,它根本不会显示任何内容!不是错误,什么都没有!
I am using spyder to re run the codes.
And I want to re run the video whenever I needed .
现在,问题出在哪里?
我用 mp4-Video 试过你的代码(http://techslides.com/demos/sample-videos/small.mp4) and it works. I converted said video to mov with https://video.online-convert.com/convert-to-mov 它仍然有效...
虽然我可能有一个有根据的猜测:
您应该尝试使用 cv2 的新实例进行每次调用。
我假设问题可能是,线程的第二次调用继承了第一次调用的状态(尤其是 cv2 的内部状态),因为它只是一个函数,因此视频处于状态 "already played" 之类的东西,不再显示任何内容。
因此,如果您将所有内容都放在 class 中,并在每次调用 Load() 时使用 cv2 的新实例进行调用,它可能会起作用。
import cv2
import threading
class Video:
def play(self):
video = cv2.VideoCapture('small.mov')
if video.isOpened() == True:
cv2.namedWindow("The Video")
cv2.moveWindow("The Video", 500,200)
elif video.isOpened() == False:
print('No Data For Loading Video')
return 0
while video.isOpened():
_, frame = video.read()
if _ == True:
cv2.imshow("The Video",frame)
if cv2.waitKey(10) & 0xff == 27:
break
if _ == False :
break
cv2.destroyAllWindows()
video.release()
def Load():
v=Video()
v.play()
del v
t = threading.Thread(target = Load)
t.start()
我想通过调用 cv2.imshow() 方法打开一个在屏幕上显示正在加载视频的功能。 首先我想演示一下代码,然后再演示问题。
import cv2
import threading
def Load():
video = cv2.VideoCapture('Loading.mov')
if video.isOpened() == True:
cv2.namedWindow("The Video")
cv2.moveWindow("The Video", 500,200)
elif video.isOpened() == False:
print('No Data For Loading Video')
return 0
while video.isOpened():
_, frame = video.read()
if _ == True:
cv2.imshow("The Video",frame)
if cv2.waitKey(10) & 0xff == 27:
break
if _ == False :
break
cv2.destroyAllWindows()
video.release()
t = threading.Thread(target = Load)
t.start()
现在,问题:
当我第一次调用 t.start() 时,线程启动并正确显示视频。
循环中断后,如果我再次尝试创建一个新的 t
作为线程并再次 .start() 它,它根本不会显示任何内容!不是错误,什么都没有!
I am using spyder to re run the codes. And I want to re run the video whenever I needed .
现在,问题出在哪里?
我用 mp4-Video 试过你的代码(http://techslides.com/demos/sample-videos/small.mp4) and it works. I converted said video to mov with https://video.online-convert.com/convert-to-mov 它仍然有效...
虽然我可能有一个有根据的猜测: 您应该尝试使用 cv2 的新实例进行每次调用。
我假设问题可能是,线程的第二次调用继承了第一次调用的状态(尤其是 cv2 的内部状态),因为它只是一个函数,因此视频处于状态 "already played" 之类的东西,不再显示任何内容。
因此,如果您将所有内容都放在 class 中,并在每次调用 Load() 时使用 cv2 的新实例进行调用,它可能会起作用。
import cv2
import threading
class Video:
def play(self):
video = cv2.VideoCapture('small.mov')
if video.isOpened() == True:
cv2.namedWindow("The Video")
cv2.moveWindow("The Video", 500,200)
elif video.isOpened() == False:
print('No Data For Loading Video')
return 0
while video.isOpened():
_, frame = video.read()
if _ == True:
cv2.imshow("The Video",frame)
if cv2.waitKey(10) & 0xff == 27:
break
if _ == False :
break
cv2.destroyAllWindows()
video.release()
def Load():
v=Video()
v.play()
del v
t = threading.Thread(target = Load)
t.start()