环境:
python3.6.4
opencv3.4.1.15
运行目标跟踪object_tracking文件夹中的mean函数时报错且不显示视频结果
Traceback (most recent call last):
File "F:\pycharm_python\projects\project_python_test\object_tracking\main.py", line 15, in <module>
height, width, _ = frame.shape
AttributeError: 'NoneType' object has no attribute 'shape'
查找原因基本上看见三个
1.图片不存在(路径不存在, 路径包含中文无法识别) 2.读取的图片内容和默认读取时参数匹配不匹配。(默认读取的是3通道的彩色图)例如读取到的图片是灰度图,就会返回None。3.也可能是路径中有中文(59条消息) AttributeError: 'NoneType' object has no attribute 'shape'_旅人_Eric的博客-CSDN博客
自己分析了一下,肯因为程序中是相对路径,
cap =cv2.VideoCapture("highway.mp4")
改成绝对路径
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文件
import cv2
from tracker import *
# Create tracker object
tracker = EuclideanDistTracker()
cap = cv2.VideoCapture("F:\pycharm_python\projects\project_python_test\object_tracking\highway.mp4")
# Object detection from Stable camera 来自稳定相机的物体检测
object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=40)
# 函数cv2.createBackgroundSubtractorMOG2返回背景比率(background ratio),
#因为相机是固定的所以history=100,varthreshold的值越低,误报的可行就越大
while True:
ret, frame = cap.read()
height, width, _ = frame.shape
# Extract Region of interest提取感兴趣区域
roi = frame[340: 720, 500: 800]
# 1. Object Detection 物体检测
mask = object_detector.apply(roi)
_, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)
#245.255表示只想显示254和255之间的值,即只想显示白色或黑色值
image, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
detections = []
for cnt in contours:
# Calculate area and remove small elements 计算面积,去除小元素
area = cv2.contourArea(cnt)
if area > 1000:
#cv2.drawContours(roi, [cnt], -1, (0, 255, 0), 2) #绘制轮廓
x, y, w, h = cv2.boundingRect(cnt) #找到对象的坐标
detections.append([x, y, w, h])
#一旦创建了对象,我们必须获得边界框的每个位置并将他们插入到单个数组中
# 2. Object Tracking目标跟踪
#将唯一ID关联到对象
boxes_ids = tracker.update(detections)#将带有位置的数组传递给tracker.update()
for box_id in boxes_ids: #为每个对象分配一个唯一的ID
x, y, w, h, id = box_id
cv2.putText(roi, str(id), (x, y - 15), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 3) #绘制矩形框
cv2.imshow("roi", roi)
cv2.imshow("Frame", frame)
cv2.imshow("Mask", mask)
key = cv2.waitKey(30)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
和tracker.py文件
import math
class EuclideanDistTracker:
def __init__(self):
# Store the center positions of the objects存储对象的中心位置
self.center_points = {}
# Keep the count of the IDs保持ID计数
# each time a new object id detected, the count will increase by one每检测到新的对象ID是我技术加1
self.id_count = 0
def update(self, objects_rect):
# Objects boxes and ids对象框和ID
objects_bbs_ids = []
# Get center point of new object获取新对象的中心点
for rect in objects_rect:
x, y, w, h = rect
cx = (x + x + w) // 2
cy = (y + y + h) // 2
# Find out if that object was detected already查明是否已经检测到该对象
same_object_detected = False
for id, pt in self.center_points.items():
#计算中心点之间的欧式距离
dist = math.hypot(cx - pt[0], cy - pt[1])
#如果欧式距离小于25则表明是同一个目标
if dist < 25:
self.center_points[id] = (cx, cy)
print(self.center_points)
objects_bbs_ids.append([x, y, w, h, id])
same_object_detected = True
break
# New object is detected we assign the ID to that object检测到新对象时我们将ID分配该该对象
if same_object_detected is False:
self.center_points[self.id_count] = (cx, cy)
objects_bbs_ids.append([x, y, w, h, self.id_count])
self.id_count += 1
# Clean the dictionary by center points to remove IDS not used anymore按中心点清理字典以删除不再使用的IDS
new_center_points = {}
for obj_bb_id in objects_bbs_ids:
_, _, _, _, object_id = obj_bb_id
center = self.center_points[object_id]
new_center_points[object_id] = center
# Update dictionary with IDs not used removed更新字典
self.center_points = new_center_points.copy()
return objects_bbs_ids
以及原视频:
highway
版权归原作者 weixin_62884045 所有, 如有侵权,请联系我们删除。