Is there a way to resize an image and a window simultaneously?
I am a rookie at coding and even with that I am probably being generous. I was wondering if there is a way to resize an image, fed by a USB webcam, and resize a window simultaneously? With the Raspberry Pi3, I am trying to run four USB cameras and display the feed on a monitor. The monitor is a 21.5" so I want to have an even vertical and horizontal split in the screen so that I can view all four cameras at once. I created/cut and pasted a script written in Python and using opencv and numpy. Any ideas?
import cv2
import numpy as np
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.resizeWindow("Left Mold - Cavity 1 & 2", 20, 20)
cv2.moveWindow("Left Mold - Cavity 1 & 2", 1, 1)
cv2.imshow("Left Mold - Cavity 1 & 2", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("Left Mold - Cavity 1 & 2")
So I revised the code but I did something wrong along the way because I get some kickbacks. Here is the revised code...
import cv2
import numpy as np
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
cap3 = cv2.VideoCapture(2)
cap4 = cv2.VideoCapture(3)
while(True):
#Read Camera 1
ret, frame = cap1.read()
#Read Camera 2
ret, frame = cap2.read()
#Read Camera 3
ret, frame = cap3.read()
#Read Camera 4
ret, frame = cap4.read()
#Reduce the image size of all camera feeds to half
cap1 = cv2.resize(cap1, (0, 0), None, .5, .5)
cap2 = cv2.resize(cap2, (0, 0), None, .5, .5)
cap3 = cv2.resize(cap3, (0, 0), None, .5, .5)
cap4 = cv2.resize(cap4, (0, 0), None, .5, .5)
#Stack camera 1 & 3 vertically and camera 2 & 4 horizontally to 1 & 3
numpy_vertical = np.vstack((cap1, cap3))
numpy_horizontal = np.hstack((cap2, cap4))
array([[cap1,cap2],
[cap3,cap4]])
#Put the camera feed on the specified axis
numpy_vertical_concat = np.concatenate((cap1, cap3), axis=0)
numpy_horizontal_concat = np.concatenate((cap2, cap4), axis=1)
#Display the video feed
cv2.imshow('Left Mold - Cavity 1 & 2', cap1)
cv2.imshow('Left Mold - Cavity 3 & 4', cap2)
cv2.imshow('Right Mold - Cavity 1 & 2', cap3)
cv2.imshow('Right Mold - Cavity 3 & 4', cap4)
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
#When everything is done, release the capture
cap1.release()
cap2.release()
cap3.release()
cap4.release()
cv2.destroyAllWindows()
And here are the errors...
VIDIOC_DQBUF: No such device
libv4l2: error turning on stream: Input/output error
VIDIOC_STREAMON: Input/output error
Traceback (most recent call last):
File "MultipleVid.py", line 20, in <module>
cap1 = cv2.resize(cap1, (0, 0), None, .5, .5)
TypeError: src is not a numpy array, neither a scalar
Unable to stop the stream.: No such device
Unable to stop the stream.: No such device
Unable to stop the stream.: No such ...
sidenote: you want to put the iimshow() call after reading the frame, else you always show the previous frame
Thanks @berak! I appreciate the help.
wow, 4 cameras ?
please check
cap.isOpened()
for all your cams, and also theret
value fromcap.read()
(they are there for a reason !)then, you have to apply
cv2.resize()
on an image, not the capture !but i guess, you'll soon saturate the usb hub(s) on your box, using 4 cams. it might be a good idea to lower the resolution of the webcams (using
cap.set(cv2.CAP_PROP_FRAME_WIDTH,320); cap.set(cv2.CAP_PROP_FRAME_WIDTH,240)
), instead of resizing the imageYeah I'm not even sure if the RaspberryPi3 can handle the 4 cams but my boss said give it a whirl! Thanks for the info though! I will make some changes. What do you mean by
ret
value? is this a return value?yea, good luck with that !
and yes, ret is a return value, it says, if an image was read from the cam (the line might break, the video file might come to an end, so please check it !
what does "mat is not a numpy array, neither a scalar" mean? - google search isn't pulling anything up for the term "mat"
look at it, again it says:
src is not a numpy array
(not mat)again you put the capture object, where an image was required. same thing with your later imshow() calls, btw.
it works like this: you have a VideoCapture instance, then you request to read an image from that, then you process/show the image
No sir. See below.
pi@MoyliePi:~ $ python CamREV2.py Traceback (most recent call last): File "CamREV2.py", line 12, in <module> cv2.imshow('Left Mold - Cavity 1 & 2', cap1) TypeError: mat is not a numpy array, neither a scalar
I did not post my third revision of the script. You must be looking at the same script you looked at before. I will edit my post and show you the new script with all of your suggestions incorporated (or so I think to the best of my knowledge with coding).
hmm, it gets pretty messy here now. what about starting a new question with you current problems ?
Haha, okay I will do that.