, d4 {# _3 E/ M$ a) }* b调用: 6 v M2 a/ [9 b$ q; L2 j% l# g5 V+ g
Mat inputBlob = blobFromImage(image, . Z0 \0 v& W1 u- m) o/ c inScaleFactor,) h2 F' |+ F% |* N! H
Size(inWidth, inHeight), - x5 f% X1 J x2 S# i. h1 X Scalar(meanVal, meanVal, meanVal), 2 N7 K/ {, q# s true, , B% _/ v. g6 Z false);. t: ~+ _9 y' b8 D K# M
14 e9 u) q/ B7 f; @/ f
29 D2 x' `/ l( h* W% u
35 b# M# b- C/ T: e) F: W# G2 O
4 : v, Z+ V ?$ Y: I9 d- ?, K! U2 r A58 X6 l7 |3 i5 R, s l9 J
6 . }0 F* C% v* r9 K" _+ E1 wThe values provided to blobFromImage are defined as constants and must be provided by the network provider (see the references section at the bottom to peek into where they come from). Here’s what they are:8 m1 p# u$ w A; M
" L1 M; a% e# a4 M# U. y
const int inWidth = 300; 4 a8 D1 M! O) T, o% \, @const int inHeight = 300;! `3 s7 S0 }' c: f9 A
const float meanVal = 127.5; // 255 divided by 2 : L9 _" y' {5 o) h2 aconst float inScaleFactor = 1.0f / meanVal; / K9 ]. f0 ~) k5 Q7 m* ]7 `1 2 g9 F' P- F6 w2 ~ S2/ _6 V( |6 J" b/ J4 P0 D P
3 % {5 E! O, h+ A44 z2 z% A: o8 Y, t2 ?: A5 O! b# ~
使用Opencv4 以上和Tensorflow 2.0 以上,让这个inScaleFactor值可以为0.95- h, {) ]9 q& W/ I4 N1 {, ~' R
让后进入网络,获取推理结果,像下面这样写就行: ' s: M5 F0 x6 m0 D/ K 1 m0 d' W3 w' g0 s! [$ m6 ztfNetwork.setInput(inputBlob); T, ]: G+ n+ s [9 M2 x6 |
Mat result = tfNetwork.forward();. S5 K# H9 ^! d V: G) ?) o' l
Mat detections(result.size[2], result.size[3], CV_32F, result.ptr<float>()); $ _9 h" r! G- o6 k7 _# l1 . Y' Z6 z( V: @+ i. l3 G+ e$ j2. w* |) {- l/ ?- \" r
37 [& M* p8 F+ V% a
处理代码里面比较简单就是为blob 设置 网络,使用forward()方法来计算结果,最后创建一个探测Mat 类, 有关参考opencv数组mat 可以查看一下链接 . |4 l4 k: R6 f/ |+ N/ f7 l; \. Ghttps://docs.opencv.org/3.4.1/d6/d0f/group__dnn.html#ga4051b5fa2ed5f54b76c059a8625df9f5 7 C: Z" j# e3 Z0 v7 L) b下一部分是使用detector后拿到识别的物体绑定矩形盒子,打印类名称,同时做显示 V. f9 i1 w/ s4 `8 ~+ \. m
9 r, i7 |( t+ `% ^0 \7 Ffor (int i = 0; i < detections.rows; i++) ( n8 @5 v% ]# l) b; g% V3 @{ 2 h& e1 r4 E: g9 o: G* H float confidence = detections.at<float>(i, 2); & J, E/ p, g& q6 V- l l! Y9 V, w; F
if (confidence > confidenceThreshold)$ ~* R( F5 c' I2 z
{ 8 D" M- \! {1 L" `& U4 m using namespace cv;% x7 P7 W3 L' k* ? T
4 @9 R' `5 W' F( T5 v int objectClass = (int)(detections.at<float>(i, 1)); ) R& w, {, ]0 H& B 3 J4 j3 ~5 w7 I" n. _9 _( i9 H int left = static_cast<int>( ) O. U2 K7 j0 B& T3 @2 b7 b detections.at<float>(i, 3) * image.cols); 3 u( C1 [' w: U7 O6 t int top = static_cast<int>(' M% D# `9 a$ \5 h
detections.at<float>(i, 4) * image.rows); / u3 m* U, o6 c9 |4 H# q& h6 u int right = static_cast<int>( . j# ]" D* }& ^ detections.at<float>(i, 5) * image.cols);: {; Q* F2 L3 w$ P- I# l: F
int bottom = static_cast<int>( / Q2 r' J+ }- n4 s detections.at<float>(i, 6) * image.rows);# v0 V! |' g7 n2 I Z* G! v
# @5 F6 P G) |& H! H rectangle(image, Point(left, top), . [1 V0 u2 I. t" ?, h! \; D Point(right, bottom), Scalar(0, 255, 0));% G( M5 d) U. f2 q8 T, K' Y& |
String label = classNames[objectClass].toStdString(); 7 `/ \9 V7 S5 Q' v int baseLine = 0; $ X9 e! R8 x& g6 |; I Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX,2 ]$ S; m2 z! p2 o
0.5, 2, &baseLine); / V' k9 t! A. [& \% L top = max(top, labelSize.height);" W0 ~1 G0 w# U F
rectangle(image, Point(left, top - labelSize.height),# I' `. S6 S, M5 q5 }7 b
Point(left + labelSize.width, top + baseLine),8 V& p v- S# R6 ^9 ]: \" K( u$ w
Scalar(255, 255, 255), FILLED);% v2 q0 D. Z' {( U% [* E4 ]& E2 R
putText(image, label, Point(left, top), 3 R5 I0 T% r$ e" A9 H FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));+ n; Z5 L5 h3 ]. ^/ ^+ D& F+ @
}$ \4 M& j# u8 C1 G2 X9 y' O. g! J
} . X0 F6 c% d+ Y5 J; |4 |$ e. Z7 S0 r1 }3 {) a; P4 p6 z
pixmap.setPixmap( J7 S* a' C9 X QPixmap::fromImage(QImage(image.data, 1 g8 A6 x3 [+ j1 [7 _6 S image.cols, 5 e7 f/ H) G1 Y5 t image.rows,2 f0 k1 w! Y5 C1 M, o/ O; T
image.step,; [) J7 }, c2 _$ j+ V
QImage::Format_RGB888).rgbSwapped()));9 Q; y- ^* h$ j" Z3 @
ui->videoView->fitInView(&pixmap, Qt::KeepAspectRatio);3 U& ?/ ?0 _% P: c2 f- ^
1 ' ~# j& Y7 p }5 q" \' C- s2 : [" `- g& j- [: O3 ! H: \: ?* f2 f4 2 k( e8 R3 t" g5 l1 Z1 P: m5 7 W, t) m7 p# {& C) o1 {: Q6 4 B3 ` t( C8 h: Z: @7 7 I+ j, C ^: ], }/ i1 [- Q84 T7 m: Y. k: S1 t9 C
9; o, z7 k* a* {/ ]
10% s9 g, X2 t3 g& z8 C: c) ]
11 6 t9 k/ s ]) ]12# K; v% n: a% @3 X
13+ |* T7 m" w# o5 I
14& V5 L7 e6 K3 [% e, S& U$ u- u( M
155 ^2 J" d* t" W) p( c; Y }! F7 A
16, U" f: u7 g# x6 T; t- V
17 " L3 T8 l# j+ l, n) U* \18" t( U( t! q: Q8 Q4 }
19 % |' z. I3 W1 h1 Q' W& a20 1 M/ l4 h' h$ N2 M2 v" n21 6 m3 n; j. h8 U( Z' m9 W P3 m* }22. |0 ?: f( {/ P S8 V5 S- ^9 d
23- F# h- Q9 @+ ]2 x ]+ e. @* }
24 / N/ Q# `8 a' Y7 w5 ~5 |25' c4 P0 f# R% t, S+ |* j) |
26 $ n i' ~( g: ~27# P8 Q6 A& D/ B9 T$ t- w
28( T$ Z9 ^# @ G. K
293 W& F5 [( E" M1 Y N/ h6 m. p' I( }
30 ; x/ e1 T9 p; ^7 K9 m4 T319 m5 c2 ^! e! i4 e# }7 F
32 9 I e2 u% e, ~; n9 P, s# N33 , L& R8 Y% ^ a1 I4 ~7 K$ U34- |5 y; ^* k& ~6 V3 l
35 + P& E4 b, h. q( k! u36 , u/ _2 v7 \/ a6 e373 Z: O4 [% L7 Q: X
38 ( J8 u- X0 n. y }1 i399 C; n+ G8 w: [2 G
40 ) |, P. G* u4 e1 M41$ j7 @0 o$ \( a' f
这样我们的程序基本就完成了, 不过这里我们还是需要预备一个TensorFlow 网络,同时需要获取tensorflow 模型 ( C3 b5 _: {! n- o 8 Y0 O. F1 }% R. {" j1 l9 |& sTensorflow模型获取% z* z/ `5 p; q( ]1 T& V L: G
首先,我们下载预先训练好的Tensorflow 模型,从下面这个网址- ]8 O! \- E4 l8 ?
4 E, u: v6 E3 ?% f- A+ j Z
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md! J% Y( E) P! h
然后使用ssd_mobilenet_v1_coco文件,可以从下面下载:4 p/ \* G7 t, L
. o% _- G. U7 s3 J7 ^
http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2017_11_17.tar.gz6 l9 F" ]8 O0 n) Y2 R @3 q
' k( n- L- A4 X0 c) JExtract it to get ssd_mobilenet_v1_coco_2017_11_17 folder with the pre-trained files. ; ?$ A+ h- W, U; H2 x4 _/ o0 U2 P我们必须拿到text graph 文件 ,模型,这些文件应该和opencv 兼容, 我们可以使用下面的ssd.py,从opencv的源代码的samples里面可以找到下面这个文件 ; J+ B, Y/ y+ n8 @7 F! l( _ ) L0 ]. ?4 b/ c+ h# y. y: v, ]opencv-source-files\samples\dnn\tf_text_graph_ssd.py 2 d) B9 t! r' ~) D 2 b* m) ]7 K7 `* x, `% a5 j% W2 C; ?当然也可以从opencv的github源码下面去下载 $ N5 @, m- y/ r) [; U 7 \& M7 y; ]6 Q) U6 \. O: O' {4 vhttps://github.com/opencv/opencv/blob/master/samples/dnn/tf_text_graph_ssd.py 5 u! }- J( w. ]9 U" u+ Z* a 8 B3 z; t) a9 C拷贝ssd_mobilenet_v1_coco_2017_11_17 文件夹 并且像下面这样执行% V0 B" e. N) R& y
Just copy it to ssd_mobilenet_v1_coco_2017_11_17 folder and execute the following:9 v' u: H: }# w4 r! y7 X
+ H8 b; O7 O @/ |: G
tf_text_graph_ssd.py --input frozen_inference_graph.pb --output frozen_inference_graph.pbtxt4 T3 Q: v9 Q# L& s3 ?7 ?
新版可以这样执行:7 F) a& ^9 l! a1 I; Z
tf_text_graph_ssd.py --input frozen_inference_graph.pb --output frozen_inference_graph.pbtxt --config pipeline.config; r5 b) k" o# V. s/ G3 C
分类文件和模型可以从下面下载1 ]" ^ e" u0 m; H. }
https://github.com/tensorflow/models/blob/master/research/object_detection/data/mscoco_label_map.pbtxt8 b2 f% D0 m! C% M# `
# I; M0 g: S/ K2 k/ n' D
This file won’t be of use for us the way it is, so here is a here’s a simpler (CSV) format that I’ve prepared to use to display class names when detecting them:1 O4 l$ l! _+ X! D# Q+ N
* `4 E+ t1 l" e6 v0 F& mhttp://amin-ahmadi.com/downloadfiles/qt-opencv-tensorflow/class-names.txt 5 H: o' m( O2 P }3 V& I& ?: Y2 W' D% \$ ~& N* i/ q
Now we have everything we need to run and test our classification app in actio $ r% f" f7 k z/ ]/ T 2 `+ ]3 S" Y9 w( o! Y/ w0 a启动图片分类应用程序3 Z1 M8 n& @+ }) C$ t
从QT Creator 中切换lab, 输入这些文件,! l: S) U3 C0 c3 m+ V7 a
0 h3 l% O7 [8 `3 g) X
现在切换回去,就可以开始探测了, 1 {' @1 t/ l# q1 q* r' S/ O& r% U/ k( ]& l `
+ I8 S6 \8 h! j$ K
以下网页应该对我们有帮助: % v7 f9 t/ }4 z( A0 K1 ?' Bhttps://github.com/opencv/opencv/tree/master/samples/dnn+ y! ]% G1 d( z
https://www.tensorflow.org/tutorials/image_retraining + e* |( ~2 g6 ^* g o" u————————————————$ v" H+ u J- {) @0 `' }( W
版权声明:本文为CSDN博主「qianbo_insist」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。; X: w' R/ e1 G' r: O
原文链接:https://blog.csdn.net/qianbo042311/article/details/1262535463 Q, p! E) i" w' _& v. i
* H% j ~% R3 C1 k
. `/ s2 ~0 M- O$ |' M# H- l; F7 j