I decided to start off developing the algorithmic part of the car collision project. After a few weeks of debating whether to use C++ or Python, I decided to use Python since there are around 10 weeks left to get this project up and running. I also decided to use OpenCV as it has many of the functions we need to perform and also its highly optimised. Although through my research into OpenCV shows that there is no built in way to get a Depth Map that returns exact distances for each pixel, I decided we can worry about that later after we get the Depth Map working. In a worst case scenario, we can map depth map values to distances or find an alternative. I just wanted to get our feet wet with the project.
Displaying Two Camera Streams
Now to actually get some code going and to start off the project, I’m going to be display the camera’s video stream to two individual windows. The code below does exactly that. One thing to note about the code below is that it is compatible with both Python and Python3. In addition to that, the only part of the code below you might have to modify to get things working are the camera numbers on lines 5 and 6. In my current situation, my computer’s webcam is camera number 0 and isn’t one of the cameras I want to be using for this project. The external cameras that I want to display re cameras 1 and 2, which is what I have selected in the code below.
import cv2
cv2.namedWindow("Left Camera") # New window for previewing the left camera
cv2.namedWindow("Right Camera") # New window for previewing the right camera
vcLeft = cv2.VideoCapture(0) # Load video campture for the left camera
vcRight = cv2.VideoCapture(1) # Load video capture for the right camera
if vcLeft.isOpened() and vcRight.isOpened():
rvalLeft, frameLeft = vcLeft.read()
rvalRight, frameRight = vcRight.read()
else:
rvalLeft = False
rvalRight = False
while rvalLeft and rvalRight: # If the cameras are opened
cv2.imshow("Left Camera", frameLeft)
cv2.imshow("Right Camera", frameRight)
rvalLeft, frameLeft = vcLeft.read()
rvalRight, frameRight = vcRight.read()
key = cv2.waitKey(20)
if key == 27:
break
# When the user hits escape, the windows will get destroyed
cv2.destroyWindow("Left Camera")
cv2.destroyWindow("Right Camera")
vcLeft.release()
vcRight.release()