我试图在检测到边界框时获取中心坐标,并将其存储在每个 FPS 的列表中。然后对于列表的每个检测到的中心坐标,我想将它存储在字典中。
列表的输出应该像 center_coord = ["point1" : [x1, y1], "point2" : [x2, y2], ........ ]
从该列表中,我想将它附加到一个字典中,例如 box_center = {"point1" : [x1, y1], "point2" : {x2, y2],.....}
import cv2
from darkflow.net.build import TFNet
import numpy as np
import time
from collections import defaultdict
options = {
'model': 'cfg/yolov2.cfg',
'load': 'bin/yolov2.weights',
'threshold': 0.8,
'gpu': 0.8
}
tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
box_center = defaultdict(list) #to store the list of center coordinates
center_coord = [] #to store the center coordinate at each FPS
while True:
stime = time.time()
ret, frame = capture.read()
if ret:
results = tfnet.return_predict(frame)
for color, result in zip(colors, results):
tl = (result['topleft']['x'], result['topleft']['y'])
br = (result['bottomright']['x'], result['bottomright']['y'])
center_x = ((tl[0] + br[0])/2) #Here it gets the midpoint of two X coordinates
center_y = ((tl[1] + br[1])/2) #Here it gets the midpoint of two Y coordinates
center_coord.append([center_x, center_y]) #Here i append the X and Y coordinate in a list
label = result['label']
confidence = result['confidence']
text = '{}: {:.0f}%'.format(label, confidence * 100)
frame = cv2.rectangle(frame, tl, br, color, 5)
frame = cv2.putText(frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
cv2.imshow('frame', frame)
print('FPS {:.1f}'.format(1 / (time.time() - stime)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
抱歉,列表在 Python 中不起作用。改用 namedTuple,这应该可以解决您的挑战。无论如何,命名元组通常与坐标相关问题结合使用
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句