コード
import cv2
cap = cv2.VideoCapture(0)
before = None
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, (int(frame.shape[1]/4), int(frame.shape[0]/4)))
cv2.imshow('Raw Frame', frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if before is None:
before = gray.copy().astype('float')
continue
cv2.accumulateWeighted(gray, before, 0.5)
mdframe = cv2.absdiff(gray, cv2.convertScaleAbs(before))
cv2.imshow('MotionDetected Frame', mdframe)
thresh = cv2.threshold(mdframe, 3, 255, cv2.THRESH_BINARY)[1]
image, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
max_area = 0
target = contours[0]
for cnt in contours:
area = cv2.contourArea(cnt)
if max_area < area and area < 10000 and area > 1000:
max_area = area;
target = cnt
if max_area <= 1000:
areaframe = frame
cv2.putText(areaframe, 'not detected', (0,50), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255,0), 3, cv2.LINE_AA)
else:
x,y,w,h = cv2.boundingRect(target)
areaframe = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('MotionDetected Area Frame', areaframe)
k = cv2.waitKey(1)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()