質問編集履歴

2

変更部分の抽出

2016/12/13 02:08

投稿

nnahito
nnahito

スコア2004

test CHANGED
File without changes
test CHANGED
@@ -370,8 +370,24 @@
370
370
 
371
371
  # 追記1
372
372
 
373
+ ベクトルの1次元変換は、このような感じでは無いでしょうか?
374
+
373
375
  ```python
374
376
 
377
+ # 次元を1次元に変換
378
+
379
+ img_np = np.asarray(img)
380
+
381
+ img_np = img_np.shape[0] * img_np.shape[1]
382
+
383
+ ```
384
+
385
+
386
+
387
+ 以下、全文
388
+
389
+ ```python
390
+
375
391
  #!/usr/bin/env python
376
392
 
377
393
  # -*- coding: utf-8 -*-

1

回答に基づくデータの追記

2016/12/13 02:08

投稿

nnahito
nnahito

スコア2004

test CHANGED
File without changes
test CHANGED
@@ -363,3 +363,315 @@
363
363
  - MacOSX Yosemite 10.10.5
364
364
 
365
365
  - Python 2.7.10
366
+
367
+
368
+
369
+
370
+
371
+ # 追記1
372
+
373
+ ```python
374
+
375
+ #!/usr/bin/env python
376
+
377
+ # -*- coding: utf-8 -*-
378
+
379
+
380
+
381
+ import sys
382
+
383
+ import os
384
+
385
+ from PIL import Image
386
+
387
+ import ImageFilter
388
+
389
+ import numpy as np
390
+
391
+ from sklearn import svm
392
+
393
+
394
+
395
+
396
+
397
+ # 定数定義場
398
+
399
+ STANDARD_SIZE = (300, 167)
400
+
401
+ CONST_NUM = 50100
402
+
403
+
404
+
405
+
406
+
407
+ # 変数定義場
408
+
409
+ Images = [] # 教師画像
410
+
411
+ Labels = [] # ラベル(ok = 0, ng = 1とする)
412
+
413
+
414
+
415
+
416
+
417
+ # 学習用画像データ(OK)を読み込む
418
+
419
+ for f in os.listdir("./ok/"):
420
+
421
+ # 画像を読み込む
422
+
423
+ img = Image.open("./ok/" + f)
424
+
425
+
426
+
427
+ # グレースケール化
428
+
429
+ img = img.convert('L')
430
+
431
+
432
+
433
+ # エッジ抽出
434
+
435
+ img = img.filter(ImageFilter.FIND_EDGES)
436
+
437
+
438
+
439
+ # ダウンサイジング
440
+
441
+ img = img.resize(STANDARD_SIZE)
442
+
443
+
444
+
445
+ # 次元を1次元に変換
446
+
447
+ img_np = np.asarray(img)
448
+
449
+ img_np = img_np.shape[0] * img_np.shape[1]
450
+
451
+
452
+
453
+ # データを追加
454
+
455
+ Images.append(img_np)
456
+
457
+
458
+
459
+ # ラベルを設定
460
+
461
+ Labels.append(0)
462
+
463
+
464
+
465
+ print "Images, Labels = " + str(len(Images)) + ", " + str(len(Labels))
466
+
467
+
468
+
469
+ img.save('./out/ok_' + f)
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+ # 学習用画像データ(NG)を読み込む
478
+
479
+ for f in os.listdir("./ng/"):
480
+
481
+ # 画像を読み込む
482
+
483
+ img = Image.open("./ng/" + f)
484
+
485
+
486
+
487
+ # グレースケール化
488
+
489
+ img = img.convert('L')
490
+
491
+
492
+
493
+ # エッジ抽出
494
+
495
+ img = img.filter(ImageFilter.FIND_EDGES)
496
+
497
+
498
+
499
+ # ダウンサイジング
500
+
501
+ img = img.resize(STANDARD_SIZE)
502
+
503
+
504
+
505
+ # 次元を1次元に変換
506
+
507
+ img_np = np.asarray(img)
508
+
509
+ img_np = img_np.shape[0] * img_np.shape[1]
510
+
511
+
512
+
513
+ # データを追加
514
+
515
+ Images.append(img_np)
516
+
517
+
518
+
519
+ img.save('./out/ng_' + f)
520
+
521
+
522
+
523
+ print "Images, Labels = " + str(len(Images)) + ", " + str(len(Labels))
524
+
525
+
526
+
527
+ # ラベルを設定
528
+
529
+ Labels.append(1)
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+ # ガウシアンカーネルによる SVM インスタンス生成
538
+
539
+ clf = svm.SVC(C=100, kernel="rbf")
540
+
541
+
542
+
543
+ # 教師データで学習 (データに対するフィッティング)
544
+
545
+ print "----------------"
546
+
547
+ print "TRAINING NOW..."
548
+
549
+ print "----------------"
550
+
551
+
552
+
553
+ print "画像数:" + str(len(Images))
554
+
555
+ print "ラベル数:" + str(len(Labels))
556
+
557
+
558
+
559
+ # トレーニング
560
+
561
+ clf.fit(Images, Labels)
562
+
563
+
564
+
565
+
566
+
567
+ # 分類する画像データを入れる変数
568
+
569
+ studentImages = []
570
+
571
+
572
+
573
+ # 分類する画像を読み込む
574
+
575
+ for f in os.listdir("./img/"):
576
+
577
+ print "filename: " + f
578
+
579
+
580
+
581
+ # 画像を読み込む
582
+
583
+ img = Image.open("./img/" + f)
584
+
585
+
586
+
587
+ # グレースケール化
588
+
589
+ img = img.convert('L')
590
+
591
+
592
+
593
+ # エッジ抽出
594
+
595
+ img = img.filter(ImageFilter.FIND_EDGES)
596
+
597
+
598
+
599
+ # ダウンサイジング
600
+
601
+ img = img.resize(STANDARD_SIZE)
602
+
603
+
604
+
605
+ # 次元を1次元に変換
606
+
607
+ img_np = np.asarray(img)
608
+
609
+ img_np = img_np.shape[0] * img_np.shape[1]
610
+
611
+
612
+
613
+ # データを追加
614
+
615
+ studentImages.append(img_np)
616
+
617
+
618
+
619
+ img.save('./out/student_' + f)
620
+
621
+
622
+
623
+
624
+
625
+ print "-------------------"
626
+
627
+ print "CLUSTERING NOW..."
628
+
629
+ print "-------------------"
630
+
631
+
632
+
633
+ # 画像データを分類する
634
+
635
+ results = clf.predict(studentImages)
636
+
637
+
638
+
639
+ for r in results:
640
+
641
+ if r == 0:
642
+
643
+ print "ok"
644
+
645
+ elif r == 1:
646
+
647
+ print "ng"
648
+
649
+ else:
650
+
651
+ print "error"
652
+
653
+
654
+
655
+ ```
656
+
657
+
658
+
659
+
660
+
661
+ # 追記2
662
+
663
+ ```
664
+
665
+ /Library/Python/2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning:
666
+
667
+ Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19.
668
+
669
+ Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
670
+
671
+
672
+
673
+ ValueError: X and y have incompatible shapes.
674
+
675
+ X has 1 samples, but y has 170.
676
+
677
+ ```