level 4
贴吧用户_524aA9E
楼主
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
ret, frame = cap.read()
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('a'):
cv2.imwrite("camera.jpg",frame)
break
cap.release()
cv2.destroyAllWindows()
cap = cv2.VideoCapture(0)
img = cv2.imread("C:\\Users\\Administrator\\camera.jpg",1)
gray = cv2.imread("C:\\Users\\Administrator\\camera.jpg",0)
img1 = cv2.imread("C:\\Users\\Administrator\\Desktop\\w.png")
img2 = cv2.imread("C:\\Users\\Administrator\\Desktop\\s.png")
img3 = cv2.imread("C:\\Users\\Administrator\\Desktop\\l.png")
img4 = cv2.imread("C:\\Users\\Administrator\\Desktop\\r.png")
img_template1 = cv2.imread("C:\\Users\\Administrator\\Desktop\\w.png",0)
img_template2 = cv2.imread("C:\\Users\\Administrator\\Desktop\\s.png",0)
img_template3 = cv2.imread("C:\\Users\\Administrator\\Desktop\\l.png",0)
img_template4 = cv2.imread("C:\\Users\\Administrator\\Desktop\\r,png",0)
h1, w1 = img_template1.shape[:2]
h2, w2= img_template2.shape[:2]
h3, w3 = img_template3.shape[:2]
h4, w4 = img_template4.shape[:2]
res1 = cv2.matchTemplate(gray,img_template1,cv2.TM_SQDIFF)
res2 = cv2.matchTemplate(gray,img_template2,cv2.TM_SQDIFF)
res3 = cv2.matchTemplate(gray,img_template3,cv2.TM_SQDIFF)
res4 = cv2.matchTemplate(gray,img_template3,cv2.TM_SQDIFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res1)
left_top = max_loc
right_bottom = (left_top[0] + w1,left_top[1] + h1)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向前")
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res2)
left_top = max_loc
right_bottom = (left_top[0] + w2,left_top[1] + h2)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向后")
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res3)
left_top = max_loc
right_bottom = (left_top[0] + w3,left_top[1] + h3)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向左")
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res4)
left_top = max_loc
right_bottom = (left_top[0] + w4,left_top[1] + h4)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向右")
AttributeError Traceback (most recent call last)
<ipython-input-3-bc4b27eec473> in <module>()
36 h2, w2= img_template2.shape[:2]
37 h3, w3 = img_template3.shape[:2]
---> 38 h4, w4 = img_template4.shape[:2]
39
40
AttributeError: 'NoneType' object has no attribute 'shape'
求助各位大佬,这类错误应该怎么改啊![[泪]](/static/emoticons/u6cea.png)
![[泪]](/static/emoticons/u6cea.png)
2020年04月21日 12点04分
1
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
ret, frame = cap.read()
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('a'):
cv2.imwrite("camera.jpg",frame)
break
cap.release()
cv2.destroyAllWindows()
cap = cv2.VideoCapture(0)
img = cv2.imread("C:\\Users\\Administrator\\camera.jpg",1)
gray = cv2.imread("C:\\Users\\Administrator\\camera.jpg",0)
img1 = cv2.imread("C:\\Users\\Administrator\\Desktop\\w.png")
img2 = cv2.imread("C:\\Users\\Administrator\\Desktop\\s.png")
img3 = cv2.imread("C:\\Users\\Administrator\\Desktop\\l.png")
img4 = cv2.imread("C:\\Users\\Administrator\\Desktop\\r.png")
img_template1 = cv2.imread("C:\\Users\\Administrator\\Desktop\\w.png",0)
img_template2 = cv2.imread("C:\\Users\\Administrator\\Desktop\\s.png",0)
img_template3 = cv2.imread("C:\\Users\\Administrator\\Desktop\\l.png",0)
img_template4 = cv2.imread("C:\\Users\\Administrator\\Desktop\\r,png",0)
h1, w1 = img_template1.shape[:2]
h2, w2= img_template2.shape[:2]
h3, w3 = img_template3.shape[:2]
h4, w4 = img_template4.shape[:2]
res1 = cv2.matchTemplate(gray,img_template1,cv2.TM_SQDIFF)
res2 = cv2.matchTemplate(gray,img_template2,cv2.TM_SQDIFF)
res3 = cv2.matchTemplate(gray,img_template3,cv2.TM_SQDIFF)
res4 = cv2.matchTemplate(gray,img_template3,cv2.TM_SQDIFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res1)
left_top = max_loc
right_bottom = (left_top[0] + w1,left_top[1] + h1)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向前")
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res2)
left_top = max_loc
right_bottom = (left_top[0] + w2,left_top[1] + h2)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向后")
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res3)
left_top = max_loc
right_bottom = (left_top[0] + w3,left_top[1] + h3)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向左")
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res4)
left_top = max_loc
right_bottom = (left_top[0] + w4,left_top[1] + h4)
cv2.rectangle(gray, left_top, right_bottom, 255, 2)
print("小车向右")
AttributeError Traceback (most recent call last)
<ipython-input-3-bc4b27eec473> in <module>()
36 h2, w2= img_template2.shape[:2]
37 h3, w3 = img_template3.shape[:2]
---> 38 h4, w4 = img_template4.shape[:2]
39
40
AttributeError: 'NoneType' object has no attribute 'shape'
求助各位大佬,这类错误应该怎么改啊