将两个值添加到列表中的一个键,并将列表添加到字典中

时间:2019-12-20 07:30:34

标签: python

我正在尝试获取检测到的边界框的中心坐标,并将其存储在每个FPS的列表中。然后,对于列表中每个检测到的中心坐标,我要将其存储在字典中。

list的输出应该像 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()

1 个答案:

答案 0 :(得分:0)

对不起,但列表在Python中不起作用。改用namedTuple可以解决您的难题。无论如何,命名元组通常与坐标相关的问题一起使用

相关问题