gpt4 book ai didi

python - opencv python中的运动跟踪

转载 作者:行者123 更新时间:2023-12-04 17:46:48 24 4
gpt4 key购买 nike

所以我一直在尝试制作一个运动跟踪器来跟踪狗在视频中移动(自上而下录制)检索显示狗的裁剪视频并忽略其余背景。

我首先尝试使用来自 this link 的 opencv 3 中的可用算法(BOOSTING、MIL、KCF、TLD、MEDIANFLOW、GOTURN(返回错误,无法解决))进行对象跟踪我什至尝试通过减去第一帧来进行运动跟踪的基本算法,但它们都没有给出好的结果。 Link

我更喜欢带有预设矩形框的代码,一旦检测到运动区域,它就会包围运动区域。像这样的 video

我对 OPENCV 不是很熟悉,但我相信单一运动跟踪不应该成为问题,因为已经完成了大量工作。我应该考虑其他库/API 还是有更好的代码/教程我可以遵循来完成这项工作?我的观点是稍后将其与神经网络一起使用(这就是我尝试使用 python/opencv 解决它的原因)

感谢您的帮助/建议

编辑:

我删除了以前的代码以使帖子更清晰。

此外,根据我得到的反馈和进一步的研究,我能够修改一些代码以使其接近我想要的结果。但是,我仍然有一个烦人的跟踪问题。似乎第一帧会影响跟踪的其余部分,因为即使在狗移动之后,它也会继续检测它的第一个位置。我试图使用标志将跟踪限制为仅 1 个 Action ,但检测变得一团糟。这是显示结果的代码和图片:

jimport imutils
import time
import cv2

previousFrame = None

def searchForMovement(cnts, frame, min_area):

text = "Undetected"

flag = 0

for c in cnts:
# if the contour is too small, ignore it
if cv2.contourArea(c) < min_area:
continue

#Use the flag to prevent the detection of other motions in the video
if flag == 0:
(x, y, w, h) = cv2.boundingRect(c)

#print("x y w h")
#print(x,y,w,h)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
text = "Detected"
flag = 1

return frame, text

def trackMotion(ret,frame, gaussian_kernel, sensitivity_value, min_area):


if ret:

# Convert to grayscale and blur it for better frame difference
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (gaussian_kernel, gaussian_kernel), 0)



global previousFrame

if previousFrame is None:
previousFrame = gray
return frame, "Uninitialized", frame, frame



frameDiff = cv2.absdiff(previousFrame, gray)
thresh = cv2.threshold(frameDiff, sensitivity_value, 255, cv2.THRESH_BINARY)[1]

thresh = cv2.dilate(thresh, None, iterations=2)
_, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

frame, text = searchForMovement(cnts, frame, min_area)
#previousFrame = gray

return frame, text, thresh, frameDiff




if __name__ == '__main__':

video = "Track.avi"
video0 = "Track.mp4"
video1= "Ntest1.avi"
video2= "Ntest2.avi"

camera = cv2.VideoCapture(video1)
time.sleep(0.25)
min_area = 5000 #int(sys.argv[1])

cv2.namedWindow("Security Camera Feed")


while camera.isOpened():

gaussian_kernel = 27
sensitivity_value = 5
min_area = 2500

ret, frame = camera.read()

#Check if the next camera read is not null
if ret:
frame, text, thresh, frameDiff = trackMotion(ret,frame, gaussian_kernel, sensitivity_value, min_area)

else:
print("Video Finished")
break


cv2.namedWindow('Thresh',cv2.WINDOW_NORMAL)
cv2.namedWindow('Frame Difference',cv2.WINDOW_NORMAL)
cv2.namedWindow('Security Camera Feed',cv2.WINDOW_NORMAL)

cv2.resizeWindow('Thresh', 800,600)
cv2.resizeWindow('Frame Difference', 800,600)
cv2.resizeWindow('Security Camera Feed', 800,600)
# uncomment to see the tresh and framedifference displays
cv2.imshow("Thresh", thresh)
cv2.imshow("Frame Difference", frameDiff)



cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("Security Camera Feed", frame)

key = cv2.waitKey(3) & 0xFF
if key == 27 or key == ord('q'):
print("Bye")
break

camera.release()
cv2.destroyAllWindows()

这张图片显示了第一帧如何仍然影响帧差异结果,这迫使盒子覆盖没有运动的区域。

Result showing the frame difference and video display

这显示了一个案例,当运动被忽略时,不再存在的运动(与视频的第二帧和第一帧的帧差异)被错误地检测到。当我允许多重跟踪时,它同时跟踪两者,这仍然是错误的,因为它检测到一个空白区域。

enter image description here

有没有人知道代码在哪里错误或缺少?我一直在尝试,但无法让它正常工作。

提前致谢!!

最佳答案

为了包括运动检测,我在 NPM Registry 和 docker hub 上创建了通用组件这会检测客户端网络摄像头(React 应用程序)上的运动,并在基于开放式 CV 的 Python 中使用服务器所以客户端只捕获网络摄像头图像,服务器使用 OPENCV 分析这些图像以确定是否有运动客户端可以指定一个回调函数,每次有 Action 时服务器都会调用该回调函数服务器只是一个 docker 镜像,您可以拉取并运行它并将其 URL 指定给客户端

NPM 注册中心(客户端)

注册表链接:

https://www.npmjs.com/settings/kunalpimparkhede/packages

命令

npm install motion-detector-client

Docker 镜像(服务器)

链接

https://hub.docker.com/r/kunalpimparkhede/motiondetectorwebcam

命令

docker pull kunalpimparkhede/motiondetectorwebcam

你只需要写下面的代码就可以实现运动检测

用法:

import MotionDetectingClient from './MotionDetectingClient';

<MotionDetectingClient server="http://0.0.0.0:8080" callback={handleMovement}/>

function handleMovement(pixels)
{
console.log("Movement By Pixel="+pixels)
}

在服务器端:只需在端口 8080 上启动 docker 服务器:

docker run --name motion-detector-server-app -P 8080:5000 motion-detector-server-app

关于python - opencv python中的运动跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48088534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com