一.需要使用的库
1.使用hyperLPR文件包所依赖的库
pip3 install opencv-python==3.4.6.27
pip3 install pyinstaller -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip3 install Keras
pip3 install Theano
pip3 install Numpy
pip3 install Scipy
pip3 install scikit-image
pip3 install pillow
pip3 install tensorflow
这几个库树莓派装的时候会报错,scipy装不上,hyper也可以正常工作。
2.hyperLPR
pip3 install hyperLPR
3.OpenCV 3.4.3并编译
https://github.com/opencv/opencv/releases/tag/3.4.3
下载源代码文件包source code(zip)
二.环境编译
1.编译opencv3.4.3
Mac版
进入解压后的opencv文件
cd opencv-3.4.3
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4
sudo make install
Linux版
先安装编译环境
sudo apt install build-essential
sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt install libqt4-test
sudo apt install libatlas-base-dev
sudo apt install libjasper-dev
sudo apt install libqtgui4
sudo apt install libhdf5-dev
sudo apt install libhdf5-serial-dev
编译opencv3.4.3
cd opencv-3.4.3
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j
sudo make install
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
2.hyperLPR
https://github.com/zeusees/HyperLPR
下载git源文件,然后进入文件并编译 mac/linux通用
cd hyperLPR
cd Prj-Linux
mkdir build
cd build
cmake ../
sudo make -j
3.树莓派编译出错问题,增加交换空间内存
cd /opt
sudo mkdir image
cd image
sudo touch swap #创建文件
sudo dd if=/dev/zero of=/opt/image/swap bs=1024 count=1024000 #添加交换文件并设置为1G
#过段时间会返回(这个略慢)
#1024000+0 records in
#1024000+0 records out
#大小 copied, 所用时间 s, 速度 MB/s
sudo mkswap /opt/image/swap #设置交换空间
sudo swapon /opt/image/swap #启用新增的交换空间
free -m #确认是否已经生效
# 修改/etc/fstab文件
sudo nano /etc/fstab
/opt/image/swap /swap swap defaults 0 0
三.测试
1.使用pip3安装的hyperlpr库
from hyperlpr import *
import cv2
class CarNumRecognition(object):
def __init__(self, image_path, video_path):
self._image_path = image_path
self._video_path = video_path
def video(self):
print('[INFO] starting video stream')
stream = cv2.VideoCapture(self._video_path)
while 1:
grabbed, frame = stream.read()
if not grabbed:
print('NO DATA')
break
res = HyperLPR_plate_recognition(frame)
print(res)
key = cv2.waitKey(5) & 0xFF
if key == ord('q'):
break
stream.release()
cv2.destroyAllWindows()
def image(self):
print('[INFO] starting image')
image = cv2.imread(self._image_path)
res = HyperLPR_plate_recognition(image)
print(res)
if __name__ == '__main__':
_video = 'test.mp4'
_image = '1.png'
_test = CarNumRecognition(video_path=_video, image_path=_image)
# _test.video()
_test.image()
2.使用编译完的hyperlpr包
用pycharm打开hyperlpr文件
然后找到test_image文件,点击运行。
四.从监控视频中提取车牌号
from hyperlpr import *
import cv2
class CarNumRecognize(object):
def __init__(self):
pass
def recognize(self):
cap = cv2.VideoCapture(0) # 打开自身摄像头
print(f'IP摄像头是否开启:{cap.isOpened()}')
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) # 设置buffer
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) # 设置视频画面宽
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) # 设置视频画面高
while 1:
grabbed, frame = cap.read()
if not grabbed:
print('NO DATA')
break
res = HyperLPR_plate_recognition(frame)
print(res)
cv2.imshow('Window', frame)
key = cv2.waitKey(2000) & 0xFF # 检测时间
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
_test = CarNumRecognize()
_test.recognize()