๋ฐ์ํ
๋จผ์ ์๋ ๋ช ๋ น์ด๋ฅผ ํตํด OpenCV ๋ฅผ ์ค์นํด์ค๋๋ค.
pip install opencv-python
์ ์ฒด์ฝ๋ ์ ๋๋ค
import cv2 import numpy as np cascadeFace = cv2.CascadeClassifier("./haarcascade_frontalface_alt.xml") cascadeHand = cv2.CascadeClassifier("./haarcascade_hand.xml") def removeFace(img, cascade): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) rects = cascade.detectMultiScale(gray, 1.4, 5) return rects def detectHand(img, cascade): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) rects = cascade.detectMultiScale(gray, 1.4, 5) return rects capt = cv2.VideoCapture(0) delback = cv2.createBackgroundSubtractorMOG2() kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) while capt.isOpened(): success, frame = capt.read() if success: # ๊ฒ์ถ ์์ ycrcb = cv2.cvtColor(frame,cv2.COLOR_BGR2YCrCb) cam1 = cv2.inRange(ycrcb,np.array([0,133,84]),np.array([255,173,127])) cam2 = delback.apply(frame) cam2 = cv2.morphologyEx(cam2, cv2.MORPH_OPEN, kernel) rectFace = removeFace(frame, cascadeFace) hand = cv2.GaussianBlur(cam2, (3,3), 0) k = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) hand = cv2.morphologyEx(hand, cv2.MORPH_CLOSE, k) hand = cv2.erode(hand, k) rectHand = detectHand(frame, cascadeHand) for x1, y1, x2, y2 in rectFace: cv2.rectangle(hand, (x1-100, 0), (x2+1000, 2000), (0,0,0), -1) for x1, y1, x2, y2 in rectHand: cv2.rectangle(frame, (x1-100, y1-100), (x2-100, y2-100), (0,255,0), 7) # ํ๋ฆฐํธ cv2.imshow('Def 1 : ์นด๋ฉ๋ผ', frame) cv2.imshow('Def 2 : ์ ๊ฒ์ถ', cam1) cv2.imshow('Def 3 : ๋ฐฐ๊ฒฝ ์ ๊ฑฐ', cam2) cv2.imshow('Def 4 : First', hand) # ESC key = cv2.waitKey(1) & 0xFF if (key == 27): break capt.release() cv2.destroyAllWindows()
๋๋ต ์ด๋ฌํ ํํ๋ก ์ถ๋ ฅ์ด ๋ฉ๋๋ค.
๋ฐ์ํ
'๐ป๊ธฐ์ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ Python ] scapy๋ก ๋ด ์ปดํจํฐ์ ํจํท ์บก์ณํ๊ธฐ (1) | 2020.10.13 |
---|