I am new to python and openCV. I hope this question makes sense. I want to use a asyncio/multithreading in python in order process cv2.VideoCapture(0) in openCV asynchronously.
The reason: I can only create one cv2.VideoCapture(0) object and I cannot copy it - as far as I know. Here the errors I got.
TypeError: cannot pickle 'cv2.VideoCapture' object
TypeError: 'cv2.VideoCapture' object is not subscriptable
First I want to show the video capturing by the PCs cam in a window:
cap = cv2.VideoCapture(0)
def one():
while cv2.waitKey(1) < 0:
hasFrame, frame = cap.read()
cv2.imshow('Capturing1', frame)
While I process the same frames in a different function:
def two():
while cv2.waitKey(1) < 0:
hasFrame, frame = cap.read()
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
(0, 0, 0), swapRB=False, crop=False)
net.setInput(inpBlob)
output = net.forward()
H = output.shape[2]
W = output.shape[3]
# Empty list to store the detected keypoints
points = []
for i in range(nPoints):
threshold = 0.1
# confidence map of corresponding body's part.
probMap = output[0, i, :, :]
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
# Scale the point to fit on the original image
x = (frameWidth * point[0]) / W
y = (frameHeight * point[1]) / H
if prob > threshold:
# cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
# cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA)
# Add the point to the list if the probability is greater than the threshold
points.append((int(x), int(y)))
print(points)
else:
points.append(None)
I want to do this, because the
cv2.imshow('Capturing1', frame)
in def one() is slown down by the code in def two() if I would combine those two functions in one.
Many thanks for help. I hope it makes sense to you.
VideoCapture? Why not copyframe? Better runVideoCapturein one process and send frame to other process using queue. - furas