前提・実現したいこと
関数上でメインとなるプログラムを作成して、if name=="main":上で関数を呼び、プログラムを実行したいです。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "##.py", line 47, in <module> camera() File "##.py", line 13, in camera frame = cv2.resize(frame, (int(frame.shape[1]/4), int(frame.shape[0]/4))) AttributeError: 'NoneType' object has no attribute 'shape'
該当のソースコード
python
1-*- coding:utf-8 -*- 2 3import cv2 4 5 def camera(): 6 cap = cv2.VideoCapture(0) 7 before = None 8 while True: 9 ret, frame = cap.read() 10 frame = cv2.resize(frame, (int(frame.shape[1]/4), int(frame.shape[0]/4))) 11 cv2.imshow('Raw Frame', frame) 12 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 13 if before is None: 14 before = gray.copy().astype('float') 15 continue 16 cv2.accumulateWeighted(gray, before, 0.5) 17 mdframe = cv2.absdiff(gray, cv2.convertScaleAbs(before)) 18 cv2.imshow('MotionDetected Frame', mdframe) 19 thresh = cv2.threshold(mdframe, 3, 255, cv2.THRESH_BINARY)[1] 20 image, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 21 max_area = 0 22 target = contours[0] 23 for cnt in contours: 24 area = cv2.contourArea(cnt) 25 if max_area < area and area < 10000 and area > 1000: 26 max_area = area; 27 target = cnt 28 29 if max_area <= 1000: 30 areaframe = frame 31 cv2.putText(areaframe, 'not detected', (0,50), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255,0), 3, cv2.LINE_AA) 32 33 else: 34 x,y,w,h = cv2.boundingRect(target) 35 areaframe = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2) 36 cv2.imshow('MotionDetected Area Frame', areaframe) 37 k = cv2.waitKey(1) 38 if k == 27: 39 break 40 cap.release() 41 cv2.destroyAllWindows() 42 43 44if __name__=="__main__": 45 camera()
回答1件
あなたの回答
tips
プレビュー