实现python 调用 C++ 接口的 easypr

本文实现了用python 调用 C++ 的easypr ,实现车牌的识别. CMakelist.txt 以及调用方法将会附上

CMakelist.txt, 需要 注意 add_definitions(-fPIC)  ,因为没有这句导致动态链接库编译失败,而且在easypr的编译中也要加上这句.

cmake_minimum_required(VERSION 3.0.0)
project(py_plate_locate)#动态链接库因为没有加这个而失败
add_definitions(-fPIC)# c++11 required
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/home/k/SoftWare/opencv-3.1.0/build")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/home/k/SoftWare/opencv-3.2.0/build")
# OpenVC3 required
find_package(OpenCV3.2 REQUIRED)# where to find header files
set(EASYPR_INCLUDE_DIRS "../include" )#find include file
include_directories(.)
include_directories(${EASYPR_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})#find lib file path
link_directories(easypr "../build")
link_directories(thirdparty "../build/thirdparty")# sub directories
#add_subdirectory(thirdparty easypr)if (CMAKE_SYSTEM_NAME MATCHES "Darwin")set(EXECUTABLE_NAME "py_plate_locate")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")set(EXECUTABLE_NAME "py_plate_locate")
endif ()#"main.cpp"
set(SOURCE_FILES  "py_plate_detector.cpp")# set to be releas mode
#  set(CMAKE_BUILD_TYPE Release)# test cases  生成可执行文件
# add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES})
#生成静态库
# add_library(${EXECUTABLE_NAME} STATIC ${SOURCE_FILES})
#生成动态库
add_library(${EXECUTABLE_NAME} SHARED ${SOURCE_FILES})# link opencv& easypr libs
target_link_libraries(${EXECUTABLE_NAME} easypr thirdparty ${OpenCV_LIBS})# MESSAGE(${CMAKE_BINARY_DIR}/../)
SET_TARGET_PROPERTIES(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY  "${CMAKE_BINARY_DIR}/../")#SET_TARGET_PROPERTIES(${EXECUTABLE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY  "${CMAKE_BINARY_DIR}/../")

C++ 部分 头文件:py_plate_detector.h


#ifndef PLATE_DETER_H
#define PLATE_DETER_H#include <iostream>
#include <cstring>
#include "opencv2/opencv.hpp"
#include <easypr.h>
#include <algorithm>
#include<ctime>extern "C"{using namespace std;using namespace cv;using namespace easypr;struct OutRec{int locate[200];};unsigned int plate_recog(int height,int width, uchar* frame_data, OutRec &out);
}
#endif

c++ 实现部分 py_plate_detector.cpp

#include "py_plate_detector.h"unsigned int plate_recog(int height,int width, uchar* frame_data,OutRec &out){//convert  image for python typecv::Mat image(height,width,CV_8UC3);uchar* pxvec = image.ptr<uchar>(0);int count = 0;for(int row = 0; row < height; row++){pxvec = image.ptr<uchar>(row);for(int col = 0; col < width; col++){for(int c = 0; c < 3; c++){pxvec[col*3+c] = frame_data[count];count++;}}}// using easypr to locate the plateeasypr::CPlateRecognize pr;pr.setResultShow(false);pr.setDetectType(easypr::PR_DETECT_CMSER);vector<easypr::CPlate> plateVec;int result = pr.plateRecognize(image, plateVec);if (result != 0) return  -1;        size_t num = plateVec.size();// unsigned char * data = new  unsigned char[4*num];// vector<unsigned int> out_;// memset(data,0, 4*num*sizeof(char));       // printf("num %d\n", num);for (size_t j = 0; j < num; j++){CPlate temp_plate = plateVec[j];// imshow("plate_locate", temp_plate.getPlateMat());RotatedRect rect = temp_plate.getPlatePos();if (rect.size.height > rect.size.width){std::swap(rect.size.height, rect.size.width);}int y_off_set = rect.size.height / 2;int x_off_set = rect.size.width / 2;Point2i left_P;Point2i right_P;left_P.x = rect.center.x - x_off_set;left_P.y = rect.center.y - y_off_set;right_P.x = rect.center.x + x_off_set;right_P.y = rect.center.y + y_off_set;Point2i corp_left_P = left_P;Point2i corp_right_P = right_P;corp_left_P.y = std::max(int(0), int(left_P.y - 4 * rect.size.height));corp_right_P.y = left_P.y;// cout << "corp_left_P" << corp_left_P << endl;// cout << "corp_right_P" << corp_right_P << endl;if(corp_left_P.y <0 ) continue;if(corp_left_P.x <0) continue;if(corp_right_P.y <0) continue; if(corp_right_P.x <0) continue;if(std::abs(corp_left_P.y - corp_right_P.y) < 50) continue;if(std::abs(corp_left_P.x - corp_right_P.x) < 50) continue;//yyxxout.locate[4*j+0] = max(0,corp_left_P.y);out.locate[4*j+1] = min(height,corp_right_P.y);out.locate[4*j+2] = max(0,corp_left_P.x);out.locate[4*j+3] = min(width,corp_right_P.x);}//out_.begin()  是只想第一个元素的迭代器,确切的说,不是指针//传的话就以 &*out_.begin() 来进行传递,实际上一下方式显得更简单,不那么晦涩return  0;
}

python 部分代码:


if __name__ == '__main__':#read the soll = cdll.LoadLibrarylib_plate_locate = ll("./libpy_plate_locate.so")#read imageframe = cv2.imread("/home/k/Longlongaaago/EasyPR-master/crop_vehicle/testImg/12166814_川BZT515.jpg")frame_data = np.asarray(frame, dtype=np.int8)#change the data formframe_data = frame_data.ctypes.data_as(c_char_p)#create the structrec = OutRec()#reference the structstate = lib_plate_locate.plate_recog(frame.shape[0], frame.shape[1], frame_data,byref(rec))locate_list = []if state !=0:print("plate locate false! or none!")for i in range(0,len(rec.locate),4):if rec.locate[i+0] == rec.locate[i+1] == rec.locate[i+2] == rec.locate[i+3] ==0:break;targrt = {}targrt["min_y"] = rec.locate[i+0]targrt["max_y"] = rec.locate[i + 1]targrt["min_x"] = rec.locate[i + 2]targrt["max_x"] = rec.locate[i + 3]print(rec.locate[i+0])print(rec.locate[i + 1])print(rec.locate[i + 2])print(rec.locate[i + 3])locate_list.append(targrt)for i in range(len(locate_list)):new_frame = frame[locate_list[i]["min_y"]:locate_list[i]["max_y"],locate_list[i]["min_x"]:locate_list[i]["max_x"],:]cv2.imshow('image', new_frame)cv2.waitKey()

至于如何进行编译,还是要自己学习一些编译知识

调用成功后,还要注意数值解析

参考博客,实现方式:

https://blog.csdn.net/Willen_/article/details/102744794

中间碰到编译问题的博客:

https://blog.csdn.net/chengde6896383/article/details/93737256

https://blog.csdn.net/qq_41784559/article/details/89358958

https://blog.csdn.net/u010312436/article/details/52486811

https://blog.csdn.net/chengde6896383/article/details/93737256

https://www.cnblogs.com/luoyinjie/p/7219344.html

实现python 调用 C++ 接口的 easypr相关推荐

  1. pythondockerapi_docker-py 用Python调用Docker接口的方法

    众所周知,Docker向外界提供了一个API来管理其中的资源.这个API可以是socket文件形式的(一般也是默认的,在/var/run/docker.sock中),也可以是TCP形式的.以前想要通过 ...

  2. c调python_应该是史上最全的python调用C接口

    在这段时间里,我需要使用python调用C接口.我在网上搜索了很多,再加上python的官方文件,并组织备份1.从ctypes加载dll导入* dll = () # func1dll = CDLL ( ...

  3. python调用webservice接口实例_python调用各种接口,webservice,c接口,com接口,socket协议方法...

    python调用webservice接口(SOAP) (2)调用示例: 需要先安装suds库:pip install suds from suds.client import Client #如果需要 ...

  4. python调用第三方接口获取数据_python调用接口,python接收post请求接口(附完整代码)...

    与Scala语言相比,Python有其独特的优势和广泛的应用,python调用接口,因此Spark也推出了PySpark,它在框架上提供了一个使用Python语言的接口,python接收post请求接 ...

  5. python rpc_对python调用RPC接口的实例详解

    要调用RPC接口,python提供了一个框架grpc,这是google开源的 rpc相关文档: 需要安装的python包如下: 1.grpc安装 pip install grpcio 2.grpc的p ...

  6. Python 调用WebService接口出错-suds.transport.TransportError: HTTP Error 401: Unauthori

    使用Python调用WebService接口出现"suds.transport.TransportError: HTTP Error 401: Unauthorized"错误. 问 ...

  7. python调用百度接口实现ocr识别_Python调用百度OCR实现图片文字识别的示例代码

    百度AI提供了一天50000次的免费文字识别额度,可以愉快的免费使用!下面直接上方法: 首先在百度AI创建一个应用,按照下图创建即可,创建后会获得如下: 创建后会获得如下信息: APP_ID = '* ...

  8. 如何利用python调用API接口获取数据进行测试

    一.Python 可以使用 requests 库来调用 API 接口获取数据.以下是基本的步骤: 1.安装 requests 库 pip install requests 2.导入 requests ...

  9. python调用第三方接口获取数据_python 接口实现 供第三方调用的例子

    python 接口实现 供第三方调用的例子 实验环境 1.环境问题 python 2.7 以上自带的pyunit bottle 作为一个python的简易服务器 在python安装目录 打开命令窗口( ...

最新文章

  1. 在线转flv+flash在线录制视频
  2. 一款N-沟道耗尽型JFET晶体管 MPF102
  3. 兼容低版本浏览器的一些方法
  4. 第0章:战胜恐惧和懊悔
  5. 在windows上搭建一个ftp服务器
  6. 数学建模 时间序列分析
  7. AjaxControltoolkit学习笔记—PopupControl 使用详解
  8. 日常开发需要掌握的Maven知识
  9. VC++显示文件或文件夹属性
  10. 为什么MES系统等数字化管理系统,在印刷行业应用发展得如此迅速
  11. VxVM Volume Snapshot Issue -- 卷快照删除失败示例一
  12. 没有全景相机,普通人如何用krpano做属于自己的全景图
  13. python ogr_python gdal教程之:用ogr读写矢量数据
  14. 记录生活账本,查看账目清晰更可查
  15. python爬虫——爬取淘票票正在热映电影
  16. R语言09-单变量绘图(频数分布直方图/折线图)
  17. 云台山风景区,来安化邂逅最美的景色
  18. 模拟调频与数字调频收音机区别
  19. Ubuntu16.04桌面图标消失
  20. 一周日期选择(周一至周日)

热门文章

  1. 如何设计制作你的新浪微博个人封面、模版下载、个性封面下载_马立杰_新浪博客...
  2. 微信中下载APP的方案 安卓手机弹出默认浏览器打开 苹果跳转App Store苹果商店下载
  3. python模拟-食行生鲜登陆
  4. 微信公众号注销【已解决】
  5. 七夕-我与故障有个约会
  6. 07年第一桶金okte超级搜索财富分红计划
  7. 小白学习Basemap气象画地图的第四天(省级温度分布)
  8. spring的生命周期详解
  9. mac读写NTFS格式移动硬盘
  10. vue中computed、methods、watched比较