来自前景提取的物体运动检测(grabcut)
Posted
技术标签:
【中文标题】来自前景提取的物体运动检测(grabcut)【英文标题】:Motion detection of object from foreground extraction(grabcut) 【发布时间】:2021-03-04 04:20:21 【问题描述】:我是 opencv python 新手
现在,我正在对从前景提取(使用抓取)中获得的对象进行运动检测(在网络摄像头中)。我已经从 Grabcut 获得了一个对象,但我不知道如何编写代码来检测该对象的移动并在网络摄像头屏幕中显示该移动。
非常感谢您
【问题讨论】:
欢迎来到 Stack Overflow。请阅读帮助中心 (***.com/help) 中的信息指南,特别是“如何提出一个好问题”(***.com/help/how-to-ask) 和“如何创建最小的、可重现的示例”( ***.com/help/minimal-reproducible-example). 【参考方案1】:这是使用运动跟踪进行对象检测的代码,但您需要下载一些外部文件,例如 mobilenet 模型、CentroidTracker
import datetime
import imutils
import numpy as np
import csv
# from centroidtracker import CentroidTracker
from pyimagesearch.centroidtracker import CentroidTracker
protopath = "mobilenet_ss/MobileNetSSD_deploy.prototxt"
modelpath = "mobilenet_ss/MobileNetSSD_deploy.caffemodel"
detector = cv2.dnn.readNetFromCaffe(prototxt=protopath, caffeModel=modelpath)
# detector.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
detector.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
fields=['new_id_detected',"total_person_count"]
filename = "person_records.csv"
outputlist=[]
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
tracker = CentroidTracker(maxDisappeared=80, maxDistance=90)
def non_max_suppression_fast(boxes, overlapThresh):
try:
if len(boxes) == 0:
return []
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
overlap = (w * h) / area[idxs[:last]]
idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))
return boxes[pick].astype("int")
except Exception as e:
print("Exception occurred in non_max_suppression : ".format(e))
def main():
cap = cv2.VideoCapture('project_video.mp4')
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
out = cv2.VideoWriter("output/output.mp4", fourcc, 5.0, (600,337))
fps_start_time = datetime.datetime.now()
fps = 0
total_frames = 0
lpc_count = 0
opc_count = 0
object_id_list = []
# dtime = dict()
# dwell_time = dict()
while True:
ret, frame = cap.read()
if not ret:
break
frame = imutils.resize(frame, width=600)
total_frames = total_frames + 1
(H, W) = frame.shape[:2]
#print("h,w",H,W)
blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5)
detector.setInput(blob)
person_detections = detector.forward()
rects = []
for i in np.arange(0, person_detections.shape[2]):
confidence = person_detections[0, 0, i, 2]
if confidence > 0.5:
idx = int(person_detections[0, 0, i, 1])
person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])
(startX, startY, endX, endY) = person_box.astype("int")
rects.append(person_box)
boundingboxes = np.array(rects)
boundingboxes = boundingboxes.astype(int)
rects = non_max_suppression_fast(boundingboxes, 0.3)
objects = tracker.update(rects)
for (objectId, bbox) in objects.items():
x1, y1, x2, y2 = bbox
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
text = "ID: ".format(objectId)
cv2.putText(frame, text, (x1, y1-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
if objectId not in object_id_list:
object_id_list.append(objectId)
# dtime[objectId] = datetime.datetime.now()
# dwell_time[objectId] = 0
# else:
# curr_time = datetime.datetime.now()
# old_time = dtime[objectId]
# time_diff = curr_time - old_time
# dtime[objectId] = datetime.datetime.now()
# sec = time_diff.total_seconds()
# dwell_time[objectId] += sec
# text = "|".format(objectId, int(dwell_time[objectId]))
# cv2.putText(frame, text, (x1, y1-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
fps_end_time = datetime.datetime.now()
time_diff = fps_end_time - fps_start_time
if time_diff.seconds == 0:
fps = 0.0
else:
fps = (total_frames / time_diff.seconds)
fps_text = "FPS: :.2f".format(fps)
cv2.putText(frame, fps_text, (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
lpc_count = len(objects)
opc_count = len(object_id_list)
lpc_txt = "LPC: ".format(lpc_count)
opc_txt = "OPC: ".format(opc_count)
# writing to csv file
outputlist.append([lpc_count,opc_count])
cv2.putText(frame, lpc_txt, (5, 60), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.putText(frame, opc_txt, (5, 90), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
out.write(frame)
cv2.imshow("Application", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(outputlist)
main()```
【讨论】:
以上是关于来自前景提取的物体运动检测(grabcut)的主要内容,如果未能解决你的问题,请参考以下文章