Soy nueva en opencv y estoy intentando encontrar el centro de los rectangulos en un rastreador de objetos de video pero me arroja el siguiente error:
Traceback (most recent call last):
File "track_p_copia.py", line 45, in <module>
objects = ct.update(rects)
File "D:\git_up\job_room\center\center_id.py", line 36, in update
for i in range(0, inputCentroids):
TypeError: only integer scalar arrays can be converted to a scalar index
El codigo que hice para rastrear a las personas en el video y crea los rectangulos es el siguiente:
import cv2
import numpy as np
from center.center_id import CentroidTracker
ct = CentroidTracker()
cap = cv2.VideoCapture(r'C:\Users\Usuario\Downloads\camina2.mp4') #change please
ret, frame1 = cap.read()
ret, frame2 = cap.read()
rects = []
while cap.isOpened():
diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5),0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
(x, y, w, h) = cv2.boundingRect(c)
if cv2.contourArea(c) < 700:
continue
rectangle = [x, y, (x + w), (y + h)]
rects.append(rectangle)
cv2.rectangle(frame1, (rectangle[0], rectangle[1]), (rectangle[2], rectangle[3]),(0, 255, 0), 2)
#Detect centroIDs
objects = ct.update(rects)
if objects is not None:
for (objectID, centroid) in objects.items():
text = "ID:{}".format(objectID)
cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 255, 0), -1)
cv2.imshow("feed", frame1)
frame1 = frame2
ret, frame2= cap.read()
if cv2.waitKey(40) == 32 or cv2.waitKey(40) == 27:
break
cv2.destroyAllWindows()
cap.release()
Y el codigo que tome de un tutorial para buscar el centro ID de los rectangulos y actualiza la posicion de los centros ID es el siguiente:
from scipy.spatial import distance as dist
from collections import OrderedDict
import numpy as np
#Makes a the next unique object ID with
#2 ordered dictionaries
class CentroidTracker():
def __init__(self, maxDisappeared = 50):
self.nextObjectID = 0
self.objects = OrderedDict()
self.disappeared = OrderedDict()
self.maxDisappeared = maxDisappeared
def register(self, centroid):
self.objects[self.nextObjectID] = centroid
self.disappeared[self.nextObjectID] = 0
self.nextObjectID += 1
def deregister(self, objectID):
del self.objects[objectID]
del self.disappeared[objectID]
def update(self, rects):
if len(rects) == 0:
for objectID in self.disappeared.keys():
self.disappeared[objectID] += 1
if self.disappeared[objectID] > self.maxDisappeared:
self.deregister(objectID)
return self.objects
inputCentroids = np.zeros((len(rects), 2), dtype="int")
for (i, (startX, startY, endX, endY)) in enumerate(rects):
cX = int((startX + endX) / 2.0)
cY = int((startY + endY) / 2.0)
inputCentroids[i] = (cX, cY)
if len(self.objects) == 0:
for i in range(0, inputCentroids):
self.register(inputCentroids[i])
else:
objectIDs = list(self.objects.keys())
objectCentroids = list(self.objects.values())
D = dist.cdist(np.array(objectCentroids), inputCentroids)
rows = D.min(axis=1).argsort()
cols = D.argmin(axis=1)[rows]
usedRows = set()
usedCols = set()
for (row, col) in zip(rows, cols):
if row in usedRows or col in usedCols:
continue
objectID = objectIDs[row]
self.objects[objectID] = inputCentroids[col]
self.disappeared[objectID] = 0
usedRows.add(row)
usedCols.add(col)
# compute both the row and column index we have NOT yet
# examined
unusedRows = set(range(0, D.shape[0])).difference(usedRows)
unusedCols = set(range(0, D.shape[1])).difference(usedCols)
if D.shape[0] >= D.shape[1]:
# loop over the unused row indexes
for row in unusedRows:
# grab the object ID for the corresponding row
# index and increment the disappeared counter
objectID = objectIDs[row]
self.disappeared[objectID] += 1
# check to see if the number of consecutive
# frames the object has been marked "disappeared"
# for warrants deregistering the object
if self.disappeared[objectID] > self.maxDisappeared:
self.deregister(objectID)
else:
for col in unusedCols:
self.register(inputCentroids[col])
# return the set of trackable objects
return self.objects
Se que el error tiene que estar en el codigo que yo hice para crear los rectangulos y no en el buscador de los centro ID ya que ese lo tome de un tutorial hecho por un experto.
Alguien podria ayudarme a saber en que me equivoque? . Se que el error tiene que ver con algo de Numpy pero estoy bastante confundida :(