OpenCV 개발(2)-신경 망 사용 예시

9101 단어 OpenCVPython다 층
OpenCV 3.4 의 신경 네트워크 기능 은 주로 다음 과 같은 세 가 지 를 제공한다.
  • ml 모듈 의 다 중 감지 기(Artificial Neural Networks-Multi-Layer Perceptrons)는 MLP 의 생 성,훈련,파라미터 설정 등 함 수 를 제공 합 니 다.예 를 들 어
    
    static Ptr< ANN_MLP >   create ()
        Creates empty model. 
    static Ptr< ANN_MLP >   load (const String &filepath)
        Loads and creates a serialized ANN from a file. 
    void    setAnnealFinalT (double val)
    void    setAnnealInitialT (double val)
    void    setAnnealItePerStep (int val)
    virtual void    setBackpropMomentumScale (double val)=0
    virtual void    setBackpropWeightScale (double val)=0
    virtual void    setLayerSizes (InputArray _layer_sizes)=0
    virtual void    setRpropDW0 (double val)=0
    virtual void    setRpropDWMax (double val)=0
    
    enum    ActivationFunctions { 
        IDENTITY = 0, 
        SIGMOID_SYM = 1, 
        GAUSSIAN = 2, 
        RELU = 3, 
        LEAKYRELU = 4 
    }
    
    enum    TrainFlags { 
        UPDATE_WEIGHTS = 1, 
        NO_INPUT_SCALE = 2, 
        NO_OUTPUT_SCALE = 4 
    }
    enum    TrainingMethods { 
        BACKPROP =0, 
        RPROP = 1, 
        ANNEAL = 2 
    }
  • 도움말 문 서 를 참조 하 십시오.
  • DNN 모듈 은 깊이 있 는 네트워크 와 매개 변수 설정,TensorFlow,Caffe,Torch 모델 을 만 들 고 불 러 오 는 방법 과 종 류 를 많이 제공 합 니 다.예 를 들 어
    class   cv::dnn::BackendNode
        Derivatives of this class encapsulates functions of certain backends.
    class   cv::dnn::BackendWrapper
        Derivatives of this class wraps cv::Mat for different backends and targets.
    class   cv::dnn::Dict
        This class implements name-value dictionary, values are instances of DictValue.
    struct      cv::dnn::DictValue
        This struct stores the scalar value (or array) of one of the following type: double, cv::String or int64. 
    class   cv::dnn::Layer
        This interface class allows to build new Layers - are building blocks of networks.
    class   cv::dnn::LayerParams
        This class provides all data needed to initialize layer. 
    class   cv::dnn::Net
        This class allows to create and manipulate comprehensive artificial neural networks.
        Mat     cv::dnn::blobFromImages (const std::vector< Mat > &images, double scalefactor=1.0, Size size=Size(), const Scalar &mean=Scalar(), bool swapRB=true, bool crop=true)
        Creates 4-dimensional blob from series of images. Optionally resizes and crops images from center, subtract mean values, scales values by scalefactor, swap Blue and Red channels.
    void    cv::dnn::NMSBoxes (const std::vector< Rect > &bboxes, const std::vector< float > &scores, const float score_threshold, const float nms_threshold, std::vector< int > &indices, const float eta=1.f, const int top_k=0)
        Performs non maximum suppression given boxes and corresponding scores.
    
    Net     cv::dnn::readNetFromCaffe (const String &prototxt, const String &caffeModel=String())
        Reads a network model stored in Caffe framework's format.
    Net     cv::dnn::readNetFromDarknet (const String &cfgFile, const String &darknetModel=String())
        Reads a network model stored in Darknet model files.
    Net     cv::dnn::readNetFromTensorflow (const String &model, const String &config=String())
        Reads a network model stored in TensorFlow framework's format. 
    Net     cv::dnn::readNetFromTorch (const String &model, bool isBinary=true)
    도움말 문 서 를 참조 하 십시오.
  • 제3자 깊이 네트워크 도구 입 니 다.자세 한 내용 은 도움말 문 서 를 보십시오.

  • 다음은 예 시 를 드 리 겠 습 니 다.1.MLP 기반 식별.이 프로그램 은 4 가지 동물 데 이 터 를 인공 적 으로 생 성하 여 MLP 네트워크 트 레이 닝 모델 을 통 해 테스트 데이터 유형 을 측정 한다.
        #exam1.py 
        import cv2
        import numpy as np
        from random import randint
        #  MLP  ,       、    、          。
        animals_net = cv2.ml.ANN_MLP_create()
        animals_net.setTrainMethod(cv2.ml.ANN_MLP_RPROP | cv2.ml.ANN_MLP_UPDATE_WEIGHTS)
        animals_net.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM)
        animals_net.setLayerSizes(np.array([3, 6, 4]))
        animals_net.setTermCriteria(( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ))
        #            
        def dog_sample():
            return [randint(10, 20), 1, randint(38, 42)]
        def dog_class():
            return [1, 0, 0, 0]
        def condor_sample():
            return [randint(3,10), randint(3,5), 0]
        def condor_class():
            return [0, 1, 0, 0]
        def dolphin_sample():
            return [randint(30, 190), randint(5, 15), randint(80, 100)]
        def dolphin_class():
            return [0, 0, 1, 0]
        def dragon_sample():
            return [randint(1200, 1800), randint(30, 40), randint(160, 180)]
        def dragon_class():
            return [0, 0, 0, 1]
        #               (  )
        def record(sample, classification):
            return (np.array([sample], dtype=np.float32), np.array([classification], dtype=np.float32))
        #  5000     
        records = []
        RECORDS = 5000
        for x in range(0, RECORDS):
            records.append(record(dog_sample(), dog_class()))
            records.append(record(condor_sample(), condor_class()))
            records.append(record(dolphin_sample(), dolphin_class()))
            records.append(record(dragon_sample(), dragon_class()))
        #  MLP  
        EPOCHS = 2
        for e in range(0, EPOCHS):
            print("Epoch %d:" % e)
            for t, c in records:
                animals_net.train(t, cv2.ml.ROW_SAMPLE, c)
        #        
        TESTS = 100
        dog_results = 0
        for x in range(0, TESTS):
            clas = int(animals_net.predict(np.array([dog_sample()], dtype=np.float32))[0])
            print("class: %d" % clas)
            if (clas) == 0:
                dog_results += 1
        condor_results = 0
        for x in range(0, TESTS):
            clas = int(animals_net.predict(np.array([condor_sample()], dtype=np.float32))[0])
            print("class: %d" % clas)
            if (clas) == 1:
                condor_results += 1
        dolphin_results = 0
        for x in range(0, TESTS):
            clas = int(animals_net.predict(np.array([dolphin_sample()], dtype=np.float32))[0])
            print("class: %d" % clas)
            if (clas) == 2:
                dolphin_results += 1
        dragon_results = 0
        for x in range(0, TESTS):
            clas = int(animals_net.predict(np.array([dragon_sample()], dtype=np.float32))[0])
            print("class: %d" % clas)
            if (clas) == 3:
                dragon_results += 1
        #       
        print("Dog accuracy: %f%%" % (dog_results))
        print("condor accuracy: %f%%" % (condor_results))
        print("dolphin accuracy: %f%%" % (dolphin_results))
        print("dragon accuracy: %f%%" % (dragon_results))
    

    2.DN 기반 식별.이 프로그램 은 미리 훈련 된 caffe 모델 을 불 러 와 카메라 에서 얻 은 이미지 에서 얼굴 을 감지 한다.
    import numpy as np
    import argparse
    import cv2 as cv
    #   ImportError,       PYTHONPATH Python        。
    #     ,      (        )。
    try:
        import cv2 as cv
    except ImportError:
        raise ImportError('Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, '
                          'configure environemnt variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)')
    #  DNN  
    from cv2 import dnn
    inWidth = 300
    inHeight = 300
    confThreshold = 0.5
    #      opencv3.4\sources\samples\dnn\face_detector   ,         OpenCV3.4        
    prototxt = 'face_detector/deploy.prototxt'
    # caffe        ,   opencv3.4\sources\samples\dnn\face_detector        
    caffemodel = 'face_detector/res10_300x300_ssd_iter_140000.caffemodel'
    #  caffe           
    if __name__ == '__main__':
        net = dnn.readNetFromCaffe(prototxt, caffemodel)
        cap = cv.VideoCapture(0)
        while True:
            ret, frame = cap.read()
            cols = frame.shape[1]
            rows = frame.shape[0]
                    #             ,        ,    
            net.setInput(dnn.blobFromImage(frame, 1.0, (inWidth, inHeight), (104.0, 177.0, 123.0), False, False))
            detections = net.forward()
            perf_stats = net.getPerfProfile()
            print('Inference time, ms: %.2f' % (perf_stats[0] / cv.getTickFrequency() * 1000))
            for i in range(detections.shape[2]):
                confidence = detections[0, 0, i, 2]
                if confidence > confThreshold:
                    xLeftBottom = int(detections[0, 0, i, 3] * cols)
                    yLeftBottom = int(detections[0, 0, i, 4] * rows)
                    xRightTop = int(detections[0, 0, i, 5] * cols)
                    yRightTop = int(detections[0, 0, i, 6] * rows)
                    cv.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop, yRightTop),
                                 (0, 255, 0))
                    label = "face: %.4f" % confidence
                    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
                    cv.rectangle(frame, (xLeftBottom, yLeftBottom - labelSize[1]),
                                        (xLeftBottom + labelSize[0], yLeftBottom + baseLine),
                                        (255, 255, 255), cv.FILLED)
                    cv.putText(frame, label, (xLeftBottom, yLeftBottom),
                               cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
            cv.imshow("detections", frame)
            if cv.waitKey(1) != -1:
                break

    좋은 웹페이지 즐겨찾기