'multiprocessing incorrectly returning an empty array
In this post I asked for some help with multiprocessing as I was struggling to get a return variable and was suggested to check out threading instead.
I found this youtube video looking for something on threading and it that explains that using concurrent.futures I can return a value, this sounds ideal as I need to return a numpy.array.
I've tried simply to run my videoLoop function with concurrent.futures to see what happens before going any further.
This code correctly returns a populated np.array. However, cv.imshow displays once and then closes despite the array returning correctly.
import cv2 as cv
import numpy as np
import concurrent.futures
from PIL import ImageGrab
def videoLoop():
img = ImageGrab.grab(bbox=(500, 50, 600, 60))
img_np = np.array(img )
DetectionWindow= cv.cvtColor(img_np, cv.COLOR_BGR2GRAY)
cv.imshow("Video Loop", DetectionWindow)
cv.waitKey(1)
return DetectionWindow
while True:
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(videoLoop)
I've tried to adapt the above to my actual code and instead I get an array of zeros.
import cv2 as cv
import numpy as np
import concurrent.futures
import pygetwindow
from PIL import ImageGrab
def videoLoop():
window = pygetwindow.getWindowsWithTitle('Notepad')[0]
x1 = window.left
y1 = window.top
height = window.height
width = window.width
x2 = x1 + width
y2 = y1 + height
haystack_img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
(width, height) = (haystack_img.width // 2, haystack_img.height // 2)
haystack_img_resized = haystack_img.resize((width, height))
crop = haystack_img_resized.crop((360,22,600,65))
haystack_img_np = np.array(crop)
haystack = cv.cvtColor(haystack_img_np, cv.COLOR_BGR2GRAY)
cv.imshow("Video Loop", haystack)
cv.waitKey(1)
return haystack
while True:
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(videoLoop)
print(f1.result())
What am I missing to return my actual array? and how can I ensure cv.imshow returns a video loop as expected.
Update re: comments
import cv2 as cv
import numpy as np
import concurrent.futures
from PIL import ImageGrab
import pygetwindow
def videoLoop():
window = pygetwindow.getWindowsWithTitle('Notepad')[0]
x1 = window.left
y1 = window.top
height = window.height
width = window.width
x2 = x1 + width
y2 = y1 + height
haystack_img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
haystack_img_np = np.array(haystack_img)
DetectionWindow= cv.cvtColor(haystack_img_np, cv.COLOR_BGR2GRAY)
#cv.imshow("Video Loop", DetectionWindow)
#cv.waitKey(1)
return DetectionWindow
while True:
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(videoLoop)
window = f1.result()
cv.imshow("Video Loop", window)
cv.waitKey(1)
print(f1.result())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
