-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1445 lines (754 loc) · 75.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="theme-next muse use-motion" lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />
<meta property="og:type" content="website">
<meta property="og:title" content="侯奇的博客">
<meta property="og:url" content="http://[email protected]/index.html">
<meta property="og:site_name" content="侯奇的博客">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="侯奇的博客">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Muse',
sidebar: {"position":"left","display":"post"},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: 'Author'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://[email protected]/"/>
<title> 侯奇的博客 </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="en">
<div class="container one-collumn sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">侯奇的博客</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
Home
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
Archives
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
Tags
</a>
</li>
<li class="menu-item menu-item-search">
<a href="javascript:;" class="popup-trigger">
<i class="menu-item-icon fa fa-search fa-fw"></i> <br />
Search
</a>
</li>
</ul>
<div class="site-search">
<div class="popup">
<span class="search-icon fa fa-search"></span>
<input type="text" id="local-search-input">
<div id="local-search-result"></div>
<span class="popup-btn-close">close</span>
</div>
</div>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://[email protected]/2016/12/28/MacOSX Sierra下Python3配置opencv/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="HouQi">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/images/me/BS-grad.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="侯奇的博客">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="侯奇的博客" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/28/MacOSX Sierra下Python3配置opencv/" itemprop="url">
MacOSX Sierra下Python3配置opencv
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-28T11:11:05+08:00">
2016-12-28
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programming/" itemprop="url" rel="index">
<span itemprop="name">Programming</span>
</a>
</span>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/12/28/MacOSX Sierra下Python3配置opencv/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/28/MacOSX Sierra下Python3配置opencv/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="编译注意事项"><a href="#编译注意事项" class="headerlink" title="编译注意事项"></a>编译注意事项</h2><p>opencv2 不支持 python3,为了在python3下使用opencv,需要使用opencv3。</p>
<h3 id="通过-homebrew-安装"><a href="#通过-homebrew-安装" class="headerlink" title="通过 homebrew 安装"></a>通过 homebrew 安装</h3><p>可以通过 brew 也可以安装opencv。</p>
<figure class="highlight brainfuck"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment">brew</span> <span class="comment">install</span> <span class="comment">opencv3</span> <span class="literal">-</span><span class="literal">-</span><span class="comment">with</span><span class="literal">-</span><span class="comment">python3</span> <span class="literal">-</span><span class="literal">-</span><span class="comment">c</span><span class="literal">+</span><span class="literal">+</span><span class="comment">11</span> <span class="literal">-</span><span class="literal">-</span><span class="comment">with</span><span class="literal">-</span><span class="comment">contrib</span> <span class="literal">-</span><span class="literal">-</span><span class="comment">HEAD</span> <span class="literal">-</span><span class="literal">-</span><span class="comment">with</span><span class="literal">-</span><span class="comment">ffmpeg</span></div></pre></td></tr></table></figure>
<p>brew 的一些参数</p>
<figure class="highlight elm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="comment">--32-bit</span></div><div class="line"> <span class="type">Build</span> <span class="number">32</span>-bit only</div><div class="line"><span class="comment">--c++11</span></div><div class="line"> <span class="type">Build</span> using <span class="type">C</span>++<span class="number">11</span> mode</div><div class="line"><span class="comment">--with-contrib</span></div><div class="line"> <span class="type">Build</span> <span class="string">"extra"</span> contributed modules</div><div class="line"><span class="comment">--with-cuda</span></div><div class="line"> <span class="type">Build</span> with <span class="type">CUDA</span> v7<span class="number">.0</span>+ sup<span class="keyword">port</span></div><div class="line"><span class="comment">--with-ffmpeg</span></div><div class="line"> Build with ffmpeg support</div><div class="line"><span class="comment">--with-gphoto2</span></div><div class="line"> Build with gphoto2 support</div><div class="line"><span class="comment">--with-gstreamer</span></div><div class="line"> Build with gstreamer support</div><div class="line"><span class="comment">--with-jasper</span></div><div class="line"> Build with jasper support</div><div class="line"><span class="comment">--with-java</span></div><div class="line"> Build with Java support</div><div class="line"><span class="comment">--with-libdc1394</span></div><div class="line"> Build with libdc1394 support</div><div class="line"><span class="comment">--with-opengl</span></div><div class="line"> Build with OpenGL support (must use <span class="comment">--with-qt5)</span></div><div class="line"><span class="comment">--with-openni</span></div><div class="line"> <span class="type">Build</span> with openni support</div><div class="line"><span class="comment">--with-openni2</span></div><div class="line"> <span class="type">Build</span> with openni2 support</div><div class="line"><span class="comment">--with-python3</span></div><div class="line"> <span class="type">Build</span> with python3 support</div><div class="line"><span class="comment">--with-qt</span></div><div class="line"> <span class="type">Build</span> the <span class="type">Qt4</span> backend to <span class="type">HighGUI</span></div><div class="line"><span class="comment">--with-qt5</span></div><div class="line"> <span class="type">Build</span> the <span class="type">Qt5</span> backend to <span class="type">HighGUI</span></div><div class="line"><span class="comment">--with-quicktime</span></div><div class="line"> <span class="type">Use</span> <span class="type">QuickTime</span> for <span class="type">Video</span> <span class="type">I</span>/<span class="type">O</span> instead of <span class="type">QTKit</span></div><div class="line"><span class="comment">--with-tbb</span></div><div class="line"> <span class="type">Enable</span> parallel code in <span class="type">OpenCV</span> using <span class="type">Intel</span> <span class="type">TBB</span></div><div class="line"><span class="comment">--without-eigen</span></div><div class="line"> <span class="type">Build</span> without eigen support</div><div class="line"><span class="comment">--without-numpy</span></div><div class="line"> <span class="type">Use</span> a numpy you've installed yourself instead of a <span class="type">Homebrew</span>-packaged numpy</div><div class="line"><span class="comment">--without-opencl</span></div><div class="line"> <span class="type">Disable</span> <span class="type">GPU</span> code in <span class="type">OpenCV</span> using <span class="type">OpenCL</span></div><div class="line"><span class="comment">--without-openexr</span></div><div class="line"> <span class="type">Build</span> without openexr support</div><div class="line"><span class="comment">--without-python</span></div><div class="line"> <span class="type">Build</span> without <span class="type">Python</span> support</div><div class="line"><span class="comment">--without-tests</span></div><div class="line"> <span class="type">Build</span> without accuracy & performance tests</div><div class="line"><span class="comment">--HEAD</span></div><div class="line"> <span class="type">Install</span> <span class="type">HEAD</span> version</div></pre></td></tr></table></figure>
<h3 id="通过-cmake-安装"><a href="#通过-cmake-安装" class="headerlink" title="通过 cmake 安装"></a>通过 cmake 安装</h3><p>下面是 cmake 的选项(请自行修改python对应的路径,如3.6.0、3.6等)。</p>
<figure class="highlight haml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line">cmake \</div><div class="line">.. \</div><div class="line">-<span class="ruby">DCMAKE_C_FLAGS_RELEASE=-DNDEBUG \</span></div><div class="line">-<span class="ruby">DCMAKE_CXX_FLAGS_RELEASE=-DNDEBUG \</span></div><div class="line">-<span class="ruby">DCMAKE_INSTALL_PREFIX=<span class="regexp">/usr/local</span><span class="regexp">/Cellar/opencv</span>3/HEAD-<span class="number">7</span>dd3723_4 \</span></div><div class="line">-<span class="ruby">DCMAKE_BUILD_TYPE=Release \</span></div><div class="line">-<span class="ruby">DCMAKE_FIND_FRAMEWORK=LAST \</span></div><div class="line">-<span class="ruby">DCMAKE_VERBOSE_MAKEFILE=ON \</span></div><div class="line">-<span class="ruby">Wno-dev \</span></div><div class="line">-<span class="ruby">DBUILD_JASPER=OFF \</span></div><div class="line">-<span class="ruby">DBUILD_JPEG=ON \</span></div><div class="line">-<span class="ruby">DBUILD_TIFF=ON \</span></div><div class="line">-<span class="ruby">DBUILD_OPENEXR=ON \</span></div><div class="line">-<span class="ruby">DBUILD_PNG=ON \</span></div><div class="line">-<span class="ruby">DBUILD_ZLIB=ON \</span></div><div class="line">-<span class="ruby">DCMAKE_OSX_DEPLOYMENT_TARGET= \</span></div><div class="line">-<span class="ruby">DJPEG_INCLUDE_DIR=<span class="regexp">/usr/local</span><span class="regexp">/opt/jpeg</span><span class="regexp">/include \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DJPEG_LIBRARY=/usr</span><span class="regexp">/local/opt</span><span class="regexp">/jpeg/lib</span><span class="regexp">/libjpeg.dylib \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DBUILD_opencv_java=ON \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DBUILD_opencv_python2=ON \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DBUILD_opencv_python3=ON \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DBUILD_TESTS=ON \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DBUILD_PERF_TESTS=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_1394=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_EIGEN=ON \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_FFMPEG=ON \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_GPHOTO2=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_GSTREAMER=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_JASPER=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_OPENEXR=ON \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_OPENGL=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_QUICKTIME=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_QT=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_TBB=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_VTK=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DWITH_CUDA=OFF \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DOPENCV_EXTRA_MODULES_PATH=/</span>Volumes/Data/share/opencv_contrib/modules \</span></div><div class="line">-<span class="ruby">DPYTHON2_EXECUTABLE=<span class="regexp">/usr/bin</span><span class="regexp">/python \</span></span></div><div class="line">-<span class="ruby"><span class="regexp">DPYTHON2_LIBRARY=/</span>System/Library/Frameworks/Python.framework/Versions/<span class="number">2.7</span>/lib/libpython2.<span class="number">7</span>.dylib \</span></div><div class="line">-<span class="ruby">DPYTHON2_INCLUDE_DIR=<span class="regexp">/System/</span>Library/Frameworks/Python.framework/Versions/<span class="number">2.7</span>/<span class="keyword">include</span>/python2.<span class="number">7</span> \</span></div><div class="line">-<span class="ruby">DPYTHON3_LIBRARY=<span class="regexp">/usr/local</span><span class="regexp">/opt/python</span>3/Frameworks/Python.framework/Versions/<span class="number">3.6</span>/lib/python3.<span class="number">6</span>/config-<span class="number">3.6</span>m-darwin/libpython3.<span class="number">6</span>.dylib \</span></div><div class="line">-<span class="ruby">DPYTHON3_INCLUDE_DIR=<span class="regexp">/usr/local</span><span class="regexp">/Cellar/python</span>3/<span class="number">3.6</span>.<span class="number">0</span>/Frameworks/Python.framework/Versions/<span class="number">3.6</span>/<span class="keyword">include</span>/python3.<span class="number">6</span>m \</span></div><div class="line">-<span class="ruby">DENABLE_SSSE3=ON \</span></div><div class="line">-<span class="ruby">DENABLE_SSE41=ON \</span></div><div class="line">-<span class="ruby">DENABLE_SSE42=ON \</span></div><div class="line">-<span class="ruby">DENABLE_AVX=ON</span></div></pre></td></tr></table></figure>
<p>之后执行</p>
<figure class="highlight gauss"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">make</span> && <span class="built_in">make</span> install</div></pre></td></tr></table></figure>
<p>找到编译完成的cv.so 文件,并创建软链接即可。</p>
<figure class="highlight crystal"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ln -s /usr/local/Cellar/opencv3/HEAD-<span class="number">7</span>dd3723_4/<span class="class"><span class="keyword">lib</span>/<span class="title">python3</span>.6/<span class="title">site</span>-<span class="title">packages</span>/<span class="title">cv2</span>.<span class="title">cpython</span>-36<span class="title">m</span>-<span class="title">darwin</span>.<span class="title">so</span> /<span class="title">usr</span>/<span class="title">local</span>/<span class="title">lib</span>/<span class="title">python3</span>.6/<span class="title">site</span>-<span class="title">packages</span>/<span class="title">cv2</span>.<span class="title">so</span></span></div></pre></td></tr></table></figure>
<h3 id="可能会遇到的问题"><a href="#可能会遇到的问题" class="headerlink" title="可能会遇到的问题"></a>可能会遇到的问题</h3><p>需要注意的是:最新的Mac系统下编译Opencv 3.0 和 Opencv 3.1 可能会出错。该问题出现在OSX 10.12 之后的版本中,早期版本无此问题。opencv 3.2解决了该问题。</p>
<figure class="highlight mel"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[ <span class="number">2</span>%] Built target zlib</div><div class="line">[ <span class="number">6</span>%] Built target libtiff</div><div class="line">[ <span class="number">11</span>%] Built target libjpeg</div><div class="line">[ <span class="number">17</span>%] Built target libwebp</div><div class="line">[ <span class="number">21</span>%] Built target libjasper</div><div class="line">[ <span class="number">24</span>%] Built target libpng</div><div class="line">[ <span class="number">32</span>%] Built target IlmImf</div><div class="line">[ <span class="number">37</span>%] Built target opencv_core</div><div class="line">[ <span class="number">42</span>%] Built target opencv_imgproc</div><div class="line">[ <span class="number">43</span>%] Built target opencv_imgcodecs</div><div class="line">[ <span class="number">44</span>%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_qtkit.mm.o</div><div class="line">/Volumes/Data/share/opencv<span class="number">-3.1</span><span class="number">.0</span>/modules/videoio/src/cap_qtkit.mm:<span class="number">46</span>:<span class="number">9</span>: fatal <span class="keyword">error</span>: <span class="string">'QTKit/QTKit.h'</span> <span class="keyword">file</span> not found</div><div class="line">#import <QTKit/QTKit.h></div></pre></td></tr></table></figure>
<h2 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h2><ul>
<li><a href="http://www.pyimagesearch.com/2015/06/29/install-opencv-3-0-and-python-3-4-on-osx/" target="_blank" rel="external">http://www.pyimagesearch.com/2015/06/29/install-opencv-3-0-and-python-3-4-on-osx/</a></li>
<li><a href="https://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/" target="_blank" rel="external">https://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/</a></li>
<li><a href="https://github.com/opencv/opencv/releases/tag/3.2.0" target="_blank" rel="external">https://github.com/opencv/opencv/releases/tag/3.2.0</a></li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://[email protected]/2016/12/13/Comparison to None will result in an elementwise object/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="HouQi">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/images/me/BS-grad.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="侯奇的博客">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="侯奇的博客" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/13/Comparison to None will result in an elementwise object/" itemprop="url">
Comparison to None will result in an elementwise object
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-13T09:59:44+08:00">
2016-12-13
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programming/" itemprop="url" rel="index">
<span itemprop="name">Programming</span>
</a>
</span>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/12/13/Comparison to None will result in an elementwise object/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/13/Comparison to None will result in an elementwise object/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p><code>numpy</code>数组和<code>None</code>进行比较时,可能导致意外的结果:</p>
<figure class="highlight clean"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line">np.array([<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>]) == None</div><div class="line">> <span class="literal">False</span></div><div class="line">> FutureWarning: comparison to `None` will result <span class="keyword">in</span> an elementwise object comparison <span class="keyword">in</span> the future.</div></pre></td></tr></table></figure>
<p>在此,我们应该选用<code>is</code>操作符。</p>
<p>You are looking for is:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> a <span class="keyword">is</span> <span class="keyword">None</span>:</div><div class="line"> a = something</div><div class="line"><span class="keyword">else</span>:</div><div class="line"> <span class="comment"># TODO</span></div></pre></td></tr></table></figure>
<p>下面是关于<code>is</code>的解释:</p>
<blockquote>
<p>The problem is that, by using the <code>==</code> operator, if the input element a is a numpy array, numpy will try to perform an element wise comparison and tell you that you cannot compare it.</p>
<p>For a a numpy array, <code>a == None</code> gives error, <code>np.all(a == None)</code> doesn’t (but does not do what you expect). Instead <code>a is None</code> will work regardless the data type of <code>a</code>.</p>
<p>From a java point of view, the python’s == would trigger java’s .equals, which in numpy is overrided to perform a element-wise check. Python’s is would be equivalent to check for reference in java (similar to using == with strings). However, for strings in python (i.e. a = ‘hello’; b = ‘hello’), the a is b would still return true, as both of them are the same constant (‘hello’)</p>
</blockquote>
<p>除了 <code>==</code> 外,numpy还重载了<code>__contains__</code>即<code>in</code>运算符。下面是解释:</p>
<blockquote>
<p>The future warning happens when you do something like this:</p>
<p><code>numpy.asarray([1,2,3,4]) == None</code></p>
<p>Which currently returns <code>False</code>, but I understand will return an array containing <code>[False,False,False,True]</code> in a future version of Numpy.</p>
<p>As I mentioned in the comments, your example is incorrect. Future versions of numpy would return <code>[False ,False, False, False]</code>, i.e. False for each element in the array that is not equal to None. This is more consistent with how element-wise comparisons to other scalar values currently work, e.g.:</p>
</blockquote>
<figure class="highlight ada"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">In</span> [<span class="number">1</span>]: np.<span class="keyword">array</span>([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]) == <span class="number">1</span></div><div class="line"><span class="keyword">Out</span>[<span class="number">1</span>]: <span class="keyword">array</span>([ <span class="literal">True</span>, <span class="literal">False</span>, <span class="literal">False</span>, <span class="literal">False</span>], dtype=bool)</div><div class="line"></div><div class="line"><span class="keyword">In</span> [<span class="number">2</span>]: np.<span class="keyword">array</span>([<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>]) == <span class="string">'b'</span></div><div class="line"><span class="keyword">Out</span>[<span class="number">2</span>]: <span class="keyword">array</span>([<span class="literal">False</span>, <span class="literal">True</span>, <span class="literal">False</span>, <span class="literal">False</span>], dtype=bool)</div></pre></td></tr></table></figure>
<blockquote>
<p>What confuses me is this behaviour of the in keyword with a 1D array compared to a list<br>When you test <code>x</code> in <code>y</code>, you are calling <code>y.__contains__(x)</code>. When y is a list, <code>__contains__</code>basically does something along the lines of this:</p>
</blockquote>
<figure class="highlight applescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span> <span class="built_in">item</span> <span class="keyword">in</span> y:</div><div class="line"> <span class="keyword">if</span> (<span class="built_in">item</span> <span class="keyword">is</span> x) <span class="keyword">or</span> (<span class="built_in">item</span> == x):</div><div class="line"><span class="built_in"> return</span> True</div><div class="line"><span class="built_in">return</span> False</div></pre></td></tr></table></figure>
<blockquote>
<p>As far as I can tell, <code>np.ndarray.__contains__(x)</code> performs the equivalent of this:</p>
</blockquote>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> <span class="keyword">any</span>(y == x):</div><div class="line"> <span class="keyword">return</span> <span class="keyword">True</span></div><div class="line"><span class="keyword">else</span>:</div><div class="line"> <span class="keyword">return</span> <span class="keyword">False</span></div></pre></td></tr></table></figure>
<blockquote>
<p>That is to say it tests element-wise equality over the whole array first (<code>y == x</code> would be a boolean array the size of <code>y</code>). Since in your case you are testing whether <code>y == None</code>, this will raise the FutureWarning for the reasons given above.</p>
<p>In the comments you also wanted to know why<br><code>np.nan in np.array([1, 2, 3, np.nan])</code><br>returns <code>False</code>, but<br><code>np.nan in [1, 2, 3, np.nan]</code><br>returns <code>True</code>. The first part is easily explained by the fact that <code>np.nan != np.nan</code> (see here for the rationale behind this). To understand why the second case returns True, remember that <code>list.__contains__()</code> first checks for identity (<code>is</code>) before checking equality (<code>==</code>). Since np.nan is <code>np.nan</code>, the second case will return <code>True</code>.</p>
</blockquote>
<p>参考链接:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/33954216/comparison-to-none-will-result-in-an-elementwise-object" target="_blank" rel="external">http://stackoverflow.com/questions/33954216/comparison-to-none-will-result-in-an-elementwise-object</a></li>
<li><a href="http://stackoverflow.com/questions/28337085/why-does-none-in-numpy-asarray-cause-a-future-warning" target="_blank" rel="external">http://stackoverflow.com/questions/28337085/why-does-none-in-numpy-asarray-cause-a-future-warning</a></li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://[email protected]/2016/12/10/swig 使用说明/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="HouQi">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/images/me/BS-grad.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="侯奇的博客">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="侯奇的博客" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/10/swig 使用说明/" itemprop="url">
swig 使用说明
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-10T21:32:22+08:00">
2016-12-10
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/12/10/swig 使用说明/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/10/swig 使用说明/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<blockquote>
<p>SWIG是个帮助使用C或者C++编写的软件能与其它各种高级编程语言进行嵌入联接的开发工具。SWIG能应用于各种不同类型的语言包括常用脚本编译语言例如Perl, PHP, Python, Tcl, Ruby and PHP。支持语言列表中也包括非脚本编译语言,例如C#, Common Lisp (CLISP, Allegro CL, CFFI, UFFI), Java, Modula-3, OCAML以及R,甚至是编译器或者汇编的计划应用(Guile, MzScheme, Chicken)。SWIG普遍应用于创建高级语言解析或汇编程序环境,用户接口,作为一种用来测试C/C++或进行原型设计的工具。SWIG还能够导出XML或Lisp s-expressions格式的解析树。SWIG可以被自由使用,发布,修改用于商业或非商业中。</p>
</blockquote>
<p>主要利用了C++转Python的方法和技巧。</p>
<p>首先,需要简单的了解C++和Python混合编程的知识。</p>
<p>对于已有的C++程序,我们希望其中的部分函数可以在Python中调用。当然我们可以通过学习C++和Python的混合编程知识,自己修改函数接口,但是上述过程比较复杂。swig就是为了自动话完成上述任务的。比如,对于一个C++函数,我们希望能够在添加Python的wrap,我们可以首先构建一个swig脚本,在脚本中注明我们希望调用的函数或者数据结构,然后swig就可以自动化的完成函数接口的wrap工作。</p>
<p>经过测试,在对C++程序添加Python的wrap时,swig可以很好的完成工作,但也存在一些问题。</p>
<h2 id="Tutorial"><a href="#Tutorial" class="headerlink" title="Tutorial"></a>Tutorial</h2><p>在使用swig 的过程中,首先,准备swig文件。一般以<code>.i</code>结束。下面是一个简单的swig文件<code>test.i</code>。</p>
<figure class="highlight mojolicious"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="xml"></span><span class="perl">%module test</span><span class="xml"></span></div><div class="line"><span class="perl"></span></div><div class="line">%header %{<span class="xml"></span></div><div class="line">#include "test1.h"</div><div class="line">#include "test2.h"</div><div class="line"><span class="perl">%}</span><span class="xml"></span></div><div class="line"><span class="perl"></span></div><div class="line">%ignore operator Point::operator <<;<span class="xml"></span></div><div class="line"><span class="perl">%rename (__add_<span class="number">_</span>) Point::operator +;</span><span class="xml"></span></div><div class="line"><span class="perl">%rename (__sub_<span class="number">_</span>) operator -;</span><span class="xml"></span></div><div class="line"><span class="perl">%include <span class="string">"test1.h"</span></span><span class="xml"></span></div><div class="line"><span class="perl">%include <span class="string">"test2.h"</span></span><span class="xml"></span></div></pre></td></tr></table></figure>
<p><strong>注意:rename和ignore应该放置在%include 的前面,否则,rename和ignore并不会生效。</strong></p>
<p>假设 <code>test1.h</code> 文件的内容如下所示:</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">class</span> Point {</div><div class="line"><span class="keyword">public</span>:</div><div class="line"> Point <span class="keyword">operator</span> + (<span class="keyword">const</span> Point & other);</div><div class="line"> ostream <span class="keyword">operator</span> << (ostream &os);</div><div class="line"> Point(<span class="keyword">int</span> x, <span class="keyword">int</span> y);</div><div class="line"><span class="keyword">private</span>:</div><div class="line"> <span class="keyword">int</span> x;</div><div class="line"> <span class="keyword">int</span> y;</div><div class="line">}</div><div class="line"></div><div class="line">Point <span class="keyword">operator</span> -(<span class="keyword">const</span> Point &p1, <span class="keyword">const</span> Point&p2);</div></pre></td></tr></table></figure>
<p>其次,执行swig命令,生成swig的wrapper,</p>
<figure class="highlight stata"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">swig -c++ -python <span class="keyword">test</span>.<span class="built_in">i</span></div></pre></td></tr></table></figure>
<p>执行该步骤后,在当前目录下生成两个文件:<code>test.py</code>,<code>test_wrap.cxx</code>。然后编译相关的函数:</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">g++ -shared -fpic `pkg-config --cflags opencv` test_wrap<span class="selector-class">.cxx</span> test1<span class="selector-class">.cpp</span> test2<span class="selector-class">.cpp</span> -o _test<span class="selector-class">.so</span> `pkg-config --libs python`</div></pre></td></tr></table></figure>
<p>注意:生成的so文件,命名为”<em>test.so”,即module的名称加上前缀的`</em>`。该步骤执行完成后,理论上当前目录下应该生成 _test.so 文件。</p>
<p>在当前目录下,运行测试文件test_swig.py:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">from</span> test <span class="keyword">import</span> *</div><div class="line">p1 = Point(<span class="number">1</span>, <span class="number">2</span>)</div><div class="line">p2 = Point(<span class="number">3</span>, <span class="number">4</span>)</div><div class="line">p3 = p1 + p2</div><div class="line">p4 = p2 - p1</div></pre></td></tr></table></figure>
<h2 id="swig-的依赖关系"><a href="#swig-的依赖关系" class="headerlink" title="swig 的依赖关系"></a>swig 的依赖关系</h2><p>swig 的过程如果需要额外的头文件依赖,需要在命令行中加以说明:</p>
<figure class="highlight autohotkey"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">swig -c++ -python `pkg-config --cflags opencv` test.i</div></pre></td></tr></table></figure>
<p>以及,在编译的时候也有可能需要指明额外的依赖,包括头文件和额外的库文件:</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">g++ -shared -fpic `pkg-config --cflags python opencv` test_wrap<span class="selector-class">.cxx</span> test1<span class="selector-class">.cpp</span> test2<span class="selector-class">.cpp</span> -o _test<span class="selector-class">.so</span> `pkg-config --libs opencv python`</div></pre></td></tr></table></figure>
<h2 id="忽略部分函数"><a href="#忽略部分函数" class="headerlink" title="忽略部分函数"></a>忽略部分函数</h2><p>在某些情况下,可能C++的部分函数和Python不完全兼容,为此,我们需要跳过某些函数。比如,流的重定向作用符;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">%ignore FlyPoints::<<;</div></pre></td></tr></table></figure>
<p>需要说明的是,注意swig中的命名空间。</p>
<p>参考链接:<br><a href="http://stackoverflow.com/questions/22518497/ignore-redefinition-of-operator-in-swig-interface" target="_blank" rel="external">http://stackoverflow.com/questions/22518497/ignore-redefinition-of-operator-in-swig-interface</a></p>
<h2 id="重命名部分函数"><a href="#重命名部分函数" class="headerlink" title="重命名部分函数"></a>重命名部分函数</h2><p>在某些情况下,我们希望重命名部分函数,或者变量名。比如:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">%rename (__add__) Points::operator +;</div></pre></td></tr></table></figure>
<h2 id="enum-的使用"><a href="#enum-的使用" class="headerlink" title="enum 的使用"></a>enum 的使用</h2><p>C类型的enum直接被处理成了Python中的全局变量;C++ 11类型的 enum class 类型在引用时被mangle了。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">enum</span> Weekday {</div><div class="line"> Mon, Tus, Wed, Thr, Fri, Sat, Sun</div><div class="line">};</div><div class="line"><span class="comment">// Python => Mon, Tue</span></div><div class="line"></div><div class="line"><span class="keyword">enum</span> <span class="keyword">class</span> Weekday {</div><div class="line"> Mon, Tus, Wed, Thr, Fri, Sat, Sun</div><div class="line">};</div><div class="line"><span class="comment">// Python => Weekday_Mon, Weekday_Tue</span></div></pre></td></tr></table></figure>
<p>参考链接:<br><a href="http://stackoverflow.com/questions/13269393/how-to-enumerate-enum-members-using-swig" target="_blank" rel="external">http://stackoverflow.com/questions/13269393/how-to-enumerate-enum-members-using-swig</a></p>
<h2 id="swig-可能遇到的问题"><a href="#swig-可能遇到的问题" class="headerlink" title="swig 可能遇到的问题"></a>swig 可能遇到的问题</h2><h3 id="so文件的命名问题"><a href="#so文件的命名问题" class="headerlink" title="so文件的命名问题"></a>so文件的命名问题</h3><p>so 文件的明明需要和module一致,<code>_$(module).so</code>。否则在python中,执行import可能会报错:</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">----> 1 import FlyLib</span></div><div class="line">ImportError: dynamic <span class="built_in">module</span> does <span class="keyword">not</span> define init <span class="function"><span class="keyword">function</span> <span class="params">(initFlyLib)</span></span></div></pre></td></tr></table></figure>
<h3 id="文件的顺序问题"><a href="#文件的顺序问题" class="headerlink" title="文件的顺序问题"></a>文件的顺序问题</h3><p>比如,在<code>test1.h</code>文件中定义enum类型:</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">enum</span> Weekday {</div><div class="line"> Mon, Tue, Wed, Thr, Fri, Sat, Sun</div><div class="line">};</div></pre></td></tr></table></figure>
<p>在 <code>test2.h</code> 中定义函数:</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"test1.h"</span></span></div><div class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isWeekends</span><span class="params">(Weekday day = Mon)</span></span>;</div></pre></td></tr></table></figure>
<p>则在<code>test.i</code>文件中,<code>test1.h</code>最好放在<code>test2.h</code>文件前,否则,在python程序import时,会出现错误:</p>
<figure class="highlight elixir"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">----> <span class="number">1</span> import test</div><div class="line"></div><div class="line">/Users/houqi/test.py <span class="keyword">in</span> <<span class="keyword">module</span>>()</div><div class="line"> <span class="number">100</span></div><div class="line"> <span class="number">101</span></div><div class="line">--> <span class="number">102</span> <span class="function"><span class="keyword">def</span> <span class="title">isWeekends</span></span>(day=Mon):</div><div class="line"> <span class="number">103</span> <span class="keyword">return</span> _test.isWeekends(day)</div><div class="line"> <span class="number">104</span> isWeekends = _test.isWeekends</div><div class="line"></div><div class="line"><span class="symbol">NameError:</span> name <span class="string">'Mon'</span> is <span class="keyword">not</span> <span class="keyword">defined</span></div></pre></td></tr></table></figure>
<h3 id="python-的版本问题"><a href="#python-的版本问题" class="headerlink" title="python 的版本问题"></a>python 的版本问题</h3><p>Python有Python2和Python3两个版本,在编译的时候需要分别指定。</p>
<figure class="highlight ada"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">pkg-config <span class="comment">--libs python2</span></div><div class="line">pkg-config <span class="comment">--libs python3</span></div></pre></td></tr></table></figure>
<p>以及,可能存在多个同一版本的python程序,比如,在Mac系统下,系统自带的Python和brew安装的Python以及Anaconda自带的Python。如果编译过程中指定的Python库和运行的Python程序不一致,在import时可能会出错。</p>
<p>在cmake下,可以通过命令行参数指定:</p>
<figure class="highlight crystal"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">cmake \</div><div class="line">-DTEST_PYTHON_VERSION=<span class="number">2</span> \</div><div class="line">-<span class="symbol">DPYTHON_EXECUTABLE:</span>FILEPATH=<span class="regexp">/usr/local</span><span class="regexp">/Cellar/python</span><span class="regexp">/2.7.12_2/</span>Frameworks/Python.framework/Versions/<span class="number">2.7</span>/bin/python \</div><div class="line">-DPYTHON_LIBRARIES=<span class="regexp">/usr/local</span><span class="regexp">/Cellar/python</span><span class="regexp">/2.7.12_2/</span>Frameworks/Python.framework/Versions/<span class="number">2.7</span>/<span class="class"><span class="keyword">lib</span>/<span class="title">libpython2</span>.7.<span class="title">dylib</span> \</span></div><div class="line">..</div></pre></td></tr></table></figure>
<h3 id="类的静态数组变量"><a href="#类的静态数组变量" class="headerlink" title="类的静态数组变量"></a>类的静态数组变量</h3><p>swig 好像不支持类的静态数组变量。</p>
<h2 id="swig-的详细说明文件"><a href="#swig-的详细说明文件" class="headerlink" title="swig 的详细说明文件"></a>swig 的详细说明文件</h2><p>Swig 的参考资料:</p>
<ul>
<li><a href="http://www.swig.org/translations/chinese/tutorial.html" target="_blank" rel="external">http://www.swig.org/translations/chinese/tutorial.html</a></li>
<li><a href="http://www.swig.org/Doc1.3/SWIGPlus.html" target="_blank" rel="external">http://www.swig.org/Doc1.3/SWIGPlus.html</a></li>
<li><a href="http://www.swig.org/Doc3.0/CPlusPlus11.html" target="_blank" rel="external">http://www.swig.org/Doc3.0/CPlusPlus11.html</a></li>
</ul>
<h2 id="swig-opencv"><a href="#swig-opencv" class="headerlink" title="swig + opencv"></a>swig + opencv</h2><p>参考链接:<br><a href="https://github.com/renatoGarcia/opencv-swig" target="_blank" rel="external">https://github.com/renatoGarcia/opencv-swig</a></p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://[email protected]/2016/10/13/Python-的字符编码/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="HouQi">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/images/me/BS-grad.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="侯奇的博客">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="侯奇的博客" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/10/13/Python-的字符编码/" itemprop="url">
Python 的字符编码
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-10-13T12:03:05+08:00">
2016-10-13
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/10/13/Python-的字符编码/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/10/13/Python-的字符编码/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Python默认使用的是UTF-8编码格式。存储方式有两种:str 和 unicode 编码。其中,str的存储单位为 byte,为unicode格式存储的是字符。比如说,str类型下,</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">x = <span class="string">u'你好,python域编码'</span></div><div class="line">x --> <span class="string">u'\u4f60\u597d\uff0cpython\u57df\u7f16\u7801'</span></div><div class="line">x[<span class="number">0</span>] --> <span class="string">u'\u4f60'</span></div></pre></td></tr></table></figure>
<p>但是如果执行<code>print x[0]</code>, 得到的结果将会是”你”。<br>可以将unicode编码转换为 utf-8 格式。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">y = x.encode('utf-8')</div><div class="line">y --> '\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8cpython\xe5\x9f\x9f\xe7\xbc\x96\xe7\xa0\x81'</div></pre></td></tr></table></figure>
<p>如果执行<code>print y</code>,只会输出无意义字符。<br>上述操作的逆操作:<code>y.decode('utf-8')</code><br>需要注意:在python中,默认的编码是<code>utf-8</code>格式,在处理的时候需要注意,最好采用统一的格式。</p>
<p>Python中检测编码的库:<code>chardet</code>,用法如下:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> chardet</div><div class="line">chardet.detect(open(<span class="string">'xxx'</span>, <span class="string">'r'</span>).read())</div></pre></td></tr></table></figure>
<p>得到类似 <code>{'confidence': 0.99, 'encoding': 'GB2312'}</code> 的结果。</p>
<p>参考链接:</p>
<ul>
<li><a href="http://www.jb51.net/article/62155.htm" target="_blank" rel="external">http://www.jb51.net/article/62155.htm</a></li>
</ul>
<p>Python中读写不同编码的库 codecs</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">f = codecs.open(file_name, <span class="string">'r'</span>, <span class="string">'utf-8-sig'</span>)</div><div class="line">f = codecs.open(file_name, <span class="string">'w'</span>, <span class="string">'gb2312'</span>)</div></pre></td></tr></table></figure>
<p>其他操作和普通的文件读写很类似。<br>注意<code>UTF-8 with BOM</code>,<code>codecs.UTF8_BOM</code></p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://[email protected]/2016/10/13/hello-world/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="HouQi">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/images/me/BS-grad.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="侯奇的博客">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="侯奇的博客" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/10/13/hello-world/" itemprop="url">
Hello World
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-10-13T11:50:45+08:00">
2016-10-13
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/10/13/hello-world/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/10/13/hello-world/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Welcome to <a href="https://hexo.io/" target="_blank" rel="external">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="external">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="external">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="external">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo new <span class="string">"My New Post"</span></div></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="external">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo server</div></pre></td></tr></table></figure>