참조 : https://webnautes.tistory.com/1248
1. opencv 설치(파이썬 3.5 pip를 pip3로 실행되도록 함)
pip3 install opencv-python
pip3 install opencv-contrib-python
#결과
C:\Users\Boeun_PC>pip3 install opencv-python
Collecting opencv-python
Downloading https://files.pythonhosted.org/packages/c6/4b/e76b7b69cb33b8248d7b76599b620af68a5fa34c1da1f4068794d905cced/opencv_python-4.2.0.32-cp35-cp35m-win_amd64.whl (33.0MB)
|################################| 33.1MB 128kB/s
Requirement already satisfied: numpy>=1.11.1 in c:\python\python35\lib\site-packages (from opencv-python) (1.17.0)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.2.0.32
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
C:\Users\Boeun_PC>pip3 install opencv-contrib-python
Collecting opencv-contrib-python
Downloading https://files.pythonhosted.org/packages/fc/c1/03a1c08c5ca3336b49aaf10b3046a41e7ca42c67834c9d04df07e200d13e/opencv_contrib_python-4.2.0.32-cp35-cp35m-win_amd64.whl (39.5MB)
|################################| 39.5MB 57kB/s
Requirement already satisfied: numpy>=1.11.1 in c:\python\python35\lib\site-packages (from opencv-contrib-python) (1.17.0)
Installing collected packages: opencv-contrib-python
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 액세스가 거부되었습니다: 'c:\\python\\python35\\Lib\\site-packages\\cv2\\cv2.cp35-win_amd64.pyd'
Consider using the `--user` option or check the permissions.
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
C:\Users\Boeun_PC>
2. 프로그래밍
import cv2
import numpy as np
print("OpenCV Version is :", cv2.__version__)
cap = cv2.VideoCapture("192.168.0.123_01_20200216_010002.avi")
#cap = cv2.imread("D:\sensorgatetest_avi\192.168.0.123_01_20200216_010002.avi")
# 옵션 설명 http://layer0.authentise.com/segment-background-using-computer-vision.html
fgbg = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=500, detectShadows=0)
while(1):
if(cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT)):
cap.open("192.168.0.123_01_20200216_010002.avi")
ret, frame = cap.read()
width = frame.shape[1]
height = frame.shape[0]
frame = cv2.resize(frame, (int(width*0.5), int(height*0.5)))
fgmask = fgbg.apply(frame)
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(fgmask)
for index, centroid in enumerate(centroids):
if stats[index][0] == 0 and stats[index][1] == 0:
continue
if np.any(np.isnan(centroid)):
continue
x, y, width, height, area = stats[index]
centerX, centerY = int(centroid[0]), int(centroid[1])
if area > 10:
cv2.circle(frame, (centerX, centerY), 1, (0, 255, 0), 2)
cv2.rectangle(frame, (x, y), (x + width, y + height), (0, 0, 255))
cv2.imshow('mask',fgmask)
cv2.imshow('frame',frame)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
참조 : https://076923.github.io/posts/Python-opencv-4/
3. 결과