BAIT2123 INTERNET OF THINGS                                                               Jul 2024
Practical 9 - Webcam
PART 1: Install necessary python libraries to view webcam
Reference: https://raspberrypi-guide.github.io/electronics/using-usb-webcams
Step 1: Connect an USB webcam (any commonly used webcam) to Raspberry Pi.
In terminal, type lsusb to list the connected usb devices, the webcam brand should be
appeared accordingly:
Step 2: Install open-cv and numpy library by using the following command (in type in terminal)
       sudo apt-get install python-opencv
       sudo apt-get install python-numpy
       For Raspbian Bookworm, use the following
       sudo apt-get install python3-opencv
       sudo apt-get install python3-numpy
       * Reference: https://raspberrypi-guide.github.io/programming/install-opencv.html
Step 3: In Thonny Python (ID), click “New” to create a new python file and Save As “test16.py”.
Type the following codes:
BAIT2123 INTERNET OF THINGS                                                             Jul 2024
Step 4: Run the codes
PART 2: View the webcam in webpage using Flasks
Flask is a micro web framework written in Python. It is classified as a microframework because it
does not require particular tools or libraries.
Step 1: Create a html file (we can use Thonny Python (ID), but no color syntax indicator). Type
the following html code and save under the directory “~\templates\index.html”
Step 2: Create a camera.py file. Type the following python code in camera class and save under
the directory “~\camera.py”
BAIT2123 INTERNET OF THINGS                                                            Jul 2024
Step 3: In Thonny Python (ID), click “New” to create a new python file and Save As
“~\test17.py”. Type the following codes:
* The host ip in line 24 can be adjusted to the Raspberry Pi connected network IP address.
Step 4: Run the codes.
BAIT2123 INTERNET OF THINGS                                                            Jul 2024
Click the http://[YOUR IP ADDRESS]:5000/ and the webcam view will be shown in the default
browser. Try access the same address in any other computer browser, in the same intranet.
* In any case that is unable to run the code due to IP or Port being used by another programme,
type the following command in terminal windows, find the PID of the programme and kill it.
       sudo netstat -nlp
       kill [PID]