15 September 2020

Python OpenCV Camera Example

This code creates displays a video feed in a window.

This code is based on this tutorial from the OpenCV documentation.  This code was tested on Windows 10, with version 4.0.0 of OpenCV. The Python version used was 3.7.7.  Python and OpenCV were installed as part of Anaconda.

The code

Start by importing the cv2 module, which will make available all the functionality we need to capture video from a camera.

1
import cv2

Then, to obtain video from a camera, we need to create an object of class VideoCapture. As input, the constructor of this class receives the index of the device we want to use. If we just have a single camera connected to the computer, we can simply pass the value 0.  I have a software virtual camera, for OBS Studio, at index 0, so to use the integrated webcam of my laptop I use an index of 1.

1
capture = cv2.VideoCapture(1)

This provides a handle to read the video from the camera frame by frame. This is achieved by calling the read method on or VideoCapture object.

This method takes no arguments and returns a tuple. The first returned value is a Boolean indicating if the frame was read correctly (True) or not (False).

We can use this value for error checking if we want to confirm no problem occurred while reading the frame. The second value returned by the read method is a numpy ndarray object representing the captured frame.

1
ret, frame = capture.read()

To display the captured frame call the imshow function of the cv2 module. This function will open a window and display the frame.

The first method argument is the window name, as a string. The second argument is the image to display.

1
cv2.imshow('video', frame)

To continuously read and display the frames, we need to repeat the two method calls. Note that when imshow is called multiple times for the same window, the new image overrides the existing one.

To avoid an infinite loop the waitKey function from cv2 can be used to check if a given key was pressed and the loop exited.

waitKey takes a delay argument in milliseconds to wait until a key is pressed. Since this is a blocking function and the code is displaying video the delay needs to be short.  A value of 1 millisecond works.

Note that the waitKey function returns the ASCII code of the pressed key.  The code assumes the ESC key is used to exit the loop. That key corresponds to the number 27.

1
2
if cv2.waitKey(1) == 27:
        break

The full loop can be seen below.

1
2
3
4
5
6
7
8
while(True):
     
    ret, frame = capture.read()
     
    cv2.imshow('video', frame)
     
    if cv2.waitKey(1) == 27:
        break

After the loop breaks, we no longer need to access the video, so we should release it with a call to the release method on the VideoCapture object.

1
capture.release()

To finalize, we will call the destroyAllWindows function of the cv2 module, to destroy the window we have created to show the frames of the camera.

1
cv2.destroyAllWindows()

The final code can be seen below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import cv2
 
capture = cv2.VideoCapture(1)
 
while(True):
     
    ret, frame = capture.read()
     
    cv2.imshow('video', frame)
     
    if cv2.waitKey(1) == 27:
        break
 
capture.release()
cv2.destroyAllWindows()

To test the code, run the script making sure you have a camera attached. You should see a result similar to figure 1, which shows the window where the frames of the camera are being displayed.

After you click the ESC key the window should close and the script complete execution. 

Figure 1: Displaying webcam video

 

Happy days!  Tis that 'simple'. 




15 August 2020

Learning Python

 Hello All,

I've been learning Python on an off for a while now, mostly prompted by the lack of proper licensing of Matlab at Manchester Metropolitan University (MMU) for research and applicable toolboxes like the DSP System and Signal Processing Toolboxes.  Python has a wealth of modules with similar functionality and I wanted to perform a comparative analysis of the two offerings.  Since then MathWorks licensing has improved dramatically for academic institutions so it became a moot point - that I have left MMU behind.

The other decision on which version of Python v2.7 or v3 has also at least for me been largely resolved since I have left MMU and gone are the advantages of available libraries v2.7.

I originally started learning Python using the recommended book Python for Scientists and Engineers and the interactive online resource CodeCombat.com.  I preferred the fun problem-based environment targetted at children rather than the book.  There are challenges and leagues for your agent-based code to compete on. 

To be continued...

24 June 2020

Raspberry Pi SSH and VNC Server

Hello Everyone,
As part of a netbooting Pi project I needed to image a freshly configured install, but encountered this screen with connecting to the VNC server:
Normally, this should be a quick fix, a bit of searching et voila!  However, this was not the case and there are many posts about resolving this, and I followed the instructions in a couple, but none resolved this issue, so that is why this post exists: concise instructions.  I remember thinking at the time when last did this that it was fiddly and that I should keep some notes about how to set this up, but I didn't, so I'm doing it now.

The Process 

After imaging a new SD card create a file "ssh", without the quotes - no dot or file suffix, in the root/top directory.  This enables the SSH service on boot.  Transfer the SD card, connect up the wired ethernet and apply power.  I use PuTTY to SSH into the RPi either use the IP or hostname "raspberrypi" and use the default username "pi" and password "raspberry".

Edit the config.txt file in the boot directory: sudo nano /boot/config.txt
Uncomment the hdmi_force_hotplug=1, by deleting the preceding hash symbol.
Ctrl+x to exit
Press y and enter to confirm saving of the file.

Run the configuration tool: sudo raspi-config

Select 3 Boot Options
Select B1 Desktop / CLI
Select B3 Desktop, to enable booting to the desktop and provide graphics for the VNC Server to display.

Select 5 Interfacing Options
Select P3 VNC
then Yes to enable VNC Server

Select Finish and then Yes to reboot.

You should now be able to connect to the Pi with a VNC Viewer. Enjoy.