首页 > 解决方案 > 如何检测质心并测量两个物体之间的距离?使用鸟瞰?

问题描述

我想检测移动的人及其质心(不使用质心跟踪器),我有走路人的视频,

这是我的代码

import cv2

face_cascade=cv2.CascadeClassifier('haarcascade_fullbody.xml')
frame=cv2.imread('person.png')

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)

for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3)

    cX = int((x+ w) / 2.0)
    cY = int((y + h) / 2.0)

   print(cX)
   print(cY)
   cv2.circle(frame, (cX, cY), 10, (205, 0, 255), -1)
   cv2.imshow('img',frame)
   if cv2.waitKey(1) & 0xFF == ord('q'):
    break
cv2.waitKey()
cv2.destroyAllWindows()

我得到这个输出,请告诉我我的代码有什么问题

标签: pythontensorflowopencvcomputer-visionartificial-intelligence

解决方案


这是您可以检测人并找到其质心+距离的代码。

import cv2
import math


face_cascade=cv2.CascadeClassifier('haarcascade_fullbody.xml')
frame=cv2.imread('dis456.png')

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
print(faces)


p1 = 361  # image pont
p2 = 237
cv2.circle(frame, (p1, p2), 5, ((139,0,0)), -1)
for face in faces:
   (x, y, w, h) = face
   (x1, y1, w1, h1) = faces[0]


   cx1, cy1 = int(x + w / 2), int(y + h / 2)
   cx2, cy2 = int(x1 + w1 / 2), int(y1 + h1 / 2)

   cv2.circle(frame, (cx1, cy1), 2, (0, 245, 30), -1)
   cv2.circle(frame, (cx2, cy2), 2, (0, 245, 30), -1)

  # dist=math.sqrt(((cx1-cx2)**2)+(cy2-cy1)**2))
   cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3)

   dis = (math.sqrt(((cx2 - cx1) ** 2) + ((cy2 - cy1) ** 2)) ** 0.5)
   dis = int(dis)
   print("distance is ",dis)
   cv2.line(frame, (cx1, cy1), (p1,p2 ), (0, 234, 76), 2)

  (x,y,w,h)=face
  cv2.putText(frame, 'distance={}'.format(dis), (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.6, 
 (139, 0, 0))




cv2.imshow('img',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()

在此处输入图像描述


推荐阅读