Practice and reinforce the concepts from Lesson 13
This exercise will guide you through the basics of using OpenCV with Python for image and video processing.
Access the exercise files here: Drive Link
Follow these steps to set up your workspace:
C13_OpenCV_template
folder from Google Drive:bulb: Make sure to extract the folder completely before opening in VSCode to avoid any file access issues.
Read, Think, Code :computer:
Question One: Setup for NumPy and OpenCV (10 minutes)
- Open the terminal in VSCode (Press `Ctrl + ``)
- Install the required libraries: CODE_BLOCK_0
- Open the file
- Import both NumPy and OpenCV: CODE_BLOCK_1
- Check and print the versions using: CODE_BLOCK_2 tip The version numbers might vary depending on when you install. As long as the installation succeeds, you're good to go!
In the
file, complete these steps:Import the required libraries: CODE_BLOCK_3
Read the image file "outing.jpg": CODE_BLOCK_4
Display the image in a window named "Outing": CODE_BLOCK_5
Keep the window open until a key is pressed: CODE_BLOCK_6
Expected Output:
:bulb: If the image doesn't appear, check that 'outing.jpg' is in the same folder as your Python file.
Question 3: Capture and show a video (20 minutes)
In the
file, create a video player:
Import the required libraries
Create a video capture object: CODE_BLOCK_7
Set up a while loop to read frames: CODE_BLOCK_8
Clean up resources: CODE_BLOCK_9 tip Common Issues
In the
file:Copy your code from
Modify the video capture line to use your webcam: CODE_BLOCK_10
Optional: Add a message when webcam starts: CODE_BLOCK_11
:warning: Privacy Notice Your webcam will turn on when you run this code. Make sure you're in a comfortable environment before testing.
:bulb: Webcam Troubleshooting
- No webcam found: Try
cv2.VideoCapture(1)
orcv2.VideoCapture(2)
- Permission denied: Check your system's camera permissions for VSCode/Terminal
- Webcam LED stays on: Make sure to properly close the program with 'q'
:warning: Important Submission Instructions Before submitting:
- Test all your Python files (setup.py, image.py, video.py, webcam.py)
- Make sure each file runs without errors
- Take screenshots of your output if required
- Zip your entire project folder
Submit your work here: Exercise Submission Form
Installation Issues:
pip install
fails, try: python -m pip install --upgrade pip
pip3 install numpy opencv-python
Common Errors:
ModuleNotFoundError
: Library not installed correctly - reinstall using pipFileNotFoundError
: Check file names and paths are correct(-215:Assertion failed)
: Usually means the image/video file wasn't foundNeed Help? Ask your instructor or post in the class forum with your error message!