我實際上是在嘗試修改一些 yolov5 腳本。在這里,我試圖在執行緒之間傳遞一個陣列。
def detection(out_q):
while(cam.isOpened()):
ref, img = cam.read()
img = cv2.resize(img, (640, 320))
result = model(img)
yoloBbox = result.xywh[0].numpy() # yolo format
bbox = result.xyxy[0].numpy() # pascal format
for i in bbox:
out_q.put(i) # 'i' is the List of length 6
def resultant(in_q):
while(cam.isOpened()):
ref, img =cam.read()
img = cv2.resize(img, (640, 320))
qbbox = in_q.get()
print(qbbox)
if __name__=='__main__':
q = Queue(maxsize = 10)
t1 = threading.Thread(target= detection, args = (q, ))
t2 = threading.Thread(target= resultant, args = (q, ))
t1.start()
t2.start()
t1.join()
t2.join()
我試過這個,但它給了我這樣的錯誤:
Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:155
那么還有其他方法可以傳遞陣列嗎?任何型別的教程/解決方案都值得贊賞。如果對我的問題有任何誤解,請告訴我。非常感謝!!
更新:::
我是這樣嘗試的。。
def detection(ns, event):#
## a = np.array([1, 2, 3]) -
#### a= list(a) | #This is working
## ns.value = a |
## event.set() -
while(cam.isOpened()):
ref, img = cam.read()
img = cv2.resize(img, (640, 320))
result = model(img)
yoloBbox = result.xywh[0].numpy() # yolo format
bbox = result.xyxy[0].numpy() # pascal format
for i in bbox:
arr = np.squeeze(np.array(i))
print("bef: ", arr) -
ns.value = arr | # This is not working
event.set() -
def transfer(ns, event):
event.wait()
print(ns.value)
if __name__=='__main__':
## detection()
manager = multiprocessing.Manager()
namespace = manager.Namespace()
event=multiprocessing.Event()
p1 = multiprocessing.Process(target=detection, args=
(namespace, event),)
p2= multiprocessing.Process(target=transfer, args=(namespace,
event),)
p1.start()
p2.start()
p1.join()
p2.join()
The output from the above "arr" = [ 0 1.8232
407.98 316.46 0.92648 0]
但我得到的只是空白。沒有錯誤,沒有警告,只有空白。我測驗了 arr 是否有價值。我測驗了串列,np 陣列都在共享標記為作業的資料。但是為什么“arr”陣列中的資料是空白的(共享后),我該怎么辦?
uj5u.com熱心網友回復:
那么還有其他方法可以傳遞陣列嗎?
是的,您可以使用multiprocessing.shared_memory,因為它是標準庫的一部分python3.8,并且PyPI 具有允許在python3.6和python3.7. 請參閱鏈接檔案中的示例以了解如何multiprocessing.shared_memory使用numpy.ndarray
uj5u.com熱心網友回復:
@Daweo 提供的建議使用共享記憶體的答案是正確的。
但是,也值得考慮使用鎖來“保護”對 numpy 陣列的訪問(這不是執行緒安全的)。
見: -這個
uj5u.com熱心網友回復:
好的,謝謝大家的幫助。我使用多處理佇列來共享資料。然后我將我的程式多處理轉移到執行緒。
def capture(q):
cap =
cv2.VideoCapture(0)
while True:
ref, frame = cap.read()
frame = cv2.resize(frame, (640, 480))
q.put(frame)
def det(q):
model = torch.hub.load('ultralytics/yolov5','yolov5s',device='cpu')
model.conf = 0.30 # model confidence level
model.classes = [0] # model classes (where 0 = person, 2 = car)
model.iou = 0.55 # bounding box accuracy
while True:
mat = q.get()
det = model(mat)
bbox = det.xyxy[0].numpy()
for i in bbox:
print(i)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/475212.html
標籤:Python python-3.x 多线程
