0


报错AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

环境:

python3.6.4

opencv3.4.1.15

运行目标跟踪object_tracking文件夹中的mean函数时报错且不显示视频结果

  1. Traceback (most recent call last):
  2. File "F:\pycharm_python\projects\project_python_test\object_tracking\main.py", line 15, in <module>
  3. height, width, _ = frame.shape
  4. AttributeError: 'NoneType' object has no attribute 'shape'

查找原因基本上看见三个

1.图片不存在(路径不存在, 路径包含中文无法识别) 2.读取的图片内容和默认读取时参数匹配不匹配。(默认读取的是3通道的彩色图)例如读取到的图片是灰度图,就会返回None。3.也可能是路径中有中文(59条消息) AttributeError: 'NoneType' object has no attribute 'shape'_旅人_Eric的博客-CSDN博客

自己分析了一下,肯因为程序中是相对路径,

  1. cap =cv2.VideoCapture("highway.mp4")

改成绝对路径

  1. cap = cv2.VideoCapture("F:\pycharm_python\projects\project_python_test\object_tracking\highway.mp4")

再次尝试运行main函数,可以显示视频窗口和结果,单依旧报错:

结果大概这样

再继续找别人踩过的坑:

图片路径前加 r ,可以解决部分问题——对我没用。

看到这篇文章写了一个解决办法,但是是针对图片的,俺纯小白不知道视频的能不能这么改。(主要是不知道怎么改)这坑先留着以后再说吧。

(59条消息) 解决easyocr不识别中文路径问题AttributeError: ‘NoneType‘ object has no attribute ‘shape‘_江河的江的博客-CSDN博客

附上有结果的main.py文件

  1. import cv2
  2. from tracker import *
  3. # Create tracker object
  4. tracker = EuclideanDistTracker()
  5. cap = cv2.VideoCapture("F:\pycharm_python\projects\project_python_test\object_tracking\highway.mp4")
  6. # Object detection from Stable camera 来自稳定相机的物体检测
  7. object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=40)
  8. # 函数cv2.createBackgroundSubtractorMOG2返回背景比率(background ratio),
  9. #因为相机是固定的所以history=100,varthreshold的值越低,误报的可行就越大
  10. while True:
  11. ret, frame = cap.read()
  12. height, width, _ = frame.shape
  13. # Extract Region of interest提取感兴趣区域
  14. roi = frame[340: 720, 500: 800]
  15. # 1. Object Detection 物体检测
  16. mask = object_detector.apply(roi)
  17. _, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)
  18. #245.255表示只想显示254和255之间的值,即只想显示白色或黑色值
  19. image, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  20. detections = []
  21. for cnt in contours:
  22. # Calculate area and remove small elements 计算面积,去除小元素
  23. area = cv2.contourArea(cnt)
  24. if area > 1000:
  25. #cv2.drawContours(roi, [cnt], -1, (0, 255, 0), 2) #绘制轮廓
  26. x, y, w, h = cv2.boundingRect(cnt) #找到对象的坐标
  27. detections.append([x, y, w, h])
  28. #一旦创建了对象,我们必须获得边界框的每个位置并将他们插入到单个数组中
  29. # 2. Object Tracking目标跟踪
  30. #将唯一ID关联到对象
  31. boxes_ids = tracker.update(detections)#将带有位置的数组传递给tracker.update()
  32. for box_id in boxes_ids: #为每个对象分配一个唯一的ID
  33. x, y, w, h, id = box_id
  34. cv2.putText(roi, str(id), (x, y - 15), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
  35. cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 3) #绘制矩形框
  36. cv2.imshow("roi", roi)
  37. cv2.imshow("Frame", frame)
  38. cv2.imshow("Mask", mask)
  39. key = cv2.waitKey(30)
  40. if key == 27:
  41. break
  42. cap.release()
  43. cv2.destroyAllWindows()

和tracker.py文件

  1. import math
  2. class EuclideanDistTracker:
  3. def __init__(self):
  4. # Store the center positions of the objects存储对象的中心位置
  5. self.center_points = {}
  6. # Keep the count of the IDs保持ID计数
  7. # each time a new object id detected, the count will increase by one每检测到新的对象ID是我技术加1
  8. self.id_count = 0
  9. def update(self, objects_rect):
  10. # Objects boxes and ids对象框和ID
  11. objects_bbs_ids = []
  12. # Get center point of new object获取新对象的中心点
  13. for rect in objects_rect:
  14. x, y, w, h = rect
  15. cx = (x + x + w) // 2
  16. cy = (y + y + h) // 2
  17. # Find out if that object was detected already查明是否已经检测到该对象
  18. same_object_detected = False
  19. for id, pt in self.center_points.items():
  20. #计算中心点之间的欧式距离
  21. dist = math.hypot(cx - pt[0], cy - pt[1])
  22. #如果欧式距离小于25则表明是同一个目标
  23. if dist < 25:
  24. self.center_points[id] = (cx, cy)
  25. print(self.center_points)
  26. objects_bbs_ids.append([x, y, w, h, id])
  27. same_object_detected = True
  28. break
  29. # New object is detected we assign the ID to that object检测到新对象时我们将ID分配该该对象
  30. if same_object_detected is False:
  31. self.center_points[self.id_count] = (cx, cy)
  32. objects_bbs_ids.append([x, y, w, h, self.id_count])
  33. self.id_count += 1
  34. # Clean the dictionary by center points to remove IDS not used anymore按中心点清理字典以删除不再使用的IDS
  35. new_center_points = {}
  36. for obj_bb_id in objects_bbs_ids:
  37. _, _, _, _, object_id = obj_bb_id
  38. center = self.center_points[object_id]
  39. new_center_points[object_id] = center
  40. # Update dictionary with IDs not used removed更新字典
  41. self.center_points = new_center_points.copy()
  42. return objects_bbs_ids

以及原视频:

highway


本文转载自: https://blog.csdn.net/weixin_62884045/article/details/129519893
版权归原作者 weixin_62884045 所有, 如有侵权,请联系我们删除。

“报错AttributeError: ‘NoneType‘ object has no attribute ‘shape‘”的评论:

还没有评论