Abstract
The following article will read like a recipe. We enumerate hardware components (ingredients), describe configuration (recipe), and connect. This is the foundation on which we’ll build.
ToC (Step-by-Step)
- Overview: OpenCV in Python for End-to-end License Plate Detection.
- Camera and computer setup. Raspberry pi (RPi), Canon DSLR, f-stop and ISO.
- Capturing images from DSLR to RPi. Automating capture of images and transfer to RPi.
- Model for detecting cars. Train model from scratch using YOLO and labeled images.
- Crop bounding box + rotate.
- TBD -- Model for finding license plates. Train a second model with YOLO.
- TBD -- Crop bounding box.
- TBD -- Read license plate with OCR. Pre-process image, and extract text with Paddle OCR.
Ingredients (Hardware)
- EOS 70D DSLR, and AC adapter for continuous power
- 300mm AF f/4-5.6 lens for EOS 70D
- Tripod for fixed position capture
- Raspberry Pi 4 Model B, AC adapter, with Raspbian, Python, and OpenCV installed
- USB cable connecting camera to Raspberry Pi, USB-A to USB Mini-B
Recipe
- Setup Raspberry Pi with Raspbian, Python, and OpenCV libraries. Links [1] and [2] are good models to follow.
Setup camera. Mount, power it up. Don’t connect USB cable yet; we can work on USB capture after adjusting camera settings next.
Your camera setup will be different; but for folks modeling after mine, here’s how it’s set up.-
Focus the camera lens on roadway. Use M setting for custom lens settings. I suggest finding a static piece of roadway like lane-line edge to check tack-sharp focus. Use magnify button to 10x on viewfinder to ensure focus.
This is 10x digital zoom testing lens focus. Picture is of a road reflector in highway asphalt.- Fair warning: your configuration here will vary based on light and distance. For me, I am ~110 ft from highway surface (camera looking down on highway from home), and I live in rainy Seattle. My lens choice, 300mm zoom lens, and my settings reflect this.
- To discover your best settings, I suggest first 1) set fast shutter speed, then 2) zoom, then 3) focus, then 4) test and iterate with taking picture and confirming with 10x digital zoom that you have sufficient clarity for human eye to read license plate from camera viewfinder.
- My settings were 1/1000 shutter speed, f/5, and auto-ISO
- I spent probably the most time on this step learning about fstop and ISO, and experimenting. This was key for high-speed detailed image capture.
Capture photo - use viewfinder - demonstrating that you have the right zoom/focus. Use viewfinder to zoom and explore photo, and confirm you can read license plate manually.
Demonstrating to myself that I could capture images, and see license plate from viewfinder.Connect USB cable between camera and Raspberry Pi
Confirm RPi OpenCV with simple script below. Equivalent of OpenCV “hello world!”
# tutorial
# https://docs.opencv.org/4.x/db/deb/tutorial_display_image.html
import cv2 as cv
import sys
photosPath = "/Users/japollock/Projects/TakePhoto1/photos/"
filename = "starry_night.jpg"
img = cv.imread(cv.samples.findFile(photosPath + filename))
if img is None:
sys.exit("Could not read the image.")
cv.imshow("Display window", img)
k = cv.waitKey(0)
At this point, we have working camera, working RPi, working python OpenCV, and ready to build on this foundation to capture images from DSLR with commands sent by RPi.
Next Link: Capturing images from DSLR to RPi
Appendix: References
- https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up/2
- https://www.zdnet.com/article/raspberry-pi-4-model-b-and-raspbian-buster-how-to-set-up-your-board/
- https://docs.opencv.org/4.x/db/deb/tutorial_display_image.html
- https://www.motorsportphotographer.com/motorsport-photography-camera-settings-cheat-sheet/#:~:text=1/2000th%20to%201/1000th,ensure%20the%20sharpest%20possible%20photo
Appendix: Interesting Points
- DSLR was an evolution in my camera choice, but necessary in the end. I started with a Raspberry Pi HD camera, and could not achieve the zoom and focus required for high-speed car image capture.
- In my case, I am 10 stories up from highway surface, and probably 40 ft laterally removed - this is why the 300mm zoom was necessary and fully utilized. This is related to why DSLR + lens was required instead of RPi HD camera.
- venv (Virtual env) helped a lot - particularly with going back and forth between writing code on mac book, then executing code on RPi. Helped navigate package dependencies, and avoid dependency-hell that might otherwise have gotten in the way.
Top comments (0)