# _2 h7 @. P+ _- f& X, |8 ^项目创建以后1 t0 I9 o6 k, I/ q: I0 }
After the project is created, replace all of the contents of CMakeLists.txt file with the following (the comments in the following code are meant as a description to why each line exists at all):& ~5 G1 q" B5 I5 ], o# N
2 r% p: I- r+ v! K! F; F" S3 r1 W+ ?/ z# Specify the minimum version of CMake(3.1 is currently recommended by Qt)) l0 N* r" s& z% f2 F
cmake_minimum_required(VERSION 3.1)! G M; o% u6 q/ T( u% P
7 s) L4 C: p" p3 d; x/ |
# Specify project title 9 m! k2 ]3 j# f) R; n9 Dproject(ImageClassifier)- `1 a9 ]1 k; {
7 |1 \) G- v0 s
# To automatically run MOC when building(Meta Object Compiler)0 K( D$ V# W5 E! S
set(CMAKE_AUTOMOC ON) & D5 P7 p" s8 ?' {% Q1 M' b' D 7 _) z5 o i7 D5 K4 E& O- E# N1 b6 v# To automatically run UIC when building(User Interface Compiler) + P- H2 W3 C% G5 L& nset(CMAKE_AUTOUIC ON), T! w" I& f; r/ m# w2 z, Z% o& n
% U) W& C9 _ V. Y# G4 a9 x
# To automatically run RCC when building(Resource Compiler) # H% X. V. o# n% K. j7 M qset(CMAKE_AUTORCC ON) 7 d' w4 e4 k" y+ \4 j+ X: ]5 L, R: V) d) H$ ]# H
# Specify OpenCV folder, and take care of dependenciesand includes$ z) D* m: Y# t' j9 W/ u' n) A. D* j
set(OpenCV_DIR "path_to_opencv")9 [' x% Y2 P- s9 }; A( C- Q( @
find_package(OpenCV REQUIRED) % z1 g9 Y# l/ v5 ^8 S2 U1 ginclude_directories(${ OpenCV_INCLUDE_DIRS }) 5 \9 S* @8 h; @1 c. ~ ) l4 I5 R" r; [) m9 n! O( y# Take care of Qt dependencies, q( ]" P" f/ }& Z r5 d6 l' C
find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED) # h# n- D; f" _( W' q. g : ]/ d2 Q, v1 h( Y4 J. s' \# add required source, header, uiand resource files; @8 p% U+ C, l
add_executable(${ PROJECT_NAME } "main.cpp" "mainwindow.h" "mainwindow.cpp" "mainwindow.ui") * D4 y3 @' f" A! r6 C, j/ z 0 `' ~* |( [, f) z% j# link required libs r* N% t/ q5 l$ ] j6 [target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Gui Qt5::Widgets ${OpenCV_LIBS}) 7 q7 l* k( d$ w6 T) B6 p. I6 J( P1# h5 D. g" P5 q. t! M# d7 ]% X
2& i7 [2 e0 S: R/ T0 R
3% ]0 ~# R# }$ C
4 + _7 \; @% _1 S6 ^3 z/ T1 k: D5" l. n% G; } Y7 t+ Z1 q l& l
69 a+ k4 A# A( p9 e& J8 B4 N! a: B
7; ~0 ^' _& [# P" L; N: s2 ]0 x7 ]
8. F6 B; G. A* S' m6 o
9 ' C+ ~+ d4 q* z! N. N100 D4 J5 m. ^$ R; v c# k
11/ M& J! w: y3 o% ?+ z
12 , r4 D a# E8 B0 F' W; `4 i13 4 V) y3 } U" N1 t0 d5 d14 & j" g& E- d9 q15, W5 _3 P. E# x; X# Z, I* ?7 T
16 . u/ ^ r7 R+ W* N6 k173 [) s" }0 s* K: D
18 ! e P' O1 x( M- D+ w& y19 ' [4 O/ [& `/ c5 e9 W: P20 4 F) N5 b9 o5 B; t% l7 ^& m8 j, v21- U+ D {' `8 x4 a
22! p1 v- l6 u' \
23+ J* n' ^5 M- Y3 f2 P
24 - O4 f' M- ?2 ]+ h) w25( c) n z% a: ?. z
26- q. v; D8 r& ^0 Y
276 ~& D' O; d v' J0 c1 W
28 ) d B/ t' N5 j5 t. K& t把里面正确的地址写好,把main.cpp 里面开始mainwindow.cpp 。/ V8 H0 X5 I' w4 p
. a4 {& b% U6 m5 H2 A
写好main.cpp! K' Y5 f9 H& K" J
#include "mainwindow.h" 1 f& o/ G& d4 O: |& ]- M8 A#include <QApplication>5 R- g5 J Y' \+ G
* L4 m" X0 [7 u5 ~* A! j6 P: Iint main(int argc, char* argv[])( O# \+ x3 K8 ^4 i. L
{ 8 e+ C2 V' ~3 K# p+ D QApplication a(argc, argv);5 L1 b: R- l E( K5 \
MainWindow w;# _# a5 g8 g1 O6 M
w.show(); , i- X. a% O( P1 m- t. L- t( N6 v/ w8 I5 n
return a.exec();7 q j( U& b1 h1 P* x# r
}+ U$ z# U! ]* t! H/ O8 `* c) B
1' v* i' a: G" O3 l$ ~! x
20 @/ a; Q0 f( J6 n t! n
3 8 s& E5 x; ], A& d: p2 N4 L4 q" Z; s$ z% c9 c$ l6 ]5$ f8 c; @: t# J
6 S8 m/ V- _, v5 U7 Y, y" e0 ~5 V: u7 * f: `- R [, D0 u( X9 y o- E' ?8 : Q" [- |8 a H' _! B& ?" z; V97 ?& D9 S0 x2 q( I, z" S
10( W/ x2 r2 X$ v( \
115 h( i. j9 i4 `( z6 B) @
现在使用设计器加上一个窗体,0 E _1 b0 p4 I ^# ~
? H. N+ n# k5 v+ } j
选择Main Window) K: y) }% K. q- v+ Q
$ K& s$ {' A* L% Z7 y 2 N- Z; W! X& B) c( ]像下面这样设计窗体就行了% \! E- ~. F: ]" @1 ]2 O; `" W
* V1 R0 N: L& K9 H: Z, r
) K, X: k& M$ Q# f, K
“mainwindow.h” 文件代码如下 ) X. O% }3 X+ t# T1 m7 f % @" f0 ?4 J7 i4 U3 z+ Z: [#include <QMainWindow>$ I! Y# h2 t7 z0 D O( E
#include <QMessageBox>5 M/ d( p1 D2 {' O! ]
#include <QDebug>- N$ `0 Q: q5 ]4 Z- N# I
#include <QFile> % w$ @9 H! }/ d. ^: A1 s& v#include <QElapsedTimer>, p" j' B4 \1 Q. @
#include <QGraphicsScene>5 x3 D- u5 m; P! h6 ]0 P8 n9 M! p+ \# S
#include <QGraphicsPixmapItem> " J7 V* D- b1 e5 S' o. O( x3 n) e#include <QCloseEvent> % C! \% M. H% H#include <QFileDialog> ! p1 p% D: ~* U. W+ ?, X' Z#include <opencv2/opencv.hpp> # Q' i) S# R0 }3 eWe will also need the following private members:5 b: F ^2 ]7 Q+ c- F) {4 v
/ B7 Q D [8 X. W( N
cv::dnn::Net tfNetwork;; O; Q4 |& t# D1 _9 l
QGraphicsScene scene; 5 H0 p1 ?9 u8 u Z' b) c; ^QGraphicsPixmapItem pixmap; . O7 {' n* U) Z2 B/ `+ `+ T3 J5 D( R/ r- Zbool videoStopped;; C4 u% e4 v# G+ ]% _. w
1( q% v1 c$ _1 h) v( h) w6 f% i# ~
2 - w; ]+ L. u f5 S8 e# A37 U3 m5 A5 }) j
4 . R5 E' o( D& B+ K5 8 J4 E k E& f5 Z$ {6' M* J- b% v3 I- v9 c& m; { a
7 5 i& o4 p/ M( r8 % L9 m6 {: ]; @- m92 @+ `4 a. j6 y( n7 o& z
10 n2 n! b6 U5 A6 `) W. U7 k8 a/ B
116 @2 t8 ^; H, U7 @
12 1 J5 M) D! k. D6 _7 |' B& V- D1 U13. {: ]- \) S0 x, f+ G
14 - p- ~; X L# d$ r7 H15+ T) G- N8 w' A( J+ ^5 A9 b. |
16, `9 `1 K4 d9 b4 V3 } w# e
tfNetwork 是opencv的深度网络分类器,场景scene and pixmaps 用来显示,变量 videoStopped 作为一个标记去停止视频. 我们的代码如下所示.& v3 W. f/ x k! g; c$ }2 P
0 U n b7 u- \
#ifndef MAINWINDOW_H , m# K. ]: ]$ _- D' f8 I#define MAINWINDOW_H 6 W$ F! E$ F) B5 s" X" p. F' U" V0 W7 n( B
#include <QMainWindow>" Q1 s) |$ [# z2 P& y0 \
#include <QMessageBox> ! z$ ]; ?: l0 v: [( s#include <QDebug>! K) f2 U) L/ f6 k! b4 K
#include <QFile> 4 n L0 n& @$ A#include <QElapsedTimer>: ~/ @5 s: p* ?, @0 l% O: D
#include <QGraphicsScene>1 C3 J# O- W% f" V2 P& b7 p0 m; C
#include <QGraphicsPixmapItem> f$ m$ \( y Y9 ~#include <QCloseEvent>6 _- D' T+ y: K% l
#include <QFileDialog>' |# c4 O: W5 L3 U
#include <opencv2/opencv.hpp> p7 N! X/ @1 E9 E$ T# p9 J4 K & ~" j& ^ t: L$ S, h8 O* Anamespace Ui { & k0 g! t/ z, k( _, l1 T8 zclass MainWindow; % ], X0 {, N9 l, ?} ! _; ?% e) p3 a" H: I& u! l" H% o# ?2 c
class MainWindow : public QMainWindow1 s/ s# T! C/ O
{7 e: ]! ~+ p2 j5 r
Q_OBJECT8 C( f, A& P7 q4 z! J
+ h3 {7 g- S$ W- \1 }: q
public: ; K; H8 [! y( q! ^% r g" v explicit MainWindow(QWidget *parent = 0);+ g7 ]- n8 j) m# q% z0 p: z: m( ^
~MainWindow();( M4 U. K0 I9 T! h) i! E
! x% Y$ F3 @" x( _
void closeEvent(QCloseEvent *event); 3 i) {' ^5 b# [5 a D( M: Q- h& I9 y2 p: W1 E4 t. i
private slots:0 v. \8 W1 Y$ m+ a0 T+ Y7 K/ r
void on_startBtn_pressed(); , g! n) v& p0 D% u- ~2 K + I9 t1 v9 \' D; ?/ L6 _0 h# o void on_browseVideoBtn_pressed();; m. I1 Z% o" D( w* \4 J4 X- V" T2 \
" @ K/ Y0 A6 A+ v% H$ t void on_pbBrowseBtn_pressed(); % U2 b9 k" m4 }& r" r9 z# f' `" b2 ?3 V: D
void on_pbtxtBrowseBtn_pressed();- D/ i0 r& U" Q: L1 H, B+ S
* S* {0 t; @$ x3 [% e void on_classesBrowseBtn_pressed();, O9 q$ q9 e3 L0 v- \/ u4 n8 i
- B0 A) E+ |7 S7 I; }https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md4 J3 p4 Y# @) q4 p
然后使用ssd_mobilenet_v1_coco文件,可以从下面下载: . A' `# e$ l1 t+ P" [& ?( d5 O , O6 o( Z: |( U2 g6 ~! rhttp://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2017_11_17.tar.gz& h. o2 }5 Q) Z2 t% ]7 p4 g
' e& @& h% p1 W ~' {Extract it to get ssd_mobilenet_v1_coco_2017_11_17 folder with the pre-trained files. 4 `3 u# C4 F& F, |' V# a我们必须拿到text graph 文件 ,模型,这些文件应该和opencv 兼容, 我们可以使用下面的ssd.py,从opencv的源代码的samples里面可以找到下面这个文件 9 n, ]6 D2 W- Z5 J; a1 l % c. J- }; e1 G5 u/ hopencv-source-files\samples\dnn\tf_text_graph_ssd.py) |3 Z2 u5 W2 ?* U8 K
& |& ~/ ^- I1 k! H
当然也可以从opencv的github源码下面去下载 9 ~. j# I+ @- a( Q) I ; D/ K( l \6 F8 A, e: V5 V4 shttps://github.com/opencv/opencv/blob/master/samples/dnn/tf_text_graph_ssd.py3 V7 y! e K4 h% ?2 y
0 O2 t! \' o# H" G% |2 F
拷贝ssd_mobilenet_v1_coco_2017_11_17 文件夹 并且像下面这样执行0 w- z4 H2 w- e
Just copy it to ssd_mobilenet_v1_coco_2017_11_17 folder and execute the following:0 h. V6 U( _; t) o* N9 I9 H% o4 Z
( y4 S, Z1 I) B5 ?" |, Xtf_text_graph_ssd.py --input frozen_inference_graph.pb --output frozen_inference_graph.pbtxt ; Y- ?7 n+ v' P% r4 S' [, l$ Q新版可以这样执行: : ?# n$ `; t5 p$ [tf_text_graph_ssd.py --input frozen_inference_graph.pb --output frozen_inference_graph.pbtxt --config pipeline.config, d6 L# m2 f" W x
分类文件和模型可以从下面下载 / ~8 z* M) w& z! ~0 M' W, n, uhttps://github.com/tensorflow/models/blob/master/research/object_detection/data/mscoco_label_map.pbtxt + B& U: W4 d7 Y) G# X& {4 @( f: \* r* h2 |
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:$ t0 q7 g, N4 V0 d& C
W& n* V$ c+ ~( P1 C W9 Phttp://amin-ahmadi.com/downloadfiles/qt-opencv-tensorflow/class-names.txt 6 F( G' E# [5 C4 L' S 8 J2 ?. U/ Z0 X/ yNow we have everything we need to run and test our classification app in actio 2 G- P0 Y0 B; ? 1 T/ ^& r! r0 m0 h" S启动图片分类应用程序 - |4 `9 S9 }: y- }/ U从QT Creator 中切换lab, 输入这些文件, ; _# E4 e% H; P8 w; \9 j( J - y- ]& R) N4 d3 }$ o* h现在切换回去,就可以开始探测了, $ G! H2 ?9 x* o+ ^7 C 2 ~* E1 Q8 \2 A! E6 U+ g" \ " K/ v; n! } T: P以下网页应该对我们有帮助: 1 D( f5 A. w% j- I# Rhttps://github.com/opencv/opencv/tree/master/samples/dnn . z# }9 K7 Q& b4 Whttps://www.tensorflow.org/tutorials/image_retraining ) h( N# R" ~" W X1 w————————————————9 t) x* |' A+ h. p% _: i9 M }
版权声明:本文为CSDN博主「qianbo_insist」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。* n: l* [$ r( {7 g; m6 c1 _
原文链接:https://blog.csdn.net/qianbo042311/article/details/126253546! M. P1 q1 D$ n! {# b, A
: ~" Q' m. m- U9 ]" n0 G