ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 라즈베리파이에서 OpenCV 정리_1
    전공공부/리눅스 2021. 7. 18. 23:00

    안녕하세요. 개알못입니다~

     

    좀더 여유 있을 때 자세한 설명으로 돌아오겠습니다...!!

     


    1. 라즈베리파이에서 카메라 촬영하기

    # 'test_1'는 파일명
    
    raspistill -o test_1.jpg

     

    2. opencv 설치 확인하기

    pkg-config --modversion opencv

     

    3. opencv로 카메라 이용하기 (상하반전, 색 변경)

    # 개알못
    
    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    cap.set(3,640) # set Width
    cap.set(4,480) # set Height
    
    while(True):
        ret, frame = cap.read()
        frame = cv2.flip(frame, -1) # 상하 반전
    #   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)	# gray color
        
        cv2.imshow('frame', frame)	# 'frame' 이름
    #   cv2.imshow('gray', gray)	# gray color
        
        k = cv2.waitKey(30) & 0xff
        if k == 27: # ESC 누르면 종료
            break
            
    cap.release()
    cv2.destroyAllWindows()

     

    4. Haar feature로 얼굴 인식하기

    # 개알못
    
    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    cap.set(3,640) # set Width
    cap.set(4,480) # set Height
    
    faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    
    while True:
        ret, img = cap.read()
        img = cv2.flip(img, -1) # 상하반전
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(20, 20)
        )
        
        for (x,y,w,h) in faces:
        
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]
            
        cv2.imshow('video',img) # 'video' 이름
        
        k = cv2.waitKey(30) & 0xff
        if k == 27: # ESC 누르면 종료
            break
            
    cap.release()
    cv2.destroyAllWindows()

     

    5. Haar feature로 얼굴 and 눈 인식하기

    # 개알못
    
    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    cap.set(3,640) # set Width
    cap.set(4,480) # set Height
    
    faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
    
    while True:
        ret, img = cap.read()
        img = cv2.flip(img, -1) # 상하반전
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(20, 20)
    	)
    
        for (x,y,w,h) in faces:
        
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]
    
            eyes = eyeCascade.detectMultiScale(
                roi_gray,
                scaleFactor= 1.5,
                minNeighbors=10,
                minSize=(5, 5),
            )
            
           for (ex, ey, ew, eh) in eyes:
           
               cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
                    
        cv2.imshow('video',img) 
        
        k = cv2.waitKey(30) & 0xff
        if k == 27: # ESC 누르면 종료
            break
            
    cap.release()
    cv2.destroyAllWindows()

     

    6. Haar feature로 얼굴 and 미소 인식하기

    # 개알못
    
    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    cap.set(3,640) # set Width
    cap.set(4,480) # set Height
    
    faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    smileCascade = cv2.CascadeClassifier('haarcascade_smile.xml')
    
    while True:
        ret, img = cap.read()
        img = cv2.flip(img, -1) # 상하반전
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(20, 20)
    	)
    
        for (x,y,w,h) in faces:
        
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]
    
    		smile = smileCascade.detectMultiScale(
                roi_gray,
                scaleFactor = 1.5,
                minNeighbors = 15,
                minSize = (25, 25),
                )
            
           for (xx, yy, ww, hh) in smile:
           
               cv2.rectangle(roi_color, (xx, yy), (xx + ww, yy + hh), (0, 255, 0), 2)
                    
        cv2.imshow('video',img) 
        
        k = cv2.waitKey(30) & 0xff
        if k == 27: # ESC 누르면 종료
            break
            
    cap.release()
    cv2.destroyAllWindows()

     

    7. Haar feature로 얼굴 and 눈 and 미소 인식하기

    # 개알못
    
    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(0)
    cap.set(3,640) # set Width
    cap.set(4,480) # set Height
    
    faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
    smileCascade = cv2.CascadeClassifier('haarcascade_smile.xml')
    
    while True:
        ret, img = cap.read()
        img = cv2.flip(img, -1) # 상하반전
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        
        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.2,
            minNeighbors=5,
            minSize=(20, 20)
        )
    
        for (x,y,w,h) in faces:
        
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]
            
            eyes = eyeCascade.detectMultiScale(
                roi_gray,
                scaleFactor= 1.5,
                minNeighbors=10,
                minSize=(5, 5),
            )
    
            for (ex, ey, ew, eh) in eyes:
           
                cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
                  
            smile = smileCascade.detectMultiScale(
                roi_gray,
                scaleFactor = 1.5,
                minNeighbors = 15,
                minSize = (25, 25),
                )
            
            for (xx, yy, ww, hh) in smile:
           
               cv2.rectangle(roi_color, (xx, yy), (xx + ww, yy + hh), (0, 255, 0), 2)
                    
        cv2.imshow('video',img) 
        
        k = cv2.waitKey(30) & 0xff
        if k == 27: # ESC 누르면 종료
            break
            
    cap.release()
    cv2.destroyAllWindows()

     

    '전공공부 > 리눅스' 카테고리의 다른 글

    [리눅스] 기본 명령어 2  (0) 2021.08.10
    [리눅스] 기본 명령어 1  (0) 2021.08.04
    라즈베리파이에서 OpenCV 정리_2  (0) 2021.07.19

    댓글

Designed by Tistory.